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



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

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

#async?Boolean

Returns:

  • (Boolean)


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

def async?
  @async ||= false
end

#call(inputs = {}) ⇒ Object



145
146
147
# File 'lib/ruby_reactor/dsl/reactor.rb', line 145

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

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



89
90
91
92
93
94
95
96
97
# File 'lib/ruby_reactor/dsl/reactor.rb', line 89

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



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

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



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

def input_validations
  @input_validations ||= {}
end

#inputsObject



21
22
23
# File 'lib/ruby_reactor/dsl/reactor.rb', line 21

def inputs
  @inputs ||= {}
end

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



99
100
101
102
103
104
105
106
107
# File 'lib/ruby_reactor/dsl/reactor.rb', line 99

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



113
114
115
# File 'lib/ruby_reactor/dsl/reactor.rb', line 113

def middleware(middleware_class)
  middlewares << middleware_class
end

#middlewaresObject



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

def middlewares
  @middlewares ||= []
end

#retry_defaults(**kwargs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_reactor/dsl/reactor.rb', line 49

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



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

def return_step
  @return_step
end

#returns(step_name) ⇒ Object



109
110
111
# File 'lib/ruby_reactor/dsl/reactor.rb', line 109

def returns(step_name)
  @return_step = step_name
end

#run(inputs = {}) ⇒ Object

Entry point for running the reactor



140
141
142
143
# File 'lib/ruby_reactor/dsl/reactor.rb', line 140

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

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



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

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



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

def steps
  @steps ||= {}
end

#validate_inputs(inputs_hash) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ruby_reactor/dsl/reactor.rb', line 117

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