Class: AutomationObject::BluePrint::HashAdapter::Composite
- Includes:
- ValidationHelper
- Defined in:
- lib/automation_object/blue_print/hash_adapter/composite.rb
Overview
Base composite for blue print hash adapter
Direct Known Subclasses
AutomaticModalChange, CustomMethod, ElementArray, ElementHash, HookAction, HookElementRequirements
Instance Attribute Summary collapse
-
#hash ⇒ Object
Returns the value of attribute hash.
Attributes included from ValidationHelper
Attributes inherited from Composite
#children, #location, #name, #parent
Instance Method Summary collapse
- #create_array_children(name, children, args) ⇒ Object
- #create_composite(args, child, name, location) ⇒ Object
- #create_hash_children(children, args) ⇒ Object
-
#get_child(name, options) ⇒ Object
Overriding base get_child method.
-
#get_children(name, options) ⇒ Object
Overriding base get_children method.
-
#initialize(hash = {}, name = :top, parent = nil, location = 'top') ⇒ Composite
constructor
A new instance of Composite.
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
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
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
#hash ⇒ Object
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
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
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
40 41 42 43 44 45 |
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 40 def get_child(name, ) child = hash[name].is_a?(Hash) ? hash[name] : {} child_location = location + "[#{name}]" create_composite(, child, name, child_location) end |
#get_children(name, options) ⇒ Object
Overriding base get_children method
51 52 53 54 55 56 |
# File 'lib/automation_object/blue_print/hash_adapter/composite.rb', line 51 def get_children(name, ) children = hash[name] children = children.is_a?(Hash) ? children : {} create_hash_children(children, ) end |