Class: Representors::RepresentorBuilder

Inherits:
Object
  • Object
show all
Includes:
RepresentorSupport::Utilities
Defined in:
lib/representors/representor_builder.rb

Overview

Builder has methods to abstract the construction of Representor objects In the present implementation it will create a hash of a specific format to Initialize the Representor with, this will create classess with it.

Constant Summary collapse

HREF_KEY =
:href
DATA_KEY =
:data

Instance Method Summary collapse

Methods included from RepresentorSupport::Utilities

#deep_dup, #dup_or_self, #map_or_apply, #symbolize_keys

Constructor Details

#initialize(representor_hash = {}) ⇒ RepresentorBuilder

Returns a new instance of RepresentorBuilder.



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

def initialize(representor_hash = {})
  @representor_hash = RepresentorHash.new(representor_hash)
end

Instance Method Details

#add_attribute(name, value, options = {}) ⇒ Object

Adds an attribute to the Representor. We are creating a hash where the keys are the names of the attributes



27
28
29
30
31
# File 'lib/representors/representor_builder.rb', line 27

def add_attribute(name, value, options={})
  new_representor_hash = RepresentorHash.new(deep_dup(@representor_hash.to_h))
  new_representor_hash.attributes[name] = options.merge({value: value})
  RepresentorBuilder.new(new_representor_hash)
end

#add_embedded(name, embedded_resource) ⇒ Object



58
59
60
61
62
# File 'lib/representors/representor_builder.rb', line 58

def add_embedded(name, embedded_resource)
  new_representor_hash = RepresentorHash.new(deep_dup(@representor_hash.to_h))
  new_representor_hash.embedded[name] = embedded_resource
  RepresentorBuilder.new(new_representor_hash)
end

#add_transition(rel, href, options = {}) ⇒ Object

Adds a transition to the Representor, each transition is a hash of values The transition collection is an Array



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/representors/representor_builder.rb', line 35

def add_transition(rel, href, options={})
  new_representor_hash = RepresentorHash.new(deep_dup(@representor_hash.to_h))
  options = symbolize_keys(options)
  options.delete(:method) if options[:method] == Transition::DEFAULT_METHOD
  link_values = options.merge({href: href, rel: rel})

  if options[DATA_KEY]
    link_values[Transition::DESCRIPTORS_KEY] = link_values.delete(DATA_KEY)
  end

  new_representor_hash.transitions.push(link_values)
  RepresentorBuilder.new(new_representor_hash)
end

#add_transition_array(rel, array_of_hashes) ⇒ Object

Adds directly an array to our array of transitions



50
51
52
53
54
55
56
# File 'lib/representors/representor_builder.rb', line 50

def add_transition_array(rel, array_of_hashes)
  array_of_hashes.reduce(RepresentorBuilder.new(@representor_hash)) do |memo, transition|
    transition = symbolize_keys(transition)
    href = transition.delete(:href)
    memo = memo.add_transition(rel, href, transition)
  end
end

#to_representor_hashObject

Returns a hash usable by the representor class



21
22
23
# File 'lib/representors/representor_builder.rb', line 21

def to_representor_hash
  @representor_hash
end