Module: Representable::Hash

Included in:
Collection, YAML
Defined in:
lib/representable/hash.rb,
lib/representable/hash/binding.rb,
lib/representable/hash/collection.rb,
lib/representable/hash/allow_symbols.rb

Overview

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

Defined Under Namespace

Modules: AllowSymbols, ClassMethods, Collection, Conversion Classes: Binding

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
# 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

Instance Method Details

#from_hash(data, options = {}, binding_builder = Binding) ⇒ Object Also known as: parse



30
31
32
33
34
# 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

#to_hash(options = {}, binding_builder = Binding) ⇒ Object Also known as: render



36
37
38
39
40
41
42
43
# 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