Class: RubyReactor::Dsl::StepBuilder

Inherits:
Object
  • Object
show all
Includes:
TemplateHelpers, ValidationHelpers
Defined in:
lib/ruby_reactor/dsl/step_builder.rb

Direct Known Subclasses

InterruptBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidationHelpers

#build_args_validator, #build_inline_validator, #build_macro_validator, #build_scalar_validator, #build_validation_schema, #create_input_validator

Methods included from TemplateHelpers

#Failure, #Success, #element, #input, #result, #value

Constructor Details

#initialize(name, impl = nil, reactor = nil) ⇒ StepBuilder

Returns a new instance of StepBuilder.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 12

def initialize(name, impl = nil, reactor = nil)
  @name = name
  @impl = impl
  @reactor = reactor
  @arguments = {}
  @run_block = nil
  @compensate_block = nil
  @undo_block = nil
  @conditions = []
  @guards = []
  @dependencies = []
  @arg_validations = []
  @validate_args_input = nil
  @args_validator = nil
  @output_validator = nil
  @async = false
  @retry_config = {}
end

Instance Attribute Details

#args_validatorObject

Returns the value of attribute args_validator.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def args_validator
  @args_validator
end

#argumentsObject

Returns the value of attribute arguments.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def arguments
  @arguments
end

#compensate_blockObject

Returns the value of attribute compensate_block.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def compensate_block
  @compensate_block
end

#conditionsObject

Returns the value of attribute conditions.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def conditions
  @conditions
end

#dependenciesObject

Returns the value of attribute dependencies.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def dependencies
  @dependencies
end

#guardsObject

Returns the value of attribute guards.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def guards
  @guards
end

#implObject

Returns the value of attribute impl.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def impl
  @impl
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def name
  @name
end

#output_validatorObject

Returns the value of attribute output_validator.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def output_validator
  @output_validator
end

#retry_configObject

Returns the value of attribute retry_config.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def retry_config
  @retry_config
end

#run_blockObject

Returns the value of attribute run_block.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def run_block
  @run_block
end

#undo_blockObject

Returns the value of attribute undo_block.



9
10
11
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 9

def undo_block
  @undo_block
end

Instance Method Details

#argument(name, source, type = nil, transform: nil, **predicates) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 31

def argument(name, source, type = nil, transform: nil, **predicates)
  @arguments[name] = {
    source: source,
    transform: transform
  }

  return unless type || predicates.any?

  @arg_validations << [name, type, false, predicates]
end

#async(async = true) ⇒ Object



88
89
90
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 88

def async(async = true)
  @async = async
end

#buildObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 100

def build
  step_config = {
    name: @name,
    impl: @impl,
    arguments: @arguments,
    run_block: @run_block,
    compensate_block: @compensate_block,
    undo_block: @undo_block,
    conditions: @conditions,
    guards: @guards,
    dependencies: @dependencies,
    args_validator: @args_validator || build_args_validator(@arg_validations, @validate_args_input),
    output_validator: @output_validator,
    async: @async,
    retry_config: @retry_config.empty? ? (@reactor&.retry_defaults || {}) : @retry_config
  }

  RubyReactor::Dsl::StepConfig.new(step_config)
end

#compensate(&block) ⇒ Object



46
47
48
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 46

def compensate(&block)
  @compensate_block = block
end

#guard(&guard_fn) ⇒ Object



58
59
60
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 58

def guard(&guard_fn)
  @guards << guard_fn
end

#retries(max_attempts: 3, backoff: :exponential, base_delay: 1) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 92

def retries(max_attempts: 3, backoff: :exponential, base_delay: 1)
  @retry_config = {
    max_attempts: max_attempts,
    backoff: backoff,
    base_delay: base_delay
  }
end

#run(&block) ⇒ Object



42
43
44
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 42

def run(&block)
  @run_block = block
end

#undo(&block) ⇒ Object



50
51
52
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 50

def undo(&block)
  @undo_block = block
end

#validate_args(schema_or_validator = nil, &block) ⇒ Object

Cross-field rules over the whole resolved argument hash. Composes with per-argument inline validations declared via ‘argument`; the block (or pre-built schema) is applied last and wins on conflicts.



69
70
71
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 69

def validate_args(schema_or_validator = nil, &block)
  @validate_args_input = block || schema_or_validator
end

#validate_output(type = nil, **predicates, &block) ⇒ Object

Scalar-aware output validation.

validate_output :integer, gteq?: 0   # single value
validate_output do ... end            # hash output
validate_output SomeSchema            # pre-built schema


77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 77

def validate_output(type = nil, **predicates, &block)
  @output_validator =
    if block
      create_input_validator(block)
    elsif type.is_a?(Symbol) || type.is_a?(Module) || predicates.any?
      build_scalar_validator(type, predicates)
    elsif type
      create_input_validator(type)
    end
end

#wait_for(*step_names) ⇒ Object



62
63
64
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 62

def wait_for(*step_names)
  @dependencies.concat(step_names)
end

#where(&predicate) ⇒ Object



54
55
56
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 54

def where(&predicate)
  @conditions << predicate
end