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, tags: [], labels: {}, parent: nil) ⇒ Entity

Returns a new instance of Entity.



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

def initialize(title, tags: [], labels: {}, parent: nil)
  @title = title
  @tags = tags
  @labels = labels
  @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



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

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

#labelsObject (readonly)

Returns the value of attribute labels.



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

def labels
  @labels
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
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



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

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



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

def (items, item, , index)
  pos_index = items.select do |item|
    item.is_a?(Entity) &&
      !item.is_a?(Runbook::Entities::Setup)
  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)


125
126
127
128
# File 'lib/runbook/entity.rb', line 125

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

#_should_reverse?(run, metadata) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/runbook/entity.rb', line 120

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

#add(item) ⇒ Object



21
22
23
24
# File 'lib/runbook/entity.rb', line 21

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

#dynamic!Object



77
78
79
80
# File 'lib/runbook/entity.rb', line 77

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

#itemsObject



26
27
28
# File 'lib/runbook/entity.rb', line 26

def items
  @items ||= []
end

#render(view, output, metadata) ⇒ Object



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

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)


38
39
40
# File 'lib/runbook/entity.rb', line 38

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

#run(run, metadata) ⇒ Object



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

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