Class: SorbetOperation::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Helpers, T::Sig
Defined in:
lib/sorbet_operation/base.rb

Overview

This class is abstract.

It cannot be directly instantiated. Subclasses must implement the abstract methods below.

Abstract base class for operations.

Subclasses must:

  1. define the ValueType type member

  2. implement the #execute method

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.

Examples:

If the operation returns a String or nil

ValueType = type_member { { fixed: T.nilable(String) } }

If the operation does not return a value

ValueType = type_member { { fixed: NilClass } }

See Also:

type_member { { upper: Object } }

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (::Logger)


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

#executeValueType (private)

This method is abstract.

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.

Returns:



77
# File 'lib/sorbet_operation/base.rb', line 77

def execute; end

#performResult[ValueType]

Performs the operation and returns the result.

Returns:



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