Class: NextStation::Operation
- Inherits:
-
Object
- Object
- NextStation::Operation
- Extended by:
- ClassMethods
- Defined in:
- lib/next_station/operation.rb,
lib/next_station/operation/node.rb,
lib/next_station/operation/errors.rb,
lib/next_station/operation/class_methods.rb
Overview
The core class for defining operations.
Operations are composed of steps and branches, and they return a NextStation::Result.
Defined Under Namespace
Modules: ClassMethods Classes: ErrorDefinition, ErrorsDSL, Halt, Node
Instance Method Summary collapse
-
#call(params = {}, context = {}) ⇒ NextStation::Result
Executes the operation.
-
#call_operation(state, operation_class, with_params:, store_result_in_key: nil) ⇒ NextStation::State
Calls another operation and integrates its result into the current state.
-
#dependency(name) ⇒ Object
Resolves a dependency by name.
-
#error!(type:, msg_keys: {}, details: {}) ⇒ Object
Halts the operation and returns a failure result.
-
#initialize(deps: {}) ⇒ Operation
constructor
Use ‘ .new(deps: …) ` to inject dependencies (e.g. test doubles).
-
#publish_log(level, message, payload = {}) ⇒ Object
Publishes a log event to the default monitor.
-
#validation(state) ⇒ NextStation::State
Built-in step for performing validation.
Methods included from ClassMethods
branch, dependencies, depends, disable_result_schema, enforce_result_schema, error_definitions, errors, force_validation!, has_step?, loaded_plugins, plugin, process, result_at, result_class, result_key, result_schema, schema_enforced?, skip_validation!, step, steps, validate_with, validation_contract_class, validation_contract_instance, validation_enforced?
Constructor Details
#initialize(deps: {}) ⇒ Operation
Use ‘ .new(deps: …) ` to inject dependencies (e.g. test doubles).
35 36 37 38 |
# File 'lib/next_station/operation.rb', line 35 def initialize(deps: {}) @injected_deps = deps @resolved_deps = {} end |
Instance Method Details
#call(params = {}, context = {}) ⇒ NextStation::Result
Executes the operation.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/next_station/operation.rb', line 60 def call(params = {}, context = {}) monitor = NextStation.config.monitor monitor.publish('operation.start', operation: self.class.name, params: params, context: context) start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) if self.class.validation_enforced? && !self.class.has_step?(:validation) raise ValidationError, 'Validation is enforced but step :validation is missing from process block' end @state = State.new(params, context, self.class.loaded_plugins) lang = context[:lang] || :en self.class.loaded_plugins.each do |mod| mod.on_operation_start(self, @state) if mod.respond_to?(:on_operation_start) end begin @state = execute_nodes(self.class.steps, @state) rescue Halt => e result = if e.error Result::Failure.new(e.error) else definition = self.class.error_definitions[e.type] raise "Undeclared error type: #{e.type}" unless definition = definition.(lang, e.msg_keys) Result::Failure.new( Result::Error.new( type: e.type, message: , help_url: definition.help_url, details: e.details, msg_keys: e.msg_keys ) ) end self.class.loaded_plugins.each do |mod| mod.on_operation_stop(self, result) if mod.respond_to?(:on_operation_stop) end monitor.publish('operation.stop', operation: self.class.name, duration: duration(start_time), result: result, state: @state) return result rescue NextStation::ValidationError => e raise e rescue NextStation::Error => e raise e rescue StandardError => e result = Result::Failure.new( Result::Error.new( type: :exception, message: e., details: { backtrace: e.backtrace } ) ) self.class.loaded_plugins.each do |mod| mod.on_operation_stop(self, result) if mod.respond_to?(:on_operation_stop) end monitor.publish('operation.stop', operation: self.class.name, duration: duration(start_time), result: result, state: @state) return result end key = self.class.result_key || :result unless @state.key?(key) raise NextStation::MissingResultKeyError, "Missing result key #{key.inspect} in state. " \ 'Operations must set this key or use result_at to specify another one.' end result = Result::Success.new( @state[key], schema: self.class.result_class, enforced: self.class.schema_enforced? ) self.class.loaded_plugins.each do |mod| mod.on_operation_stop(self, result) if mod.respond_to?(:on_operation_stop) end monitor.publish('operation.stop', operation: self.class.name, duration: duration(start_time), result: result, state: @state) result end |
#call_operation(state, operation_class, with_params:, store_result_in_key: nil) ⇒ NextStation::State
Calls another operation and integrates its result into the current state.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/next_station/operation.rb', line 233 def call_operation(state, operation_class, with_params:, store_result_in_key: nil) params = with_params.is_a?(Proc) ? with_params.call(state) : with_params operation = if operation_class.is_a?(Class) operation_class.new(deps: @injected_deps) else operation_class end result = operation.call(params, state.context) if result.success? state[store_result_in_key] = result.value if store_result_in_key state else child_error = result.error raise Halt.new(error: child_error) unless self.class.error_definitions.key?(child_error.type) error!( type: child_error.type, msg_keys: child_error.msg_keys, details: child_error.details ) end end |
#dependency(name) ⇒ Object
Resolves a dependency by name.
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/next_station/operation.rb', line 43 def dependency(name) return @resolved_deps[name] if @resolved_deps.key?(name) if @injected_deps.key?(name) @resolved_deps[name] = @injected_deps[name] else default = self.class.dependencies.fetch(name) @resolved_deps[name] = default.is_a?(Proc) ? default.call : default end end |
#error!(type:, msg_keys: {}, details: {}) ⇒ Object
Halts the operation and returns a failure result.
195 196 197 |
# File 'lib/next_station/operation.rb', line 195 def error!(type:, msg_keys: {}, details: {}) raise Halt.new(type: type, msg_keys: msg_keys, details: details) end |
#publish_log(level, message, payload = {}) ⇒ Object
Publishes a log event to the default monitor.
NextStation provides a built-in event system powered by dry-monitor to track user-defined logs. Inside your operation steps, you can use publish_log to broadcast custom events. These are automatically routed to the configured logger by default.
By default, NextStation logs to STDOUT using the standard Ruby Logger.
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/next_station/operation.rb', line 216 def publish_log(level, , payload = {}) NextStation.config.monitor.publish( 'log.custom', level: level, message: , operation: self.class.name, step_name: @state&.current_step, payload: payload ) end |
#validation(state) ⇒ NextStation::State
Built-in step for performing validation.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/next_station/operation.rb', line 161 def validation(state) contract_class = self.class.validation_contract_class raise ValidationError, 'Step :validation called but no contract defined via validate_with' unless contract_class return state unless self.class.validation_enforced? lang = state.context[:lang] || :en contract = self.class.validation_contract_instance result = contract.call(state.params) if result.success? state[:params] = result.to_h state else # Attempt to get localized errors from dry-validation, fallback to default if it fails # (e.g. if I18n is not configured for that language in dry-validation) validation_errors = begin result.errors(locale: lang).to_h rescue StandardError result.errors.to_h end error!( type: :validation, msg_keys: { errors: validation_errors }.merge(state.params), details: validation_errors ) end end |