lib/undo/serializer/active_model.rb in undo-serializer-active_model-0.0.2 vs lib/undo/serializer/active_model.rb in undo-serializer-active_model-0.0.3

- old
+ new

@@ -1,16 +1,19 @@ +require "active_support" + module Undo module Serializer class ActiveModel - VERSION = "0.0.2" - - def initialize(model_serializer = nil) - @model_serializer = model_serializer + def initialize(*args) + options = args.extract_options! + @serializer = args.first + @serializer_source = options.fetch :serializer, + ->(object) { object.active_model_serializer.new object } end def serialize(object) - model_serializer(object).as_json + serializer(object).as_json end def deserialize(hash) hash.each do |object_class, data| next unless data.is_a? Hash @@ -33,12 +36,13 @@ return object end end private - def model_serializer(object) - @model_serializer ||= object.active_model_serializer - @model_serializer.new object + attr_reader :serializer_source + + def serializer(object) + @serializer ||= serializer_source.call object end def deserialize_association(association, values) Array.wrap(values).each do |value| deserialize(association.singularize => value)