lib/representable/deserializer.rb in representable-1.7.7 vs lib/representable/deserializer.rb in representable-1.8.0

- old
+ new

@@ -1,57 +1,53 @@ module Representable - class CollectionDeserializer < Array # always is the targeted collection, already. + class CollectionDeserializer def initialize(binding) # TODO: get rid of binding dependency - # next step: use #get always. @binding = binding - collection = [] - # should be call to #default: - collection = binding.get if binding.sync? - - super collection end def deserialize(fragment) # next step: get rid of collect. - fragment.enum_for(:each_with_index).collect { |item_fragment, i| - @deserializer = ObjectDeserializer.new(@binding, lambda { self[i] }) + fragment.enum_for(:each_with_index).collect do |item_fragment, i| + @deserializer = ObjectDeserializer.new(@binding) - @deserializer.call(item_fragment) # FIXME: what if obj nil? - } + @deserializer.call(item_fragment, i) # FIXME: what if obj nil? + end end end class ObjectDeserializer - # dependencies: Def#options, Def#create_object, Def#get - def initialize(binding, object) + # dependencies: Def#options, Def#create_object + def initialize(binding) @binding = binding - @object = object end - def call(fragment) - # TODO: this used to be handled in #serialize where Object added it's behaviour. treat scalars as objects to remove this switch: - return fragment unless @binding.typed? + def call(fragment, *args) # FIXME: args is always i. + return fragment unless @binding.typed? # customize with :extend. this is not really straight-forward. - if @binding.sync? - # TODO: this is also done when instance: { nil } - @object = @object.call # call Binding#get or Binding#get[i] - else - @object = @binding.create_object(fragment) - end + # what if create_object is responsible for providing the deserialize-to object? + object = @binding.create_object(fragment, *args) # customize with :instance and :class. # DISCUSS: what parts should be in this class, what in Binding? - representable = prepare(@object) - deserialize(representable, fragment, @binding.user_options) - #yield @object + representable = prepare(object) # customize with :prepare and :extend. + + deserialize(representable, fragment, @binding.user_options) # deactivate-able via :representable => false. end private - def deserialize(object, fragment, options) + def deserialize(object, fragment, options) # TODO: merge with #serialize. + return object unless @binding.representable? + object.send(@binding.deserialize_method, fragment, options) end def prepare(object) + @binding.send(:evaluate_option, :prepare, object) do + prepare!(object) + end + end + + def prepare!(object) mod = @binding.representer_module_for(object) return object unless mod mod = mod.first if mod.is_a?(Array) # TODO: deprecate :extend => [..] \ No newline at end of file