Class: Sbmt::Outbox::Middleware::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/outbox/middleware/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack) ⇒ Runner

Returns a new instance of Runner.



9
10
11
# File 'lib/sbmt/outbox/middleware/runner.rb', line 9

def initialize(stack)
  @stack = stack
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



7
8
9
# File 'lib/sbmt/outbox/middleware/runner.rb', line 7

def stack
  @stack
end

Instance Method Details

#call(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sbmt/outbox/middleware/runner.rb', line 13

def call(*args)
  return yield if stack.empty?

  chain = stack.map { |i| i.new }
  traverse_chain = proc do
    if chain.empty?
      yield
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end
  traverse_chain.call
end