Class: Representors::RepresentorHash
- Inherits:
-
Struct
- Object
- Struct
- Representors::RepresentorHash
- Includes:
- RepresentorSupport::Utilities
- Defined in:
- lib/representors/representor_hash.rb
Overview
This is the structure shared between the builder and the representor. This class allows to pass all the data to the representor without polluting it with methods It is supposed to be a fast class (Struct is implemented in C) The structure looks like this: id: [string] doc: [string] href: [string] protocol: [string] attributes: [hash] { key => value } links: [array of hashes] transitions: [array of hashes] embedded: [hash] where each value can be recursively defined by this same structure
Constant Summary collapse
- DEFAULT_HASH_VALUES =
{ id: nil, doc: nil, href: nil, #TODO protocol doesnt belong in representors, remove it protocol: nil, #TODO fix this, make it the same interface as transitions links: {}, attributes: {}, transitions: [], embedded: {} }.each_value(&:freeze).freeze
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#doc ⇒ Object
Returns the value of attribute doc.
-
#embedded ⇒ Object
Returns the value of attribute embedded.
-
#href ⇒ Object
Returns the value of attribute href.
-
#id ⇒ Object
Returns the value of attribute id.
-
#links ⇒ Object
Returns the value of attribute links.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#transitions ⇒ Object
Returns the value of attribute transitions.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(hash = {}) ⇒ RepresentorHash
constructor
be able to create from a hash.
-
#merge(hash) ⇒ Object
Be able to generate a new structure with myself and a hash.
-
#to_h ⇒ Object
to_h does not exists in Ruby < 2.0.
Methods included from RepresentorSupport::Utilities
#deep_dup, #dup_or_self, #map_or_apply, #symbolize_keys
Constructor Details
#initialize(hash = {}) ⇒ RepresentorHash
be able to create from a hash
34 35 36 |
# File 'lib/representors/representor_hash.rb', line 34 def initialize(hash = {}) DEFAULT_HASH_VALUES.each { |k, v| self[k] = dup_or_self(hash[k] || v) } end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def attributes @attributes end |
#doc ⇒ Object
Returns the value of attribute doc
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def doc @doc end |
#embedded ⇒ Object
Returns the value of attribute embedded
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def @embedded end |
#href ⇒ Object
Returns the value of attribute href
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def href @href end |
#id ⇒ Object
Returns the value of attribute id
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def id @id end |
#links ⇒ Object
Returns the value of attribute links
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def links @links end |
#protocol ⇒ Object
Returns the value of attribute protocol
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def protocol @protocol end |
#transitions ⇒ Object
Returns the value of attribute transitions
16 17 18 |
# File 'lib/representors/representor_hash.rb', line 16 def transitions @transitions end |
Instance Method Details
#==(other) ⇒ Object
54 55 56 |
# File 'lib/representors/representor_hash.rb', line 54 def ==(other) members.all? { |k| self[k] == other[k] } end |
#empty? ⇒ Boolean
50 51 52 |
# File 'lib/representors/representor_hash.rb', line 50 def empty? members.all? { |k| self[k].nil? || self[k].empty? } end |
#merge(hash) ⇒ Object
Be able to generate a new structure with myself and a hash
39 40 41 42 43 |
# File 'lib/representors/representor_hash.rb', line 39 def merge(hash) new_representor_hash = RepresentorHash.new(to_h) hash.each_pair { |k, v| new_representor_hash[k] = v } new_representor_hash end |
#to_h ⇒ Object
to_h does not exists in Ruby < 2.0
46 47 48 |
# File 'lib/representors/representor_hash.rb', line 46 def to_h members.each_with_object({}) { |member, hash| hash[member] = self[member] } end |