Code Monkey home page Code Monkey logo

easy_serializer's People

Contributors

arturictus avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

easy_serializer's Issues

Error handling

Would be nice to add options for handling errors.

Serializer.call(object, rescue: ->(error) { puts error})

cache key should be composed with `class.name`

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 with cache true serializes the first object

caching

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"}]}

Not caching:

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"}]}

Custom error

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

When passing serializer do not crash if nil

class UserSerializer < EasySerializer::Base
  attribute :ready_estrage_method_name
end
class Example< EasySerializer::Base
  attribute :user, serializer: UserSerializer
end
Example.call(OpenStruct.new)

Add transform attribute

class Example < EasySerializer::Base
  attribute :id, transform: :to_s
  def to_s(value)
    value.to_s
  end
end

Error when passing two attributes to the `attribute` method

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"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.