Class: Teckel::Operation::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/teckel/operation/runner.rb

Overview

Note:

You shouldn’t need to call this explicitly. Use MyOperation.with() or MyOperation.call() instead.

The default implementation for executing a single Teckel::Operation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, settings = UNDEFINED) ⇒ Runner

Returns a new instance of Runner.



13
14
15
# File 'lib/teckel/operation/runner.rb', line 13

def initialize(operation, settings = UNDEFINED)
  @operation, @settings = operation, settings
end

Instance Attribute Details

#operationObject (readonly)

Returns the value of attribute operation.



16
17
18
# File 'lib/teckel/operation/runner.rb', line 16

def operation
  @operation
end

#settingsObject (readonly)

Returns the value of attribute settings.



16
17
18
# File 'lib/teckel/operation/runner.rb', line 16

def settings
  @settings
end

Instance Method Details

#call(input = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/teckel/operation/runner.rb', line 18

def call(input = nil)
  catch(:failure) do
    out = catch(:success) do
      run operation.input_constructor.call(input)
      return nil # :sic!: return values need to go through +success!+
    end

    return out
  end
end

#fail!(*args) ⇒ Object (protected)

Halt any further execution with an error value

Returns:

  • a thing matching your error definition



54
55
56
57
58
59
60
61
62
63
# File 'lib/teckel/operation/runner.rb', line 54

def fail!(*args)
  value =
    if args.size == 1 && operation.error === args.first # rubocop:disable Style/CaseEquality
      args.first
    else
      operation.error_constructor.call(*args)
    end

  throw :failure, operation.result_constructor.call(value, false)
end

#success!(*args) ⇒ Object (protected)

Halt any further execution with a output value

Returns:

  • a thing matching your output definition



39
40
41
42
43
44
45
46
47
48
# File 'lib/teckel/operation/runner.rb', line 39

def success!(*args)
  value =
    if args.size == 1 && operation.output === args.first # rubocop:disable Style/CaseEquality
      args.first
    else
      operation.output_constructor.call(*args)
    end

  throw :success, operation.result_constructor.call(value, true)
end