Module: Representable::JSON
- Defined in:
- lib/representable/json.rb,
lib/representable/bindings/json_bindings.rb
Overview
Brings #to_xml, #to_hash, #from_xml and #from_hash to your object.
Note: The authorative methods are #to_hash and #from_hash, if you override #to_json instead, things might work as expected.
Defined Under Namespace
Modules: ClassMethods Classes: Binding, ObjectBinding, TextBinding
Constant Summary collapse
- BINDING_FOR_TYPE =
TODO: refactor #representable_accessor for better extendability.
{ # TODO: refactor #representable_accessor for better extendability. :text => TextBinding, }
Class Method Summary collapse
Instance Method Summary collapse
- #from_json(data, options = {}, &block) ⇒ Object
- #to_hash(options = {}) ⇒ Object
-
#to_json(options = {}) ⇒ Object
Returns a JSON string representing this object.
Class Method Details
.included(base) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/representable/json.rb', line 14 def self.included(base) base.class_eval do include Representable extend ClassMethods end end |
Instance Method Details
#from_json(data, options = {}, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/representable/json.rb', line 50 def from_json(data, ={}, &block) data = ::JSON[data] data = data[self.class.representation_name.to_s] unless [:wrap] == false data ||= {} # FIXME: should we fail here? generate a warning? update_properties_from(data, &block) end |
#to_hash(options = {}) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/representable/json.rb', line 58 def to_hash(={}) hash = create_representation_with({}) # DISCUSS: where to wrap? [:wrap] == false ? hash : {self.class.representation_name => hash} end |
#to_json(options = {}) ⇒ Object
Returns a JSON string representing this object.
66 67 68 |
# File 'lib/representable/json.rb', line 66 def to_json(={}) to_hash().to_json end |