Sha256: 9d003aa859f6604ffb8cf5e7ff239ae6ba277e52e7437e113588d4041c167a6b
Contents?: true
Size: 1.58 KB
Versions: 3
Compression:
Stored size: 1.58 KB
Contents
require 'representable' require 'representable/hash/binding' module Representable # The generic representer. Brings #to_hash and #from_hash to your object. # If you plan to write your own representer for a new media type, try to use this module (e.g., check how JSON reuses Hash's internal # architecture). module Hash def self.included(base) base.class_eval do include Representable # either in Hero or HeroRepresentation. extend ClassMethods register_feature Representable::Hash end end module ClassMethods def format_engine Representable::Hash end def collection_representer_class Collection end end def from_hash(data, options={}, binding_builder=Binding) data = filter_wrap(data, options) update_properties_from(data, options, binding_builder) end def to_hash(options={}, binding_builder=Binding) hash = create_representation_with({}, options, binding_builder) return hash if options[:wrap] == false return hash unless wrap = options[:wrap] || representation_wrap(options) {wrap => hash} end alias_method :render, :to_hash alias_method :parse, :from_hash private def filter_wrap(data, options) return data if options[:wrap] == false return data unless wrap = options[:wrap] || representation_wrap(options) filter_wrap_for(data, wrap) end def filter_wrap_for(data, wrap) data[wrap.to_s] || {} # DISCUSS: don't initialize this more than once. # TODO: this should be done with #read. end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
representable-3.0.4 | lib/representable/hash.rb |
representable-3.0.3 | lib/representable/hash.rb |
representable-3.0.2 | lib/representable/hash.rb |