Class: Sequent::Core::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/sequent/core/middleware/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



9
10
11
# File 'lib/sequent/core/middleware/chain.rb', line 9

def initialize
  clear
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



7
8
9
# File 'lib/sequent/core/middleware/chain.rb', line 7

def entries
  @entries
end

Instance Method Details

#add(middleware) ⇒ Object



13
14
15
# File 'lib/sequent/core/middleware/chain.rb', line 13

def add(middleware)
  @entries.push(middleware)
end

#clearObject



17
18
19
# File 'lib/sequent/core/middleware/chain.rb', line 17

def clear
  @entries = []
end

#invoke(*args, &invoker) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sequent/core/middleware/chain.rb', line 21

def invoke(*args, &invoker)
  chain = @entries.dup

  traverse_chain = -> do
    if chain.empty?
      invoker.call
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end

  traverse_chain.call
end