class Fluffle::MiddlewareStack

Public Class Methods

new() click to toggle source
# File lib/fluffle/middleware_stack.rb, line 3
def initialize
  @stack = []
end

Public Instance Methods

<<(middleware)
Alias for: push
call(callable = nil, &block) click to toggle source

Calls the stack in FIFO order with the callable (passed as an object receiving `#call` or an `&block`) being called last.

For example:

stack.push 1
stack.push 2
stack.call 3

Will be evaluated 1 -> 2 -> 3 -> 2 -> 1.

# File lib/fluffle/middleware_stack.rb, line 23
def call(callable = nil, &block)
  callable ||= block

  @stack
    .reverse
    .inject(callable) { |previous, middleware|
      ->{ middleware.call(previous) }
    }
    .call
end
push(middleware) click to toggle source
# File lib/fluffle/middleware_stack.rb, line 7
def push(middleware)
  @stack << middleware
  self
end
Also aliased as: <<