arturictus / easy_serializer Goto Github PK
View Code? Open in Web Editor NEWSemantic way to serialize your objects
License: MIT License
Semantic way to serialize your objects
License: MIT License
Would be nice to add options for handling errors.
Serializer.call(object, rescue: ->(error) { puts error})
class AddressSerializer
attribute :country # This will be the attribute that ends up in the blop
end
class CompanySerializer
attribute :country do |obj|
# This block never gets called
obj.address.upcase
end
attribute :address,
key: false,
serializer: AddressSerializer
end
example:
having:
user = User.first
we can have multiple serializers:
class UserSerializer < EasySerializer::Base
cache
attribute :name
end
# cache key should be: ['UserSerializer', user]
module MyModule
class UserSerializer < EasySerializer::Base
cache
attributes :name, :lastname
end
end
# cache key should be: ['MyModule::UserSerializer', user]
class SellerSerializer < EasySerializer::Base
cache
attributes :name, :lastname
collection :products, serializer: Product
end
# cache key should be: ['SellerSerializer', user]
this way is easier to maintein different serializers and caches
collection :weather_observations, key: :weather_observation, serializer: WeatherObservationSerializer, cache: true
output:
{:weather_observation=>[{:high=>"26", :low=>"13", :icon=>"partlycloudy"}, {:high=>"26", :low=>"13", :icon=>"partlycloudy"}, {:high=>"26", :low=>"13", :icon=>"partlycloudy"}]}
The correct data
collection :weather_observations, key: :weather_observation, serializer: WeatherObservationSerializer
output:
{:weather_observation=>[{:high=>"26", :low=>"13", :icon=>"partlycloudy"}, {:high=>"26", :low=>"14", :icon=>"partlycloudy"}, {:high=>"24", :low=>"14", :icon=>"chancerain"}]}
Add custom errors for rescuing from them and callback to rescue from them
class MySerializer < EasySerializer::Base
rescue_from :all, do: ->(e) { "log error" }, default: nil
rescue_from EasySerializer::NoMethodError, do: ->(e) { "log error" }, default: nil
attribute :name, rescue_from: {errors: :all, do: ->(e) { "log error" }, reraise: true }
attribute :lastname do
rescue_from(EasySerializer::NoMethodError,
do: ->(e) { "log error" },
default: nil) do
raise 'thing'
end
end
end
obj = OpenStruct.new(name: 'foo')
class Nested < EasySerializer::Base
attribute :name
end
class Seri < EasySerializer::Base
attribute :nested, serializer: Nested, cache: true
end
Seri.call(obj) # => TypeError: can't dump hash with default proc
class UserSerializer < EasySerializer::Base
attribute :ready_estrage_method_name
end
class Example< EasySerializer::Base
attribute :user, serializer: UserSerializer
end
Example.call(OpenStruct.new)
class Example < EasySerializer::Base
attribute :id, transform: :to_s
def to_s(value)
value.to_s
end
end
example:
attribute :seat, :class_type
# it should have to be written like:
# attributes :seat, :class_type
This raises this error: TypeError: no implicit conversion of Symbol into Hash
It would be nicer to do a message like:
Did you mean "attributes"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.