Class: Eventboss::Middleware::Chain

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



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

def initialize
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

Instance Method Details

#add(klass, options = {}) ⇒ Object



10
11
12
# File 'lib/eventboss/middleware.rb', line 10

def add(klass, options = {})
  @entries << Entry.new(klass, options)
end

#clearObject



27
28
29
# File 'lib/eventboss/middleware.rb', line 27

def clear
  @entries.clear
end

#invoke(*args) ⇒ Object



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

def invoke(*args)
  chain = @entries.map(&:build).reverse!

  invoke_lambda = lambda do
    if (mid = chain.pop)
      mid.call(*args, &invoke_lambda)
    else
      yield
    end
  end
  invoke_lambda.call
end