Class: LB::Operation

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Includes:
Log
Defined in:
lib/lb/operation.rb,
lib/lb/operation/log.rb,
lib/lb/operation/version.rb

Overview

Base class for operations

Defined Under Namespace

Modules: Log

Constant Summary collapse

VERSION =

Version

'0.0.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

included, #log, #logger

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config


19
20
21
# File 'lib/lb/operation.rb', line 19

def config
  @config
end

Class Method Details

.operation_nameSymbol

Retrieves the operation name.

Returns:

  • (Symbol)

24
25
26
# File 'lib/lb/operation.rb', line 24

def self.operation_name
  config.name
end

Instance Method Details

#callself

Executes the operation.

Returns:

  • (self)

Raises:

  • (NotImplementedError)

31
32
33
# File 'lib/lb/operation.rb', line 31

def call
  raise NotImplementedError, 'Override Operation#call(*args)!'
end

#handle_exception(error) ⇒ self

Handles exception if given: Logs message and backtrace as :error.

Parameters:

  • error (Hash)

Returns:

  • (self)

49
50
51
52
53
54
55
56
# File 'lib/lb/operation.rb', line 49

def handle_exception(error)
  return self unless error.key?(:exception)
  exception = error.fetch(:exception)
  log :error, exception.message
  log :error, exception.backtrace.inspect

  self
end

#value(*args) ⇒ Object

Executes the operation with given arguments. Returns result or value of result if result responds to :value.

Parameters:

  • args (Array)

Returns:

  • (Object)

40
41
42
43
# File 'lib/lb/operation.rb', line 40

def value(*args)
  result = call(*args)
  result.respond_to?(:value) ? result.value : result
end