Sha256: 9d4f6e41140a98219f6b0907164048ec43038cdecaca115755961e1d8fe7aeba

Contents?: true

Size: 1.41 KB

Versions: 7

Compression:

Stored size: 1.41 KB

Contents

module BusinessFlow
  # Default behavior for running a step queue -- execute each step in turn
  # halting the moment something goes wrong. Use the same flow as input
  # and output to all steps.
  class DefaultStepExecutor
    def initialize(step_queue, flow)
      @step_queue = step_queue
      @flow = flow
    end

    def call
      return if @flow.errors.present?
      ActiveSupport::Notifications.instrument(flow_event_name, flow: @flow) do
        @step_queue.each do |step|
          break unless process_step(step)
        end
      end
    end

    protected

    def process_step(step)
      catch(:halt_step) { execute_step(step) }
      return true if @flow.errors.blank?
      ActiveSupport::Notifications.publish(
        event_name(step) + '.error', step: step, flow: @flow
      )
      false
    end

    def execute_step(step)
      ActiveSupport::Notifications.instrument(
        event_name(step), flow: @flow, step: step
      ) do |payload|
        payload[:step_result] = result = step.call(@flow)
        result.merge_into(@flow)
        result
      end
    end

    def flow_name
      @flow_name ||= @flow.class.to_s.underscore
    end

    def flow_event_name
      @flow_event_name ||= "business_flow.flow.#{flow_name}"
    end

    def step_event_name(step)
      "#{flow_name}.#{step.to_s.underscore}"
    end

    def event_name(step)
      "business_flow.step.#{step_event_name(step)}"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
business_flow-0.7.0 lib/business_flow/default_step_executor.rb
business_flow-0.6.0 lib/business_flow/default_step_executor.rb
business_flow-0.5.2 lib/business_flow/default_step_executor.rb
business_flow-0.5.1 lib/business_flow/default_step_executor.rb
business_flow-0.5.0 lib/business_flow/default_step_executor.rb
business_flow-0.4.4 lib/business_flow/default_step_executor.rb
business_flow-0.4.3 lib/business_flow/default_step_executor.rb