Class: AutomationObject::Composite

Inherits:
Object
  • Object
show all
Includes:
CompositeHook, Reflection
Defined in:
lib/automation_object/helpers/composite.rb

Overview

Composite is a super class that helps build composite objects based of a Hash Composite classes should inherit from this class and use the class-level methods to add the components

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Reflection

#add_alias, #add_attribute

Methods included from CompositeHook

#after_create_run, #before_create_run, included

Constructor Details

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

Returns a new instance of Composite

Parameters:

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

    name of the object

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

    parent composite object

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

    string location for error/debugging purposes



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/automation_object/helpers/composite.rb', line 18

def initialize(name = :top, parent = nil, location = 'top')
  self.name = name
  self.parent = parent
  self.location = location

  before_create_run

  add_has_one_relationships
  add_has_many_relationships

  after_create_run
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children



13
14
15
# File 'lib/automation_object/helpers/composite.rb', line 13

def children
  @children
end

#locationObject

Returns the value of attribute location



13
14
15
# File 'lib/automation_object/helpers/composite.rb', line 13

def location
  @location
end

#nameObject

Returns the value of attribute name



13
14
15
# File 'lib/automation_object/helpers/composite.rb', line 13

def name
  @name
end

#parentObject

Returns the value of attribute parent



13
14
15
# File 'lib/automation_object/helpers/composite.rb', line 13

def parent
  @parent
end

Class Method Details

.has_many(children_name, args) ⇒ Object

Has many children relationship for the composite

Parameters:

  • children_name (Symbol)

    name of the children, should be a BluePrint method

  • args (Hash)

    additional arguments, expects interface



79
80
81
# File 'lib/automation_object/helpers/composite.rb', line 79

def has_many(children_name, args)
  has_many_relationships[children_name] = args
end

.has_many_relationshipsHash

Returns relationships for the composite

Returns:

  • (Hash)

    relationships for the composite



84
85
86
# File 'lib/automation_object/helpers/composite.rb', line 84

def has_many_relationships
  @has_many_relationships ||= {}
end

.has_one(child_name, args) ⇒ Object

Parameters:

  • child_name (Symbol)

    name of key

  • args (Hash)

    arguments



90
91
92
# File 'lib/automation_object/helpers/composite.rb', line 90

def has_one(child_name, args)
  has_one_relationships[child_name] = args
end

.has_one_relationshipsHash

Returns hash of relationships

Returns:

  • (Hash)

    hash of relationships



95
96
97
# File 'lib/automation_object/helpers/composite.rb', line 95

def has_one_relationships
  @has_one_relationships ||= {}
end

Instance Method Details

#add_has_many_relationshipsObject



67
68
69
70
71
72
73
# File 'lib/automation_object/helpers/composite.rb', line 67

def add_has_many_relationships
  self.class.has_many_relationships.each do |name, options|
    composite_children = get_children(name, options)
    children[name] = composite_children
    add_attribute(name, children[name])
  end
end

#add_has_one_relationshipsObject



60
61
62
63
64
65
# File 'lib/automation_object/helpers/composite.rb', line 60

def add_has_one_relationships
  self.class.has_one_relationships.each do |name, options|
    children[name] = get_child(name, options)
    add_attribute(name, children[name])
  end
end

#get_child(_name, _options) ⇒ Object

Abstract argument, override

Parameters:

  • _name (Symbol)

    name of child

  • _options (Hash)

    options for child



48
49
50
# File 'lib/automation_object/helpers/composite.rb', line 48

def get_child(_name, _options)
  raise 'Abstract method'
end

#get_children(_name, _options) ⇒ Object

Abstract argument, override

Parameters:

  • _name (Symbol)

    name of child

  • _options (Hash)

    options for child



56
57
58
# File 'lib/automation_object/helpers/composite.rb', line 56

def get_children(_name, _options)
  raise 'Abstract method'
end

#topAutomationObject::Composite

Get top composite Object



39
40
41
42
# File 'lib/automation_object/helpers/composite.rb', line 39

def top
  # Should recursively call top until parent is nil
  parent.nil? ? self : parent.top
end