Class: Strategy::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/strategy/plan.rb

Overview

Plan is a wrapper around a set of executable steps. It maintains a runbook of what to do and how to do it, and provides an easy way of displaying an execution plan or “strategy” before executing anything. A Plan object is the highest-level container around an execution strategy, and may contain as few or as many Step objects as required.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description) ⇒ Plan

Create a new plan, to which individual execution steps will be added.

Parameters:

description

The description of the execution plan. This is a short sentence which describes what the plan will do.



17
18
19
20
# File 'lib/strategy/plan.rb', line 17

def initialize description
  @description = description
  @steps = []
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/strategy/plan.rb', line 8

def description
  @description
end

#stepsObject (readonly)

Returns the value of attribute steps.



8
9
10
# File 'lib/strategy/plan.rb', line 8

def steps
  @steps
end

Instance Method Details

#add(step) ⇒ Object

Adds a step object to the execution plan. Each step carries its own set of actions, description, and so on.

Parameters:

step

A Strategy::Step object encapsulating some actions



29
30
31
32
33
34
# File 'lib/strategy/plan.rb', line 29

def add step
  if !step.kind_of? Step
    raise TypeError, "Expected Strategy::Step but got #{step.class}"
  end
  @steps << step
end

#describeObject

Describe the plan itself and all steps involved. This method puts together a textual representation of the execution plan which can be displayed to a user before executing anything for confirmation.



53
54
55
56
57
58
59
60
# File 'lib/strategy/plan.rb', line 53

def describe
  description = [@description]
  n = 0
  @steps.each do |step|
    description << "  #{n+=1}. #{step.description}"
  end
  description.join "\n"
end

#execute!Object

Iterate over all steps contained in the plan and execute them. This method will yield the step number and description if a block is given, which enables one to print out a step banner just before execution if desired.



39
40
41
42
43
44
45
46
47
48
# File 'lib/strategy/plan.rb', line 39

def execute!
  n = 0
  @steps.each do |step|
    n += 1
    if block_given?
      yield n, step.description
    end
    step.execute!
  end
end