Class: Upperkut::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/upperkut/middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



6
7
8
# File 'lib/upperkut/middleware.rb', line 6

def initialize
  @items = []
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



4
5
6
# File 'lib/upperkut/middleware.rb', line 4

def items
  @items
end

Instance Method Details

#add(item) ⇒ Object



10
11
12
13
14
# File 'lib/upperkut/middleware.rb', line 10

def add(item)
  return @items if @items.include?(item)

  @items << item
end

#invoke(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/upperkut/middleware.rb', line 20

def invoke(*args)
  chain = @items.map(&:new)

  traverse_chain = lambda do
    if chain.empty?
      yield
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end

  traverse_chain.call
end

#remove(item) ⇒ Object



16
17
18
# File 'lib/upperkut/middleware.rb', line 16

def remove(item)
  @items.delete(item)
end