Class: Jackhammer::MiddlewareCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/jackhammer/middleware_collection.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initializeMiddlewareCollection

Returns a new instance of MiddlewareCollection.



3
4
5
# File 'lib/jackhammer/middleware_collection.rb', line 3

def initialize
  @entries = []
end

Instance Method Details

#call(*args, **kwargs, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/jackhammer/middleware_collection.rb', line 11

def call(*args, **kwargs, &block)
  call_chain = @entries.map(&:instantiate) + [block]

  traverse = proc do |*procargs, **prockwargs|
    call_chain.shift.call(*procargs, **prockwargs, &traverse) unless call_chain.empty?
  end

  traverse.call(*args, **kwargs)
end

#use(klass, *args, **kwargs, &block) ⇒ Object



7
8
9
# File 'lib/jackhammer/middleware_collection.rb', line 7

def use(klass, *args, **kwargs, &block)
  @entries << Entry.new(klass: klass, args: args, kwargs: kwargs, block: block)
end