Class: Runbook::Entity

Inherits:
Node
  • Object
show all
Includes:
Hooks::Invoker
Defined in:
lib/runbook/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks::Invoker

#_execute_after_hooks, #_execute_around_hooks, #_execute_before_hooks, #invoke_with_hooks

Methods inherited from Node

#dynamic?, #visited!, #visited?

Constructor Details

#initialize(title, parent: nil) ⇒ Entity

Returns a new instance of Entity.



13
14
15
16
17
# File 'lib/runbook/entity.rb', line 13

def initialize(title, parent: nil)
  @title = title
  @parent = parent
  @dsl = "#{self.class}::DSL".constantize.new(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



28
29
30
31
32
33
34
# File 'lib/runbook/entity.rb', line 28

def method_missing(method, *args, &block)
  if dsl.respond_to?(method)
    dsl.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#dslObject (readonly)

Returns the value of attribute dsl.



11
12
13
# File 'lib/runbook/entity.rb', line 11

def dsl
  @dsl
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/runbook/entity.rb', line 10

def parent
  @parent
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/runbook/entity.rb', line 11

def title
  @title
end

Class Method Details

.inherited(child_class) ⇒ Object



6
7
8
# File 'lib/runbook/entity.rb', line 6

def self.inherited(child_class)
  child_class.const_set(:DSL, Runbook::DSL.class)
end

Instance Method Details

#_render_metadata(items, item, metadata, index) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/runbook/entity.rb', line 80

def (items, item, , index)
  index = items.select do |item|
    item.is_a?(Entity)
  end.index(item)

  .merge(
    {
      depth: [:depth] + 1,
      index: index,
    }
  )
end

#_run_metadata(items, item, metadata, index) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/runbook/entity.rb', line 93

def (items, item, , index)
  pos_index = items.select do |item|
    item.is_a?(Entity)
  end.index(item)

  if pos_index
    if [:position].empty?
      pos = "#{pos_index + 1}"
    else
      pos = "#{[:position]}.#{pos_index + 1}"
    end
  else
    pos = [:position]
  end

  .merge(
    {
      depth: [:depth] + 1,
      index: index,
      position: pos,
    }
  )
end

#_should_retraverse?(run, metadata) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
# File 'lib/runbook/entity.rb', line 122

def _should_retraverse?(run, )
  return false unless [:reverse]
  run.start_at_is_substep?(self, )
end

#_should_reverse?(run, metadata) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
# File 'lib/runbook/entity.rb', line 117

def _should_reverse?(run, )
  return false unless [:reverse]
  run.past_position?([:position], [:start_at])
end

#add(item) ⇒ Object



19
20
21
22
# File 'lib/runbook/entity.rb', line 19

def add(item)
  items << item
  item.parent = self
end

#dynamic!Object



75
76
77
78
# File 'lib/runbook/entity.rb', line 75

def dynamic!
  items.each(&:dynamic!)
  @dynamic = true
end

#itemsObject



24
25
26
# File 'lib/runbook/entity.rb', line 24

def items
  @items ||= []
end

#render(view, output, metadata) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/runbook/entity.rb', line 40

def render(view, output, )
  invoke_with_hooks(view, self, output, ) do
    view.render(self, output, )
    items.each_with_index do |item, index|
       = (items, item, , index)
      item.render(view, output, )
    end
  end
end

#respond_to?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/runbook/entity.rb', line 36

def respond_to?(name, include_private = false)
  !!(dsl.respond_to?(name) || super)
end

#run(run, metadata) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/runbook/entity.rb', line 50

def run(run, )
  return if _should_reverse?(run, )
  return if dynamic? && visited?

  invoke_with_hooks(run, self, ) do
    run.execute(self, )
    next if _should_reverse?(run, )
    loop do
      items.each_with_index do |item, index|
         = (items, item, , index)
        # Optimization
        break if _should_reverse?(run, )
        item.run(run, )
      end

      if _should_retraverse?(run, )
        [:reverse] = false
      else
        break
      end
    end
  end
  self.visited!
end