Class: Representable::JSON::JSONBinding

Inherits:
Binding
  • Object
show all
Defined in:
lib/representable/bindings/json_bindings.rb

Direct Known Subclasses

CollectionBinding, HashBinding, PropertyBinding

Instance Attribute Summary

Attributes inherited from Binding

#definition

Instance Method Summary collapse

Methods included from Binding::Hooks

#deserialize, #serialize

Constructor Details

#initialize(definition) ⇒ JSONBinding

FIXME. make generic.



19
20
21
22
# File 'lib/representable/bindings/json_bindings.rb', line 19

def initialize(definition) # FIXME. make generic.
  super
  extend ObjectBinding if definition.typed?
end

Instance Method Details

#read(hash) ⇒ Object



24
25
26
27
28
29
# File 'lib/representable/bindings/json_bindings.rb', line 24

def read(hash)
  return FragmentNotFound unless hash.has_key?(definition.from) # DISCUSS: put it all in #read for performance. not really sure if i like returning that special thing.
  
  fragment = hash[definition.from]
  deserialize_from(fragment)
end

#write(hash, value) ⇒ Object



31
32
33
# File 'lib/representable/bindings/json_bindings.rb', line 31

def write(hash, value)
  hash[definition.from] = serialize_for(value)
end