lib/representable/binding.rb in representable-1.3.5 vs lib/representable/binding.rb in representable-1.4.0
- old
+ new
@@ -91,28 +91,27 @@
return unless options[option_name]
represented.instance_exec(*args+[user_options], &options[option_name])
end
- # Hooks into #serialize and #deserialize to extend typed properties
+ # Hooks into #serialize and #deserialize to setup (extend/decorate) typed properties
# at runtime.
- module Extend
+ module Prepare
# Extends the object with its representer before serialization.
def serialize(*)
- extend_for(super)
+ prepare(super)
end
def deserialize(*)
- extend_for(super)
+ prepare(super)
end
- def extend_for(object)
- if mod = representer_module_for(object) # :extend.
- object.extend(*mod)
- end
+ def prepare(object)
+ return object unless mod = representer_module_for(object) # :extend.
- object
+ mod = mod.first if mod.is_a?(Array) # TODO: deprecate :extend => [..]
+ mod.prepare(object)
end
private
def representer_module_for(object, *args)
call_proc_for(representer_module, object) # TODO: how to pass additional data to the computing block?`
@@ -123,21 +122,25 @@
# DISCUSS: use represented_exec_for here?
@represented.instance_exec(*args, &proc)
end
end
+ # Overrides #serialize/#deserialize to call #to_*/from_*.
+ # Computes :class in #deserialize. # TODO: shouldn't this be in a separate module? ObjectSerialize/ObjectDeserialize?
module Object
- include Binding::Extend # provides #serialize/#deserialize with extend.
+ include Binding::Prepare
def serialize(object)
return object if object.nil?
super.send(serialize_method, @user_options.merge!({:wrap => false})) # TODO: pass :binding => self
end
def deserialize(data)
# DISCUSS: does it make sense to skip deserialization of nil-values here?
- super(create_object(data)).send(deserialize_method, data, @user_options)
+ create_object(data).tap do |obj|
+ super(obj).send(deserialize_method, data, @user_options)
+ end
end
def create_object(fragment)
instance_for(fragment) or class_for(fragment)
end