class StepMachine::Runner
Attributes
continue[RW]
failed_step[R]
first_step[RW]
next_step[RW]
repeat_what[RW]
status[R]
times_to_repeat[RW]
Public Class Methods
new()
click to toggle source
# File lib/step_machine/runner.rb, line 7 def initialize @steps = [] @groups = [] @failure_treatments = [] @before_each_step = [] @after_each_step = [] @times_to_repeat = -1 end
Public Instance Methods
after_each_step(options = {}, &block)
click to toggle source
# File lib/step_machine/runner.rb, line 41 def after_each_step(options = {}, &block) @after_each_step << options.merge(:block => block) end
before_each_step(options = {}, &block)
click to toggle source
# File lib/step_machine/runner.rb, line 37 def before_each_step(options = {}, &block) @before_each_step << options.merge(:block => block) end
first_step=(step)
click to toggle source
# File lib/step_machine/runner.rb, line 45 def first_step=(step) @next_step = @first_step = step end
group(name) { || ... }
click to toggle source
# File lib/step_machine/runner.rb, line 25 def group(name) return nil if name.nil? @current_group = group = @groups.detect {|g| g.name == name} || create_group(name) yield if block_given? @current_group = nil group end
on_step_failure(options = {}, &block)
click to toggle source
# File lib/step_machine/runner.rb, line 33 def on_step_failure(options = {}, &block) @failure_treatments << FailureTreatment.new(self, block, options) end
run(options = {})
click to toggle source
# File lib/step_machine/runner.rb, line 49 def run(options = {}) if group = group(options[:group]) assign_group_first_step(group) return if @next_step.group != group end assign_from_step(options[:from]) unless options[:from].nil? @continue = nil step = @next_step @status ||= :success execute_before_each_step(step) unless step.perform @failed_step = step return repeat if repeat? execute_step_failures(step) return run if @continue @status = :failure return end execute_after_each_step(step) return if step.name == options[:upto] run(options) if @next_step = step.next end
step(name, &block)
click to toggle source
# File lib/step_machine/runner.rb, line 16 def step(name, &block) step = get_step(name) || create_step(name) step.block = block if block @first_step ||= step @next_step ||= @first_step step end
Private Instance Methods
assign_from_step(step)
click to toggle source
# File lib/step_machine/runner.rb, line 84 def assign_from_step(step) if !@from_first_step @next_step = get_step(step) @from_first_step = true end end
assign_group_first_step(group)
click to toggle source
# File lib/step_machine/runner.rb, line 91 def assign_group_first_step(group) if !@group_first_step @next_step = group.first_step @group_first_step = true end end
create_group(name)
click to toggle source
# File lib/step_machine/runner.rb, line 133 def create_group(name) group = Group.new(name) @groups << group group end
create_step(name)
click to toggle source
# File lib/step_machine/runner.rb, line 124 def create_step(name) step = Step.new(name) step.group = @current_group @current_group.first_step ||= step if @current_group @steps << step @steps[-2].next_step = step if @steps.length > 1 step end
execute_after_each_step(step)
click to toggle source
# File lib/step_machine/runner.rb, line 106 def execute_after_each_step(step) @after_each_step.each do |after| next if after.has_key?(:only) && !after[:only].include?(step.name) next if after.has_key?(:except) && after[:except].include?(step.name) after[:block].call(step) end end
execute_before_each_step(step)
click to toggle source
# File lib/step_machine/runner.rb, line 98 def execute_before_each_step(step) @before_each_step.each do |before| next if before.has_key?(:only) && !before[:only].include?(step.name) next if before.has_key?(:except) && before[:except].include?(step.name) before[:block].call(step) end end
execute_step_failures(step)
click to toggle source
# File lib/step_machine/runner.rb, line 114 def execute_step_failures(step) @failure_treatments.each do |failure_treatment| failure_treatment.treat(step) end end
get_step(name)
click to toggle source
# File lib/step_machine/runner.rb, line 120 def get_step(name) @steps.find { |step| step.name == name } end
repeat()
click to toggle source
# File lib/step_machine/runner.rb, line 143 def repeat @times_to_repeat -= 1 if @times_to_repeat == -1 @status = :failure return end @next_step = @repeat_what == :process ? @first_step : @failed_step return run end
repeat?()
click to toggle source
# File lib/step_machine/runner.rb, line 139 def repeat? @times_to_repeat >= 0 end