Class: RubyReactor::Dsl::StepBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidationHelpers

#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
# 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 = []
  @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, transform: nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 29

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

#async(async = true) ⇒ Object



76
77
78
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 76

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

#buildObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 88

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,
    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



40
41
42
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 40

def compensate(&block)
  @compensate_block = block
end

#guard(&guard_fn) ⇒ Object



52
53
54
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 52

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

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



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

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



36
37
38
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 36

def run(&block)
  @run_block = block
end

#undo(&block) ⇒ Object



44
45
46
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 44

def undo(&block)
  @undo_block = block
end

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



60
61
62
63
64
65
66
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 60

def validate_args(schema_or_validator = nil, &block)
  if block_given?
    @args_validator = build_input_validator(block)
  elsif schema_or_validator
    @args_validator = build_input_validator(schema_or_validator)
  end
end

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



68
69
70
71
72
73
74
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 68

def validate_output(schema_or_validator = nil, &block)
  if block_given?
    @output_validator = build_input_validator(block)
  elsif schema_or_validator
    @output_validator = build_input_validator(schema_or_validator)
  end
end

#wait_for(*step_names) ⇒ Object



56
57
58
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 56

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

#where(&predicate) ⇒ Object



48
49
50
# File 'lib/ruby_reactor/dsl/step_builder.rb', line 48

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