Class: SorbetOperation::Base

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

Overview

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

#loggerObject (private)



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

#executeObject (private)



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

def execute; end

#performObject



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