Class: Ixtlan::Babel::Deserializer
- Inherits:
-
Object
- Object
- Ixtlan::Babel::Deserializer
- Defined in:
- lib/ixtlan/babel/deserializer.rb
Instance Method Summary collapse
- #from_hash(data, options = nil) ⇒ Object
- #from_json(json, options = nil) ⇒ Object
- #from_yaml(yaml, options = nil) ⇒ Object
-
#initialize(model_class) ⇒ Deserializer
constructor
A new instance of Deserializer.
- #use(context_or_options) ⇒ Object
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, = nil) filter. = || {} filter.[:root] ||= self.class.config.root if data.is_a? Array from_array_hash( data ) else data = data[ filter.[:root] ] if filter.[: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, = nil) data = MultiJson.load(json) from_hash(data, ) end |
#from_yaml(yaml, options = nil) ⇒ Object
82 83 84 85 |
# File 'lib/ixtlan/babel/deserializer.rb', line 82 def from_yaml(yaml, = nil) data = YAML.load_stream(StringIO.new(yaml)).documents from_hash(data, ) end |
#use(context_or_options) ⇒ Object
52 53 54 55 |
# File 'lib/ixtlan/babel/deserializer.rb', line 52 def use() @context_or_options = self end |