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
# File 'lib/teckel/operation/runner.rb', line 18

def call(input = nil)
  catch(:halt) do
    op = instance
    op_input = op.instance_exec(input, &operation.input_constructor)
    op.call(op_input)
    nil # return values need to go through +success!+ or +fail!+
  end
end

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

Halt any further execution with an error value

Returns:

  • a thing matching your error definition



62
63
64
65
66
67
68
69
70
71
# File 'lib/teckel/operation/runner.rb', line 62

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

  throw :halt, instance.instance_exec(value, false, &operation.result_constructor)
end

#instanceObject



27
28
29
30
31
32
33
34
35
# File 'lib/teckel/operation/runner.rb', line 27

def instance
  return @instance if instance_variable_defined?(:@instance)

  op = operation.new
  op.runner = self
  op.settings = settings unless settings.eql?(UNDEFINED)

  @instance = op
end

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

Halt any further execution with a output value

Returns:

  • a thing matching your output definition



47
48
49
50
51
52
53
54
55
56
# File 'lib/teckel/operation/runner.rb', line 47

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

  throw :halt, instance.instance_exec(value, true, &operation.result_constructor)
end