Class: AutomationObject::Dsl::Base
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- AutomationObject::Dsl::Base
- Defined in:
- lib/automation_object/dsl/_base.rb
Overview
Base DSL Object
Direct Known Subclasses
Class Method Summary collapse
-
.has_many(children_name, composite_class) ⇒ Object
Has many children relationship for the composite.
-
.has_many_relationships ⇒ Hash
Relationships for the composite.
Instance Method Summary collapse
- #formatted_name(key, indent) ⇒ String
-
#initialize(blue_prints, state) ⇒ Base
constructor
A new instance of Base.
- #inspect(_indent = 5) ⇒ String
- #method_missing(method, *args, &block) ⇒ Object
- #sub_inspect(value, indent) ⇒ String
- #to_s ⇒ Object
Constructor Details
#initialize(blue_prints, state) ⇒ Base
Returns a new instance of Base
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
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
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_relationships ⇒ Hash
Returns 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
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
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
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_s ⇒ Object
32 33 34 |
# File 'lib/automation_object/dsl/_base.rb', line 32 def to_s inspect end |