class Representable::Deserializer

Deserializer's job is deserializing the already parsed fragment into a scalar or an object. This object is then returned to the Populator.

It respects :deserialize, :prepare, :class, :instance

Collection bindings return an array of parsed fragment items (still native format, e.g. Nokogiri node, for nested objects).

Workflow

call -> instance/class -> prepare -> deserialize -> from_json.

Public Class Methods

new(binding) click to toggle source
# File lib/representable/deserializer.rb, line 12
def initialize(binding)
  @binding = binding
end

Public Instance Methods

call(fragment, *args) click to toggle source
# File lib/representable/deserializer.rb, line 16
def call(fragment, *args) # FIXME: args is always i.
  return fragment unless @binding.typed? # customize with :extend. this is not really straight-forward.
  return fragment if fragment.nil?

  # what if create_object is responsible for providing the deserialize-to object?
  object        = create_object(fragment, *args) # customize with :instance and :class.
  representable = prepare(object) # customize with :prepare and :extend.

  deserialize(representable, fragment, @binding.user_options) # deactivate-able via :representable => false.
end

Private Instance Methods

class_for(fragment, *args) click to toggle source
# File lib/representable/deserializer.rb, line 65
def class_for(fragment, *args)
  item_class = class_from(fragment, *args) or raise DeserializeError.new(":class did not return class constant.")
  item_class.new
end
class_from(fragment, *args) click to toggle source
# File lib/representable/deserializer.rb, line 70
def class_from(fragment, *args)
  @binding.evaluate_option(:class, fragment, *args)
end
create_object(fragment, *args) click to toggle source
# File lib/representable/deserializer.rb, line 61
def create_object(fragment, *args)
  instance_for(fragment, *args) or class_for(fragment, *args)
end
demarshal(object, fragment, options) click to toggle source
# File lib/representable/deserializer.rb, line 36
def demarshal(object, fragment, options)
  object.send(@binding.deserialize_method, fragment, options)
end
deserialize(object, fragment, options) click to toggle source
# File lib/representable/deserializer.rb, line 28
def deserialize(object, fragment, options) # TODO: merge with #serialize.
  return object unless @binding.representable?

  @binding.evaluate_option(:deserialize, object, fragment) do
    demarshal(object, fragment, options)
  end
end
instance_for(fragment, *args) click to toggle source
# File lib/representable/deserializer.rb, line 74
def instance_for(fragment, *args)
  # cool: if no :instance set, { return } will jump out of this method.
  @binding.evaluate_option(:instance, fragment, *args) { return } or raise DeserializeError.new(":instance did not return object.")
end