Module: Representable::JSON
- Included in:
- Collection, Hash
- Defined in:
- lib/representable/json.rb,
lib/representable/json/hash.rb,
lib/representable/json/collection.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, Collection, Hash, ObjectBinding Classes: CollectionBinding, HashBinding, JSONBinding, PropertyBinding
Class Method Summary collapse
Instance Method Summary collapse
- #from_hash(data, options = {}) ⇒ Object
-
#from_json(data, *args) ⇒ Object
Parses the body as JSON and delegates to #from_hash.
- #to_hash(options = {}) ⇒ Object
-
#to_json(*args) ⇒ Object
Returns a JSON string representing this object.
Class Method Details
.binding_for_definition(definition) ⇒ Object
11 12 13 14 15 |
# File 'lib/representable/json.rb', line 11 def self.binding_for_definition(definition) return CollectionBinding.new(definition) if definition.array? return HashBinding.new(definition) if definition.hash? PropertyBinding.new(definition) end |
.included(base) ⇒ Object
17 18 19 20 21 22 |
# File 'lib/representable/json.rb', line 17 def self.included(base) base.class_eval do include Representable # either in Hero or HeroRepresentation. extend ClassMethods # DISCUSS: do that only for classes? end end |
Instance Method Details
#from_hash(data, options = {}) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/representable/json.rb', line 43 def from_hash(data, ={}) if wrap = [:wrap] || representation_wrap data = data[wrap.to_s] end update_properties_from(data, , JSON) end |
#from_json(data, *args) ⇒ Object
Parses the body as JSON and delegates to #from_hash.
38 39 40 41 |
# File 'lib/representable/json.rb', line 38 def from_json(data, *args) data = ::JSON[data] from_hash(data, *args) end |
#to_hash(options = {}) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/representable/json.rb', line 51 def to_hash(={}) hash = create_representation_with({}, , JSON) return hash unless wrap = [:wrap] || representation_wrap {wrap => hash} end |
#to_json(*args) ⇒ Object
Returns a JSON string representing this object.
60 61 62 |
# File 'lib/representable/json.rb', line 60 def to_json(*args) to_hash(*args).to_json end |