Class: Operandi::Base
- Inherits:
-
Object
- Object
- Operandi::Base
- Extended by:
- CallbackDsl
- Includes:
- Callbacks, Concerns::Execution, Concerns::ParentService, Concerns::StateManagement, Dsl::ArgumentsDsl, Dsl::OutputsDsl, Dsl::StepsDsl
- Defined in:
- lib/operandi/base.rb
Overview
Base class for building service objects with arguments, outputs, and steps.
Constant Summary
Constants included from Callbacks
Class Attribute Summary collapse
-
.class_config ⇒ Hash?
Class-level configuration options.
Instance Attribute Summary collapse
-
#arg ⇒ Collection::Base
readonly
Collection of argument values.
-
#errors ⇒ Messages
readonly
Collection of error messages.
-
#output ⇒ Collection::Base
readonly
Collection of output values.
-
#warnings ⇒ Messages
readonly
Collection of warning messages.
Class Method Summary collapse
-
.config(config = {}) ⇒ Hash
Set class-level configuration for this service.
-
.run(**kwargs) ⇒ Base
Run the service and return the result.
-
.run!(**kwargs) ⇒ Base
Run the service and raise an error if it fails.
-
.with(service_or_config, config = {}) ⇒ BaseWithContext
Create a context for running the service with a parent service or config.
Instance Method Summary collapse
-
#call ⇒ void
Execute the service steps.
-
#errors? ⇒ Boolean
Check if the service has any errors.
-
#fail!(message) ⇒ void
Add an error to the :base key.
-
#fail_immediately!(message) ⇒ void
Add an error and stop execution immediately, causing transaction rollback.
-
#failed? ⇒ Boolean
Check if the service completed with errors.
-
#initialize(args = {}, config = {}, parent_service = nil) ⇒ Base
constructor
Initialize a new service instance.
-
#stop! ⇒ Boolean
Stop executing remaining steps after the current step completes.
-
#stop_immediately! ⇒ void
Stop execution immediately, skipping any remaining code in the current step.
-
#stopped? ⇒ Boolean
Check if the service has been stopped.
-
#success? ⇒ Boolean
(also: #successful?)
Check if the service completed without errors.
-
#warnings? ⇒ Boolean
Check if the service has any warnings.
Methods included from CallbackDsl
after_service_run, after_step_run, all_callbacks_for, around_service_run, around_step_run, before_service_run, before_step_run, callbacks_for, on_service_failure, on_service_success, on_step_crash, on_step_failure, on_step_success
Methods included from Dsl::StepsDsl
Methods included from Dsl::OutputsDsl
Methods included from Dsl::ArgumentsDsl
Methods included from Callbacks
Constructor Details
#initialize(args = {}, config = {}, parent_service = nil) ⇒ Base
Initialize a new service instance.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/operandi/base.rb', line 71 def initialize(args = {}, config = {}, parent_service = nil) @config = Operandi.config.merge(self.class.class_config || {}).merge(config) @parent_service = parent_service @output = Collection::Base.new(self, CollectionTypes::OUTPUTS) @arg = Collection::Base.new(self, CollectionTypes::ARGUMENTS, args.dup) @stopped = false @launched_steps = [] initialize_errors initialize_warnings end |
Class Attribute Details
.class_config ⇒ Hash?
Returns class-level configuration options.
177 178 179 |
# File 'lib/operandi/base.rb', line 177 def class_config @class_config end |
Instance Attribute Details
#arg ⇒ Collection::Base (readonly)
Returns collection of argument values.
55 56 57 |
# File 'lib/operandi/base.rb', line 55 def arg @arg end |
#errors ⇒ Messages (readonly)
Returns collection of error messages.
61 62 63 |
# File 'lib/operandi/base.rb', line 61 def errors @errors end |
#output ⇒ Collection::Base (readonly)
Returns collection of output values.
58 59 60 |
# File 'lib/operandi/base.rb', line 58 def output @output end |
#warnings ⇒ Messages (readonly)
Returns collection of warning messages.
64 65 66 |
# File 'lib/operandi/base.rb', line 64 def warnings @warnings end |
Class Method Details
.config(config = {}) ⇒ Hash
Set class-level configuration for this service.
183 184 185 |
# File 'lib/operandi/base.rb', line 183 def config(config = {}) self.class_config = config end |
.run(**kwargs) ⇒ Base
Run the service and return the result.
195 196 197 |
# File 'lib/operandi/base.rb', line 195 def run(**kwargs) new(kwargs).tap(&:call) end |
.run!(**kwargs) ⇒ Base
Run the service and raise an error if it fails.
207 208 209 |
# File 'lib/operandi/base.rb', line 207 def run!(**kwargs) new(kwargs, { raise_on_error: true }).tap(&:call) end |
.with(service_or_config, config = {}) ⇒ BaseWithContext
Create a context for running the service with a parent service or config.
222 223 224 225 226 227 |
# File 'lib/operandi/base.rb', line 222 def with(service_or_config, config = {}) service = service_or_config.is_a?(Hash) ? nil : service_or_config config = service_or_config unless service BaseWithContext.new(self, service, config.dup) end |
Instance Method Details
#call ⇒ void
This method returns an undefined value.
Execute the service steps.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/operandi/base.rb', line 160 def call load_defaults_and_validate run_callbacks(:before_service_run, self) run_callbacks(:around_service_run, self) do execute_service end run_service_result_callbacks rescue StandardError => e run_steps_with_always raise e end |
#errors? ⇒ Boolean
Check if the service has any errors.
103 104 105 |
# File 'lib/operandi/base.rb', line 103 def errors? @errors.any? end |
#fail!(message) ⇒ void
This method returns an undefined value.
Add an error to the :base key.
141 142 143 |
# File 'lib/operandi/base.rb', line 141 def fail!() errors.add(:base, ) end |
#fail_immediately!(message) ⇒ void
This method returns an undefined value.
Add an error and stop execution immediately, causing transaction rollback.
Steps marked with always: true will still run after this method is called.
151 152 153 154 |
# File 'lib/operandi/base.rb', line 151 def fail_immediately!() errors.add(:base, , rollback: false) raise Operandi::FailExecution end |
#failed? ⇒ Boolean
Check if the service completed with errors.
96 97 98 |
# File 'lib/operandi/base.rb', line 96 def failed? errors? end |
#stop! ⇒ Boolean
Stop executing remaining steps after the current step completes.
117 118 119 |
# File 'lib/operandi/base.rb', line 117 def stop! @stopped = true end |
#stop_immediately! ⇒ void
This method returns an undefined value.
Stop execution immediately, skipping any remaining code in the current step.
132 133 134 135 |
# File 'lib/operandi/base.rb', line 132 def stop_immediately! @stopped = true raise Operandi::StopExecution end |
#stopped? ⇒ Boolean
Check if the service has been stopped.
124 125 126 |
# File 'lib/operandi/base.rb', line 124 def stopped? @stopped end |
#success? ⇒ Boolean Also known as: successful?
Check if the service completed without errors.
88 89 90 |
# File 'lib/operandi/base.rb', line 88 def success? !errors? end |
#warnings? ⇒ Boolean
Check if the service has any warnings.
110 111 112 |
# File 'lib/operandi/base.rb', line 110 def warnings? @warnings.any? end |