Module: Dry::Workflow::Mixin

Defined in:
lib/dry/workflow.rb

Overview

The main mixin to be included in classes that define workflows.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



22
23
24
# File 'lib/dry/workflow.rb', line 22

def steps
  @steps
end

#workflow_instance_methods_moduleObject (readonly)

Returns the value of attribute workflow_instance_methods_module.



22
23
24
# File 'lib/dry/workflow.rb', line 22

def workflow_instance_methods_module
  @workflow_instance_methods_module
end

Class Method Details

.extended(base) ⇒ Object



16
17
18
19
20
# File 'lib/dry/workflow.rb', line 16

def self.extended(base)
  base.instance_variable_set(:@steps, [])
  base.instance_variable_set(:@workflow_instance_methods_module, Module.new)
  base.include base.instance_variable_get(:@workflow_instance_methods_module)
end

Instance Method Details

#map(step_name, with: nil, **options) ⇒ void

This method returns an undefined value.

Defines a ‘map’ step, which is expected to transform the input and always succeed. Rollbacks are typically not defined for map steps.

Parameters:

  • step_name (Symbol)

    the name of the step

  • with (Symbol, Proc) (defaults to: nil)

    an optional explicit method name or proc for the step’s operation

  • options (Hash)

    additional options for the step



66
67
68
69
70
71
72
73
74
# File 'lib/dry/workflow.rb', line 66

def map(step_name, with: nil, **options)
  define_workflow_step(
    name: step_name,
    type: :map,
    operation_source: with || step_name,
    rollback_source: nil, # Maps typically don't have rollbacks
    options: options
  )
end

#step(step_name, with: nil, rollback: nil, **options) ⇒ void

This method returns an undefined value.

Defines a standard step in the workflow.

Parameters:

  • step_name (Symbol)

    the name of the step (and the method to call if :with is not provided)

  • with (Symbol, Proc) (defaults to: nil)

    an optional explicit method name or proc for the step’s operation

  • rollback (Symbol, Proc) (defaults to: nil)

    an optional method name or proc for rollback

  • options (Hash)

    additional options for the step



31
32
33
34
35
36
37
38
39
# File 'lib/dry/workflow.rb', line 31

def step(step_name, with: nil, rollback: nil, **options)
  define_workflow_step(
    name: step_name,
    type: :step,
    operation_source: with || step_name,
    rollback_source: rollback,
    options: options
  )
end

#try(step_name, with: nil, rollback: nil, catch_exception: StandardError, **options) ⇒ void

This method returns an undefined value.

Defines a ‘try’ step that catches exceptions and wraps the result in a Result monad.

Parameters:

  • step_name (Symbol)

    the name of the step

  • with (Symbol, Proc) (defaults to: nil)

    an optional explicit method name or proc for the step’s operation

  • rollback (Symbol, Proc) (defaults to: nil)

    an optional method name or proc for rollback

  • catch_exception (Class, Array<Class>) (defaults to: StandardError)

    Exception(s) to catch

  • options (Hash)

    additional options for the step



49
50
51
52
53
54
55
56
57
# File 'lib/dry/workflow.rb', line 49

def try(step_name, with: nil, rollback: nil, catch_exception: StandardError, **options)
  define_workflow_step(
    name: step_name,
    type: :try,
    operation_source: with || step_name,
    rollback_source: rollback,
    options: options.merge(catch_exception: catch_exception)
  )
end