Module: RubyReactor::Dsl::Reactor::ClassMethods

Includes:
TemplateHelpers, ValidationHelpers
Defined in:
lib/ruby_reactor/dsl/reactor.rb

Instance Method Summary collapse

Methods included from ValidationHelpers

#build_validation_schema, #create_input_validator

Methods included from TemplateHelpers

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

Instance Method Details

#async(async = true) ⇒ Object



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

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

#async?Boolean

Returns:

  • (Boolean)


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

def async?
  @async ||= false
end

#call(inputs = {}) ⇒ Object



158
159
160
# File 'lib/ruby_reactor/dsl/reactor.rb', line 158

def call(inputs = {})
  run(inputs)
end

#compose(name, composed_reactor_class = nil, &block) ⇒ Object



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

def compose(name, composed_reactor_class = nil, &block)
  builder = RubyReactor::Dsl::ComposeBuilder.new(name, composed_reactor_class, self, &block)

  builder.instance_eval(&block) if block_given?

  step_config = builder.build
  steps[name] = step_config
  step_config
end

#input(name, transform: nil, description: nil, validate: nil, optional: false, redact: false, &validation_block) ⇒ Object

rubocop:disable Metrics/ParameterLists



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_reactor/dsl/reactor.rb', line 65

def input(name, transform: nil, description: nil, validate: nil, optional: false, redact: false,
          &validation_block)
  # rubocop:enable Metrics/ParameterLists
  inputs[name] = {
    transform: transform,
    description: description,
    optional: optional,
    redact: redact
  }

  # Handle validation
  return unless validate || validation_block

  validator = create_input_validator(validation_block || validate)
  input_validations[name] = validator
end

#input_validationsObject



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

def input_validations
  @input_validations ||= {}
end

#inputsObject



24
25
26
# File 'lib/ruby_reactor/dsl/reactor.rb', line 24

def inputs
  @inputs ||= {}
end

#interrupt(name, &block) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/ruby_reactor/dsl/reactor.rb', line 112

def interrupt(name, &block)
  builder = RubyReactor::Dsl::InterruptBuilder.new(name, self)
  builder.instance_eval(&block) if block_given?

  step_config = builder.build
  steps[name] = step_config
  step_config
end

#map(name, reactor_class = nil, &block) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/ruby_reactor/dsl/reactor.rb', line 102

def map(name, reactor_class = nil, &block)
  builder = RubyReactor::Dsl::MapBuilder.new(name, reactor_class, self, &block)

  builder.instance_eval(&block) if block_given?

  step_config = builder.build
  steps[name] = step_config
  step_config
end

#middleware(middleware_class) ⇒ Object



126
127
128
# File 'lib/ruby_reactor/dsl/reactor.rb', line 126

def middleware(middleware_class)
  middlewares << middleware_class
end

#middlewaresObject



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

def middlewares
  @middlewares ||= []
end

#retry_defaults(**kwargs) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_reactor/dsl/reactor.rb', line 52

def retry_defaults(**kwargs)
  if kwargs.empty?
    @retry_defaults ||= { max_attempts: 1, backoff: :exponential, base_delay: 1 }
  else
    @retry_defaults = {
      max_attempts: kwargs[:max_attempts] || 1,
      backoff: kwargs[:backoff] || :exponential,
      base_delay: kwargs[:base_delay] || 1
    }
  end
end

#return_stepObject



32
33
34
# File 'lib/ruby_reactor/dsl/reactor.rb', line 32

def return_step
  @return_step
end

#returns(step_name = nil) ⇒ Object



121
122
123
124
# File 'lib/ruby_reactor/dsl/reactor.rb', line 121

def returns(step_name = nil)
  @return_step = step_name if step_name
  @return_step
end

#run(inputs = {}) ⇒ Object

Entry point for running the reactor



153
154
155
156
# File 'lib/ruby_reactor/dsl/reactor.rb', line 153

def run(inputs = {})
  reactor = new
  reactor.run(inputs)
end

#step(name, impl = nil, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/ruby_reactor/dsl/reactor.rb', line 82

def step(name, impl = nil, &block)
  builder = RubyReactor::Dsl::StepBuilder.new(name, impl, self)

  builder.instance_eval(&block) if block_given?

  step_config = builder.build
  steps[name] = step_config
  step_config
end

#stepsObject



28
29
30
# File 'lib/ruby_reactor/dsl/reactor.rb', line 28

def steps
  @steps ||= {}
end

#validate_inputs(inputs_hash) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ruby_reactor/dsl/reactor.rb', line 130

def validate_inputs(inputs_hash)
  errors = {}

  input_validations.each do |input_name, validator|
    # Skip validation if input is optional and not provided
    next if inputs[input_name][:optional] && !inputs_hash.key?(input_name)

    input_data = inputs_hash[input_name]
    # Validate by wrapping the individual input in a hash with its name
    result = validator.call({ input_name => input_data })

    errors.merge!(result.error.field_errors) if result.failure? && result.error.respond_to?(:field_errors)
  end

  if errors.empty?
    RubyReactor.Success(inputs_hash)
  else
    error = RubyReactor::Error::InputValidationError.new(errors)
    RubyReactor.Failure(error)
  end
end