class Strategy::Plan

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.

Attributes

description[R]
steps[R]

Public Class Methods

new(description) click to toggle source

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.

# File lib/strategy/plan.rb, line 17
def initialize description
  @description = description
  @steps = []
end

Public Instance Methods

add(step) click to toggle source

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

# 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
describe() click to toggle source

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.

# 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!() { |n, description| ... } click to toggle source

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.

# 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