Class: Servus::Base Abstract
- Inherits:
-
Object
- Object
- Servus::Base
- Includes:
- Events::Emitter, Guards, Support::Errors, Support::Rescuer
- Defined in:
- lib/servus/base.rb
Overview
Subclass and implement initialize and call methods to create a service
Base class for all service objects in the Servus framework.
This class provides the foundational functionality for implementing the Service Object pattern, including automatic validation, logging, benchmarking, and error handling.
Constant Summary collapse
- Logger =
Support class aliases
Servus::Support::Logger
- Emitter =
Servus::Events::Emitter
- Response =
Servus::Support::Response
- Validator =
Servus::Support::Validator
Class Attribute Summary collapse
-
.arguments_schema ⇒ Hash?
readonly
private
Returns the arguments schema defined via the schema DSL method.
-
.result_schema ⇒ Hash?
readonly
private
Returns the result schema defined via the schema DSL method.
Class Method Summary collapse
-
.after_call(result, instance) ⇒ void
private
Executes post-call hooks including result validation and event emission.
-
.before_call(args) ⇒ void
private
Executes pre-call hooks including logging and argument validation.
-
.benchmark(**_args) ⇒ Servus::Support::Response
private
Measures service execution time and logs the result.
-
.call(**args) ⇒ Servus::Support::Response
Executes the service with automatic validation, logging, and benchmarking.
-
.schema(arguments: nil, result: nil) ⇒ void
Defines schema validation rules for the service's arguments and/or result.
Instance Method Summary collapse
-
#error!(message = nil, type: Servus::Support::Errors::ServiceError) ⇒ void
Logs an error and raises an exception, halting service execution.
-
#failure(message = nil, type: Servus::Support::Errors::ServiceError) ⇒ Servus::Support::Response
Creates a failure response with an error.
-
#success(data) ⇒ Servus::Support::Response
Creates a successful response with the provided data.
Methods included from Guards
Methods included from Events::Emitter
#emit_events_for, emit_result_events!
Methods included from Support::Rescuer
Class Attribute Details
.arguments_schema ⇒ Hash? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the arguments schema defined via the schema DSL method.
257 258 259 |
# File 'lib/servus/base.rb', line 257 def arguments_schema @arguments_schema end |
.result_schema ⇒ Hash? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the result schema defined via the schema DSL method.
263 264 265 |
# File 'lib/servus/base.rb', line 263 def result_schema @result_schema end |
Class Method Details
.after_call(result, instance) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Executes post-call hooks including result validation and event emission.
This method is automatically called after service execution completes and handles:
- Validating the result data against RESULT_SCHEMA (if defined)
- Emitting events declared with the emits DSL
293 294 295 296 |
# File 'lib/servus/base.rb', line 293 def after_call(result, instance) Validator.validate_result!(self, result) Emitter.emit_result_events!(instance, result) end |
.before_call(args) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Executes pre-call hooks including logging and argument validation.
This method is automatically called before service execution and handles:
- Logging the service call with arguments
- Validating arguments against ARGUMENTS_SCHEMA (if defined)
276 277 278 279 |
# File 'lib/servus/base.rb', line 276 def before_call(args) Logger.log_call(self, args) Validator.validate_arguments!(self, args) end |
.benchmark(**_args) ⇒ Servus::Support::Response
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Measures service execution time and logs the result.
This method wraps the service execution to capture timing metrics. The duration is logged along with the success/failure status of the service.
308 309 310 311 312 313 314 315 316 |
# File 'lib/servus/base.rb', line 308 def benchmark(**_args) start_time = Time.now.utc result = yield duration = Time.now.utc - start_time Logger.log_result(self, result, duration) result end |
.call(**args) ⇒ Servus::Support::Response
Executes the service with automatic validation, logging, and benchmarking.
This is the primary entry point for executing services. It handles the complete service lifecycle including:
- Input argument validation against schema
- Service instantiation
- Execution timing/benchmarking
- Result validation against schema
- Automatic logging of calls, results, and errors
rubocop:disable Metrics/MethodLength
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/servus/base.rb', line 185 def call(**args) before_call(args) instance = new(**args) # Wrap execution in catch block to handle guard failures result = catch(:guard_failure) do benchmark(**args) { instance.call } end # If result is a GuardError, a guard failed - wrap in failure Response result = Response.new(false, nil, result) if result.is_a?(Servus::Support::Errors::GuardError) after_call(result, instance) result rescue Servus::Support::Errors::ValidationError => e Logger.log_validation_error(self, e) raise e rescue StandardError => e Logger.log_exception(self, e) raise e end |
.schema(arguments: nil, result: nil) ⇒ void
This method returns an undefined value.
Defines schema validation rules for the service's arguments and/or result.
This method provides a clean DSL for specifying JSON schemas that will be used to validate service inputs and outputs. Schemas defined via this method take precedence over ARGUMENTS_SCHEMA and RESULT_SCHEMA constants. The next major version will deprecate those constants in favor of this DSL.
248 249 250 251 |
# File 'lib/servus/base.rb', line 248 def schema(arguments: nil, result: nil) @arguments_schema = arguments.with_indifferent_access if arguments @result_schema = result.with_indifferent_access if result end |
Instance Method Details
#error!(message = nil, type: Servus::Support::Errors::ServiceError) ⇒ void
Prefer #failure for expected error conditions. Use this for exceptional cases.
This method returns an undefined value.
Logs an error and raises an exception, halting service execution.
Use this method when you need to immediately halt execution with an exception rather than returning a failure response. The error is automatically logged before the exception is raised.
143 144 145 146 147 148 149 150 151 |
# File 'lib/servus/base.rb', line 143 def error!( = nil, type: Servus::Support::Errors::ServiceError) error = type.new() Logger.log_exception(self.class, error) # Emit error! events before raising emit_events_for(:error!, Response.new(false, nil, error)) raise type, end |
#failure(message = nil, type: Servus::Support::Errors::ServiceError) ⇒ Servus::Support::Response
Creates a failure response with an error.
Use this method to return failure results from your service's call method. The failure is logged automatically and returns a response containing the error.
115 116 117 118 |
# File 'lib/servus/base.rb', line 115 def failure( = nil, type: Servus::Support::Errors::ServiceError) error = type.new() Response.new(false, nil, error) end |
#success(data) ⇒ Servus::Support::Response
Creates a successful response with the provided data.
Use this method to return successful results from your service's call method. The data will be validated against the RESULT_SCHEMA if one is defined.
81 82 83 |
# File 'lib/servus/base.rb', line 81 def success(data) Response.new(true, data, nil) end |