Class: Middleware::Builder
- Inherits:
-
Object
- Object
- Middleware::Builder
- Defined in:
- lib/middleware/builder.rb
Overview
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)
Instance Method Summary collapse
-
#call(env = nil) ⇒ Object
Runs the builder stack with the given environment.
-
#delete(index) ⇒ Object
Deletes the given middleware object or index.
-
#flatten ⇒ Object
Returns a mergeable version of the builder.
-
#initialize(opts = nil) { ... } ⇒ Builder
constructor
Initializes the builder.
-
#insert(index, middleware, *args, &block) ⇒ Object
(also: #insert_before)
Inserts a middleware at the given index or directly before the given middleware object.
-
#insert_after(index, middleware, *args, &block) ⇒ Object
Inserts a middleware after the given index or middleware object.
-
#replace(index, middleware, *args, &block) ⇒ Object
Replaces the given middlware object or index with the new middleware.
-
#use(middleware, *args, &block) ⇒ Object
Adds a middleware class to the middleware stack.
Constructor Details
#initialize(opts = nil) { ... } ⇒ Builder
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
35 36 37 38 39 |
# File 'lib/middleware/builder.rb', line 35 def initialize(opts=nil, &block) opts ||= {} @runner_class = opts[:runner_class] || Runner instance_eval(&block) if block_given? end |
Instance Method Details
#call(env = nil) ⇒ Object
Runs the builder stack with the given environment.
101 102 103 |
# File 'lib/middleware/builder.rb', line 101 def call(env=nil) to_app.call(env) end |
#delete(index) ⇒ Object
Deletes the given middleware object or index
95 96 97 98 |
# File 'lib/middleware/builder.rb', line 95 def delete(index) index = self.index(index) unless index.is_a?(Integer) stack.delete_at(index) end |
#flatten ⇒ Object
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.
44 45 46 47 48 |
# File 'lib/middleware/builder.rb', line 44 def flatten lambda do |env| self.call(env) end end |
#insert(index, middleware, *args, &block) ⇒ Object Also known as: insert_before
Inserts a middleware at the given index or directly before the given middleware object.
68 69 70 71 |
# File 'lib/middleware/builder.rb', line 68 def insert(index, middleware, *args, &block) index = self.index(index) unless index.is_a?(Integer) stack.insert(index, [middleware, args, block]) end |
#insert_after(index, middleware, *args, &block) ⇒ Object
Inserts a middleware after the given index or middleware object.
76 77 78 79 80 |
# File 'lib/middleware/builder.rb', line 76 def insert_after(index, middleware, *args, &block) index = self.index(index) unless index.is_a?(Integer) raise "no such middleware to insert after: #{index.inspect}" unless index insert(index + 1, middleware, *args, &block) end |
#replace(index, middleware, *args, &block) ⇒ Object
Replaces the given middlware object or index with the new middleware.
84 85 86 87 88 89 90 91 92 |
# File 'lib/middleware/builder.rb', line 84 def replace(index, middleware, *args, &block) if index.is_a?(Integer) delete(index) insert(index, middleware, *args, &block) else insert_before(index, middleware, *args, &block) delete(index) end end |
#use(middleware, *args, &block) ⇒ Object
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.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/middleware/builder.rb', line 55 def use(middleware, *args, &block) if middleware.kind_of?(Builder) # Merge in the other builder's stack into our own self.stack.concat(middleware.stack) else self.stack << [middleware, args, block] end self end |