Class: AutomationObject::Dsl::Base

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/automation_object/dsl/_base.rb

Overview

Base DSL Object

Direct Known Subclasses

Element, ElementArray, ElementHash, Modal, Screen, Top

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blue_prints, state) ⇒ Base

Returns a new instance of Base

Parameters:



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/automation_object/dsl/_base.rb', line 9

def initialize(blue_prints, state)
  ostruct_hash = {}

  # Add attributes the call super
  self.class.has_many_relationships.each do |name, composite_class|
    blue_prints.send(name).each do |child_key, child_blue_prints|
      child_state = state.send(name)[child_key]
      ostruct_hash[child_key] = composite_class.new(child_blue_prints, child_state, child_key)
    end
  end

  super ostruct_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Parameters:

  • method (Symbol)
  • args (Array, nil)
  • block (Proc)

Raises:

  • (NoMethodError)


26
27
28
29
30
# File 'lib/automation_object/dsl/_base.rb', line 26

def method_missing(method, *args, &block)
  return super if to_h.key?(method)

  raise NoMethodError.new("undefined method '#{method}'", method, args)
end

Class Method Details

.has_many(children_name, composite_class) ⇒ Object

Has many children relationship for the composite

Parameters:

  • children_name (Symbol)

    name of the children, should be a BluePrint method

  • composite_class (Class)

    interface



72
73
74
# File 'lib/automation_object/dsl/_base.rb', line 72

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

.has_many_relationshipsHash

Returns relationships for the composite

Returns:

  • (Hash)

    relationships for the composite



77
78
79
# File 'lib/automation_object/dsl/_base.rb', line 77

def has_many_relationships
  @has_many_relationships ||= {}
end

Instance Method Details

#formatted_name(key, indent) ⇒ String

Parameters:

  • key (String)
  • indent (Integer)

Returns:



52
53
54
55
# File 'lib/automation_object/dsl/_base.rb', line 52

def formatted_name(key, indent)
  color = self[key].active? ? :green : :blue
  "\n#{' ' * indent} #{key}:".colorize(color)
end

#inspect(_indent = 5) ⇒ String

Parameters:

  • _indent (Integer) (defaults to: 5)

Returns:



38
39
40
41
42
43
44
45
46
47
# File 'lib/automation_object/dsl/_base.rb', line 38

def inspect(_indent = 5)
  string = self.class.to_s

  to_h.each do |key, value|
    string += formatted_name(key, _indent)
    string += sub_inspect(value, _indent)
  end

  string
end

#sub_inspect(value, indent) ⇒ String

Parameters:

  • value (Object)
  • indent (Integer)

Returns:



60
61
62
63
64
65
66
# File 'lib/automation_object/dsl/_base.rb', line 60

def sub_inspect(value, indent)
  if value.is_a?(Base)
    " #{value.inspect(indent + 10)}"
  else
    " #{value.inspect}"
  end
end

#to_sObject



32
33
34
# File 'lib/automation_object/dsl/_base.rb', line 32

def to_s
  inspect
end