Class: Runbook::Entity

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

Instance Attribute Summary collapse

Attributes inherited from Node

#parent

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?, #parent_entity, #visited!, #visited?

Constructor Details

#initialize(title, tags: [], labels: {}, parent: nil) ⇒ Entity

Returns a new instance of Entity.



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

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

Instance Attribute Details

#dslObject (readonly)

Returns the value of attribute dsl.



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

def dsl
  @dsl
end

#labelsObject (readonly)

Returns the value of attribute labels.



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

def labels
  @labels
end

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
end

#titleObject (readonly)

Returns the value of attribute title.



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

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



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

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



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

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)


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

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

#_should_reverse?(run, metadata) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#add(item) ⇒ Object



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

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

#dynamic!Object



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

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

#itemsObject



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

def items
  @items ||= []
end

#render(view, output, metadata) ⇒ Object



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

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)


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

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

#run(run, metadata) ⇒ Object



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

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