Class: Servus::Base Abstract
- Inherits:
-
Object
- Object
- Servus::Base
- Extended by:
- Schema::Declaration
- Includes:
- Events::Emitter, Guards, Support::Errors, Support::Lockdown, 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
Constants included from Events::Emitter
Events::Emitter::EMISSION_TRIGGERS
Class Method Summary collapse
-
.after_call(result, instance) ⇒ void
private
Executes post-call hooks including result validation and event emission.
-
.arguments_schema ⇒ Hash?
The compiled arguments schema.
-
.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.
-
.failure_schema ⇒ Hash?
The compiled failure schema.
-
.result_schema ⇒ Hash?
The compiled result schema.
-
.schema(arguments: nil, result: nil, failure: nil) ⇒ void
Declares the JSON schemas used to validate this service.
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, data: 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 Schema::Declaration
declare_schemas, schema, schema_types
Methods included from Guards
Methods included from Events::Emitter
#build_event_payload, #emission_condition_met?, #emit_events_for, emit_result_events!, #evaluate_emission_condition, #require_event_schema!, #validate_event_payload!
Methods included from Support::Lockdown
Methods included from Support::Rescuer
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
305 306 307 308 |
# File 'lib/servus/base.rb', line 305 def after_call(result, instance) Validator.validate_result!(self, result) Emitter.emit_result_events!(instance, result) end |
.arguments_schema ⇒ Hash?
Returns the compiled arguments schema.
110 |
# File 'lib/servus/base.rb', line 110 declare_schemas :arguments, :result, :failure |
.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)
288 289 290 291 |
# File 'lib/servus/base.rb', line 288 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.
320 321 322 323 324 325 326 327 328 |
# File 'lib/servus/base.rb', line 320 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
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/servus/base.rb', line 250 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.send(:call) } end if result.is_a?(Servus::Support::Errors::GuardError) Logger.log_guard_failure(self, result) result = Response.new(false, nil, result) end 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 |
.failure_schema ⇒ Hash?
Returns the compiled failure schema.
110 |
# File 'lib/servus/base.rb', line 110 declare_schemas :arguments, :result, :failure |
.result_schema ⇒ Hash?
Returns the compiled result schema.
110 |
# File 'lib/servus/base.rb', line 110 declare_schemas :arguments, :result, :failure |
.schema(arguments: nil, result: nil, failure: nil) ⇒ void
This method returns an undefined value.
Declares the JSON schemas used to validate this service.
Arguments are validated before call runs, so the body can trust the
shape of its inputs. Result data is validated after it returns, so a
service that stops honouring its own contract fails loudly rather than
shipping the wrong shape to its callers.
Schemas may reference shared fragments registered with Schema.register; refs are resolved on first read.
Omitting a keyword leaves any schema declared earlier — or by a
superclass — in place. Passing one explicitly as nil raises.
110 |
# File 'lib/servus/base.rb', line 110 declare_schemas :arguments, :result, :failure |
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.
208 209 210 211 212 213 214 215 216 |
# File 'lib/servus/base.rb', line 208 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, data: 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.
180 181 182 183 |
# File 'lib/servus/base.rb', line 180 def failure( = nil, data: nil, type: Servus::Support::Errors::ServiceError) error = type.new() Response.new(false, data, 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.
139 140 141 |
# File 'lib/servus/base.rb', line 139 def success(data) Response.new(true, data, nil) end |