Sha256: 0186ec8d063c3178bfbe0230848260f9b6ba001f080c685ef541095974627695
Contents?: true
Size: 1.67 KB
Versions: 3
Compression:
Stored size: 1.67 KB
Contents
require 'representable' require 'representable/bindings/json_bindings' require 'json' module Representable # 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. module JSON def self.binding_for_definition(definition) return ObjectBinding.new(definition) if definition.typed? TextBinding.new(definition) end 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 module ClassMethods # Creates a new object from the passed JSON document. def from_json(*args, &block) create_represented(*args, &block).from_json(*args) end def from_hash(*args, &block) create_represented(*args, &block).from_hash(*args) end end # Parses the body as JSON and delegates to #from_hash. def from_json(data, *args) data = ::JSON[data] from_hash(data, *args) end def from_hash(data, options={}) if wrap = options[:wrap] || representation_wrap data = data[wrap.to_s] end update_properties_from(data, options, JSON) end def to_hash(options={}) hash = create_representation_with({}, options, JSON) return hash unless wrap = options[:wrap] || representation_wrap {wrap => hash} end # Returns a JSON string representing this object. def to_json(*args) to_hash(*args).to_json end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
representable-1.0.1 | lib/representable/json.rb |
representable-1.0.0 | lib/representable/json.rb |
representable-0.13.1 | lib/representable/json.rb |