module Chaintown::Chain

Public Class Methods

new(state, params = {}) click to toggle source
# File lib/chaintown/chain.rb, line 36
def initialize(state, params = {})
  @state, @params = state, params.freeze
end

Public Instance Methods

perform() click to toggle source
# File lib/chaintown/chain.rb, line 40
def perform
  perform_steps(steps, failed_steps)
  state
end

Protected Instance Methods

current_step_name() click to toggle source
# File lib/chaintown/chain.rb, line 47
def current_step_name
  current_step&.step_handler.to_s
end

Private Instance Methods

perform_step(step) click to toggle source
# File lib/chaintown/chain.rb, line 67
def perform_step(step)
  self.current_step = step # set step to use in callbacks
  run_before_actions
  with_around_actions do
    if step.steps.present?
      method(step.step_handler).call do
        perform_steps(step.steps, step.failed_steps)
        self.current_step = step # set proper step to use in callbacks after processing nested steps
      end
    else
      method(step.step_handler).call
    end
  end
  run_after_actions
end
perform_steps(steps, failed_steps) click to toggle source
# File lib/chaintown/chain.rb, line 53
def perform_steps(steps, failed_steps)
  steps.each do |step|
    break unless state.valid?
    next unless step.if_condition.blank? || self.instance_exec(&step.if_condition)
    perform_step(step)
  end
  unless state.valid?
    failed_steps.each do |step|
      next unless step.if_condition.blank? || self.instance_exec(&step.if_condition)
      perform_step(step)
    end
  end
end