Class: ConvenientService::Dependencies::Extractions::RubyMiddleware::Middleware::Builder
- Inherits:
-
Object
- Object
- ConvenientService::Dependencies::Extractions::RubyMiddleware::Middleware::Builder
- Defined in:
- lib/convenient_service/dependencies/extractions/ruby_middleware/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 |b|
b.use A
b.use B
end
# Call the middleware
app.call(7)
Direct Known Subclasses
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.
- #inject_logger(logger) ⇒ Object
-
#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.
-
#insert_after_each(middleware, *args, &block) ⇒ Object
Inserts a middleware after each middleware object.
-
#insert_before_each(middleware, *args, &block) ⇒ Object
Inserts a middleware before each middleware object.
- #inspect ⇒ Object
-
#name ⇒ Object
Returns the name of the current middleware.
-
#replace(index, middleware, *args, &block) ⇒ Object
Replaces the given middleware 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
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 56 def initialize(opts = nil, &block) opts ||= {} @runner_class = opts.fetch(:runner_class, Runner) @middleware_name = opts.fetch(:name, 'Middleware') if block_given? if block.arity == 1 yield self else instance_eval(&block) end end end |
Instance Method Details
#call(env = nil) ⇒ Object
Runs the builder stack with the given environment.
145 146 147 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 145 def call(env = nil) to_app.call(env) end |
#delete(index) ⇒ Object
Deletes the given middleware object or index
139 140 141 142 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 139 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.
78 79 80 81 82 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 78 def flatten lambda do |env| call(env) end end |
#inject_logger(logger) ⇒ Object
156 157 158 159 160 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 156 def inject_logger logger insert_before_each Middleware::Logger, logger, name self 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.
101 102 103 104 105 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 101 def insert(index, middleware, *args, &block) index = self.index(index) unless index.is_a?(Integer) fail "no such middleware to insert before: #{index.inspect}" unless index stack.insert(index, [middleware, args, block]) end |
#insert_after(index, middleware, *args, &block) ⇒ Object
Inserts a middleware after the given index or middleware object.
110 111 112 113 114 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 110 def insert_after(index, middleware, *args, &block) index = self.index(index) unless index.is_a?(Integer) fail "no such middleware to insert after: #{index.inspect}" unless index insert(index + 1, middleware, *args, &block) end |
#insert_after_each(middleware, *args, &block) ⇒ Object
Inserts a middleware after each middleware object
124 125 126 127 128 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 124 def insert_after_each(middleware, *args, &block) self.stack = stack.reduce([]) do |carry, item| carry.push(item, [middleware, args, block]) end end |
#insert_before_each(middleware, *args, &block) ⇒ Object
Inserts a middleware before each middleware object
117 118 119 120 121 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 117 def insert_before_each(middleware, *args, &block) self.stack = stack.reduce([]) do |carry, item| carry.push([middleware, args, block], item) end end |
#inspect ⇒ Object
149 150 151 152 153 154 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 149 def inspect name+'[' + stack.map do |middleware| name = middleware[0].is_a?(Proc) ? 'Proc' : middleware[0].name "#{name}(#{middleware[1].join(', ')})" end.join(', ') + ']' end |
#name ⇒ Object
Returns the name of the current middleware
71 72 73 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 71 def name @middleware_name end |
#replace(index, middleware, *args, &block) ⇒ Object
Replaces the given middleware object or index with the new middleware.
131 132 133 134 135 136 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 131 def replace(index, middleware, *args, &block) index = self.index index unless index.is_a? Integer delete(index) insert(index, middleware, *args, &block) 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.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/convenient_service/dependencies/extractions/ruby_middleware/middleware/builder.rb', line 89 def use(middleware, *args, &block) if middleware.is_a?(Builder) # Merge in the other builder's stack into our own stack.concat(middleware.stack) else stack << [middleware, args, block] end self end |