module Representable::Hash

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).

Public Class Methods

included(base) click to toggle source
# File lib/representable/hash.rb, line 12
def self.included(base)
  base.class_eval do
    include Representable # either in Hero or HeroRepresentation.
    extend ClassMethods
    register_feature Representable::Hash
  end
end

Public Instance Methods

from_hash(data, options={}, binding_builder=Binding) click to toggle source
# File lib/representable/hash.rb, line 30
def from_hash(data, options={}, binding_builder=Binding)
  data = filter_wrap(data, options)

  update_properties_from(data, options, binding_builder)
end
Also aliased as: parse
parse(data, options={}, binding_builder=Binding)
Alias for: from_hash
render(options={}, binding_builder=Binding)
Alias for: to_hash
to_hash(options={}, binding_builder=Binding) click to toggle source
# File lib/representable/hash.rb, line 36
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
Also aliased as: render

Private Instance Methods

filter_wrap(data, options) click to toggle source
# File lib/representable/hash.rb, line 49
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
filter_wrap_for(data, wrap) click to toggle source
# File lib/representable/hash.rb, line 56
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