class EXEL::Middleware::Chain

Chain of middleware to be invoked in sequence around each processor execution.

Constants

Entry

Attributes

entries[R]

Public Class Methods

new() click to toggle source
# File lib/exel/middleware/chain.rb, line 30
def initialize
  @entries = []
end

Public Instance Methods

add(klass, *args) click to toggle source

Adds a middleware class to the chain. If it is already in the chain it will be removed and added to the end. Any additional arguments will be passed to new when the middleware is created.

# File lib/exel/middleware/chain.rb, line 36
def add(klass, *args)
  remove(klass)
  @entries << Entry.new(klass, args)
end
include?(klass) click to toggle source

Returns true if the given class is in the chain.

# File lib/exel/middleware/chain.rb, line 47
def include?(klass)
  @entries.any? { |entry| entry.klass == klass }
end
invoke(*args) { || ... } click to toggle source

Calls each middleware in the chain.

# File lib/exel/middleware/chain.rb, line 52
def invoke(*args)
  chain = @entries.map { |entry| entry.klass.new(*entry.args) }

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

  traverse_chain.call
end
remove(klass) click to toggle source

Removes a middleware class from the chain.

# File lib/exel/middleware/chain.rb, line 42
def remove(klass)
  @entries.delete_if { |entry| entry.klass == klass }
end