class Conglomerate::TreeDeserializer

Constants

PARTICLES

Attributes

object[RW]

Public Class Methods

new(object) click to toggle source
# File lib/conglomerate/tree_deserializer.rb, line 7
def initialize(object)
  self.object = object
end

Public Instance Methods

deserialize(item = object) click to toggle source
# File lib/conglomerate/tree_deserializer.rb, line 11
def deserialize(item = object)
  raise "ObjectNotCollectionRoot" if !object["collection"]
  deserialize_particle(object["collection"], Collection)
end

Private Instance Methods

deserialize_array(items, contains) click to toggle source
# File lib/conglomerate/tree_deserializer.rb, line 45
def deserialize_array(items, contains)
  array = Conglomerate::Array.new(contains)

  items.each do |item|
    array << deserialize_particle(item, contains)
  end

  array
end
deserialize_attribute(attribute, attr_metadata) click to toggle source
# File lib/conglomerate/tree_deserializer.rb, line 34
def deserialize_attribute(attribute, attr_metadata)
  if PARTICLES.include?(attr_metadata[:type])
    deserialize_particle(attribute, attr_metadata[:type])
  elsif attr_metadata[:type] == :array
    raise "ArrayExpected" unless attribute.is_a?(::Array)
    deserialize_array(attribute, attr_metadata[:contains])
  else
    attribute
  end
end
deserialize_particle(object, klass) click to toggle source
# File lib/conglomerate/tree_deserializer.rb, line 20
def deserialize_particle(object, klass)
  attributes = {}
  attribute_info = klass.attributes

  attribute_info.each do |attr, attr_metadata|
    attr = attr.to_s
    if object[attr]
      attributes[attr] = deserialize_attribute(object[attr], attr_metadata)
    end
  end

  klass.new(attributes)
end