Module: Representable::JSON::ClassMethods
- Defined in:
- lib/representable/json.rb
Instance Method Summary collapse
-
#from_json(data, options = {}) ⇒ Object
Creates a new Ruby object from XML using mapping information declared in the class.
Instance Method Details
#from_json(data, options = {}) ⇒ Object
Creates a new Ruby object from XML using mapping information declared in the class.
Example:
book = Book.from_xml("<book><name>Beyond Java</name></book>")
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/representable/json.rb', line 26 def from_json(data, ={}) # DISCUSS: extract #from_json call in Bindings to this place. data = ::JSON[data] if data.is_a?(String) # DISCUSS: #from_json sometimes receives a string (in nestings). data ||= {} data = data[representation_name.to_s] unless [:wrap] == false create_from_json.tap do |inst| refs = representable_attrs.map {|attr| JSON.binding_for_definition(attr) } refs.each do |ref| value = ref.value_in(data) inst.send(ref.definition.setter, value) end end end |