Module: Dry::Workflow::InstanceMethods

Defined in:
lib/dry/workflow.rb

Overview

Instance methods for classes that include Dry::Workflow::Mixin

Instance Method Summary collapse

Instance Method Details

#call(initial_input = {}) ⇒ Dry::Monads::Result

Executes the defined workflow.

Parameters:

  • initial_input (Object) (defaults to: {})

    The initial input to the first step.

Returns:

  • (Dry::Monads::Result)

    Success(value) or Failure(value)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dry/workflow.rb', line 120

def call(initial_input = {})
  executed_steps_with_rollbacks = []
  current_value = initial_input

  self.class.steps.each do |step|
    operation_result = execute_step_operation(step, current_value)

    if operation_result.failure?
      return execute_rollbacks(executed_steps_with_rollbacks, operation_result.failure, current_value)
    end

    current_value = operation_result.value!
    # Store the input to the step that succeeded, for rollback purposes
    if step.rollback_operation
      executed_steps_with_rollbacks << { step: step, input_to_step: operation_result.value! } # or current_value before this step?
      # Using output of step for rollback
    end
  end

  Success(current_value)
end