Class: SorbetOperation::Base Abstract
- Inherits:
-
Object
- Object
- SorbetOperation::Base
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Defined in:
- lib/sorbet_operation/base.rb
Overview
Constant Summary collapse
- ValueType =
The type of the value returned by this operation. The type can be any valid Sorbet type, as long as it’s a subtype of
Object
. type_member { { upper: Object } }
Instance Attribute Summary collapse
-
#logger ⇒ ::Logger
private
Returns the logger for this operation.
Instance Method Summary collapse
-
#execute ⇒ ValueType
private
abstract
Implement this method in subclasses to perform the operation.
-
#perform ⇒ Result[ValueType]
Performs the operation and returns the result.
Instance Attribute Details
#logger ⇒ ::Logger (private)
Returns the logger for this operation. If no logger has been set, the default logger will be returned instead.
82 83 84 |
# File 'lib/sorbet_operation/base.rb', line 82 def logger @logger ||= T.let(SorbetOperation.default_logger, T.nilable(::Logger)) end |
Instance Method Details
#execute ⇒ ValueType (private)
Implement this method in subclasses to perform the operation.
This method must either return a value of type ValueType, in which case the operation is considered successful, or raise an exception of type Failure, in which case the operation is considered failed.
Raising an exception of any other type will result in an unhandled exception. The exception will not be caught and will be propagated to the caller.
This method should be declared as private
in subclasses to prevent callers from calling it directly. Callers should instead call #perform to perform the operation and get the result.
77 |
# File 'lib/sorbet_operation/base.rb', line 77 def execute; end |
#perform ⇒ Result[ValueType]
Performs the operation and returns the result.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sorbet_operation/base.rb', line 40 def perform logger.debug { "Performing operation #{self.class.name}" } begin value = execute rescue Failure => e logger.debug { "Operation #{self.class.name} failed, failure = #{e.inspect}" } Result.new(false, nil, e) else logger.debug { "Operation #{self.class.name} succeeded, return value = #{value.inspect}" } Result.new(true, value, nil) end end |