Class: Representors::RepresentorHash

Inherits:
Struct
  • Object
show all
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

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes

Returns:

  • (Object)

    the current value of attributes



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def attributes
  @attributes
end

#docObject

Returns the value of attribute doc

Returns:

  • (Object)

    the current value of doc



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def doc
  @doc
end

#embeddedObject

Returns the value of attribute embedded

Returns:

  • (Object)

    the current value of embedded



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def embedded
  @embedded
end

#hrefObject

Returns the value of attribute href

Returns:

  • (Object)

    the current value of href



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def href
  @href
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def id
  @id
end

Returns the value of attribute links

Returns:

  • (Object)

    the current value of links



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def links
  @links
end

#protocolObject

Returns the value of attribute protocol

Returns:

  • (Object)

    the current value of protocol



16
17
18
# File 'lib/representors/representor_hash.rb', line 16

def protocol
  @protocol
end

#transitionsObject

Returns the value of attribute transitions

Returns:

  • (Object)

    the current value of 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

Returns:

  • (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_hObject

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