Sha256: e56b648d797dd6a226cfee596c4fd4594192c3d8807879eb3d21027d24e63428
Contents?: true
Size: 1.4 KB
Versions: 2
Compression:
Stored size: 1.4 KB
Contents
module Representable module JSON class Binding attr_reader :definition def initialize(definition) @definition = definition end def read(hash) value_from_hash(hash) or default end private def default "" end def collect_for(hash) nodes = hash[definition.from] or return nodes = [nodes] unless nodes.is_a?(Array) vals = nodes.collect { |node| yield node } definition.array? ? vals : vals.first end end # Represents plain key-value. class TextBinding < Binding def write(hash, value) hash[definition.from] = value end private def value_from_hash(hash) collect_for(hash) do |value| value end end end # Represents a tag with object binding. class ObjectBinding < Binding def write(hash, value) if definition.array? hash.merge! ({definition.from => value.collect {|v| v.to_hash(:wrap => false)}}) # hier name=> wech. else hash.merge! value.to_hash end end private def default [] end def value_from_hash(hash) collect_for(hash) do |node| definition.sought_type.from_hash(node) # call #from_hash as it's already deserialized. end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
representable-0.9.2 | lib/representable/bindings/json_bindings.rb |
representable-0.9.1 | lib/representable/bindings/json_bindings.rb |