Class Middleware::Builder
In: lib/middleware/builder.rb
Parent: Object

This provides a DSL for building up a stack of middlewares.

This code is based heavily off of `Rack::Builder` and `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.

# Usage

Building a middleware stack is very easy:

    app = Middleware::Builder.new do
      use A
      use B
    end

    # Call the middleware
    app.call(7)

Methods

call   delete   flatten   index   insert   insert_after   insert_before   new   replace   stack   to_app   use  

Public Class methods

Initializes the builder. An optional block can be passed which will be evaluated in the context of the instance.

Example:

    Builder.new do
      use A
      use B
    end

@param [Hash] opts Options hash @option opts [Class] :runner_class The class to wrap the middleware stack

  in which knows how to run them.

@yield [] Evaluated in this instance which allows you to use methods

  like {#use} and such.

Public Instance methods

Runs the builder stack with the given environment.

Deletes the given middleware object or index

Returns a mergeable version of the builder. If `use` is called with the return value of this method, then the stack will merge, instead of being treated as a separate single middleware.

Inserts a middleware at the given index or directly before the given middleware object.

Inserts a middleware after the given index or middleware object.

insert_before(index, middleware, *args, &block)

Alias for insert

Replaces the given middlware object or index with the new middleware.

Adds a middleware class to the middleware stack. Any additional args and a block, if given, are saved and passed to the initializer of the middleware.

@param [Class] middleware The middleware class

Protected Instance methods

Returns the numeric index for the given middleware object.

@param [Object] object The item to find the index for @return [Integer]

Returns the current stack of middlewares. You probably won‘t need to use this directly, and it‘s recommended that you don‘t.

@return [Array]

Converts the builder stack to a runnable action sequence.

@return [Object] A callable object

[Validate]