Class: Ixtlan::Babel::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/babel/deserializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Deserializer

Returns a new instance of Deserializer.



26
27
28
# File 'lib/ixtlan/babel/deserializer.rb', line 26

def initialize(model_class)
  @model_class = model_class
end

Instance Method Details

#from_hash(data, options = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ixtlan/babel/deserializer.rb', line 66

def from_hash(data, options = nil)
  filter.options = options || {}
  filter.options[:root] ||= self.class.config.root
  if data.is_a? Array
    from_array_hash( data )
  else
    data = data[ filter.options[:root] ] if filter.options[:root]
    @model_class.new( filter.filter( data ) )
  end
end

#from_json(json, options = nil) ⇒ Object



77
78
79
80
# File 'lib/ixtlan/babel/deserializer.rb', line 77

def from_json(json, options = nil)
  data = MultiJson.load(json)
  from_hash(data, options)
end

#from_yaml(yaml, options = nil) ⇒ Object



82
83
84
85
# File 'lib/ixtlan/babel/deserializer.rb', line 82

def from_yaml(yaml, options = nil)
  data = YAML.load_stream(StringIO.new(yaml)).documents
  from_hash(data, options)
end

#use(context_or_options) ⇒ Object



52
53
54
55
# File 'lib/ixtlan/babel/deserializer.rb', line 52

def use(context_or_options)
  @context_or_options = context_or_options
  self
end