Class: AutomationObject::BluePrint::HashAdapter::Composite

Inherits:
Composite
  • Object
show all
Includes:
ValidationHelper
Defined in:
lib/automation_object/blue_print/hash_adapter/composite.rb

Overview

Base composite for blue print hash adapter

Instance Attribute Summary collapse

Attributes included from ValidationHelper

#errors

Attributes inherited from Composite

#children, #location, #name, #parent

Instance Method Summary collapse

Methods included from ValidationHelper

#add_errors, included, #valid?

Methods inherited from Composite

#add_has_many_relationships, #add_has_one_relationships, has_many, has_many_relationships, has_one, has_one_relationships, #top

Methods included from Reflection

#add_alias, #add_attribute

Methods included from CompositeHook

#after_create_run, #before_create_run, included

Constructor Details

#initialize(hash = {}, name = :top, parent = nil, location = 'top') ⇒ Composite

Returns a new instance of Composite

Parameters:

  • hash (Hash) (defaults to: {})

    hash for the composite to build of off

  • name (Symbol) (defaults to: :top)

    name of composite element

  • parent (Object, nil) (defaults to: nil)

    parent composite object

  • location (String) (defaults to: 'top')

    string location for error/debugging purposes

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 22

def initialize(hash = {}, name = :top, parent = nil, location = 'top')
  # Add hash before calling super
  self.hash = hash.is_a?(Hash) ? hash : {}
  self.hash.symbolize_keys_deep!

  super(name, parent, location)

  # Validate using ValidationHelper
  return if valid?

  raise ValidationError, errors.uniq.reverse unless self.parent

  self.parent.add_errors(errors)
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash



16
17
18
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 16

def hash
  @hash
end

Instance Method Details

#create_array_children(name, children, args) ⇒ Object

Parameters:

  • name (Symbol)

    name of child

  • children (Array)

    hash of children

  • args (Hash)

    arguments for adding children



61
62
63
64
65
66
67
68
69
70
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 61

def create_array_children(name, children, args)
  composite_children = children.map.with_index do |child, index|
    location = args[:location] ? args[:location] : self.location
    child_location = location + "[#{index}]"

    create_composite(args, child, "#{name}[#{index}]", child_location)
  end

  composite_children
end

#create_composite(args, child, name, location) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 85

def create_composite(args, child, name, location)
  # Get the Composite Class that corresponds with the HashAdapter Class
  class_name = args[:interface].name.split('::').last

  require_relative "../composite/#{class_name.to_underscore}"
  composite_class = AutomationObject::BluePrint::Composite.const_get(class_name)

  composite_class.new(args[:interface].new(child, name, self, location))
end

#create_hash_children(children, args) ⇒ Object

Parameters:

  • children (Hash)

    hash of children

  • args (Hash)

    arguments for adding children



74
75
76
77
78
79
80
81
82
83
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 74

def create_hash_children(children, args)
  composite_children = children.each_with_object({}) do |(key, value), hash|
    child_location = location + "[#{key}]"

    hash[key] = create_composite(args, value, key, child_location)
    hash
  end

  composite_children
end

#get_child(name, options) ⇒ Object

Overriding base get_child method

Parameters:

  • name (Symbol)

    name of child

  • options (Hash)

    options for child



40
41
42
43
44
45
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 40

def get_child(name, options)
  child = hash[name].is_a?(Hash) ? hash[name] : {}
  child_location = location + "[#{name}]"

  create_composite(options, child, name, child_location)
end

#get_children(name, options) ⇒ Object

Overriding base get_children method

Parameters:

  • name (Symbol)

    name of child

  • options (Hash)

    options for child



51
52
53
54
55
56
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 51

def get_children(name, options)
  children = hash[name]
  children = children.is_a?(Hash) ? children : {}

  create_hash_children(children, options)
end