Class: Pancake::Middleware::StackMiddleware
- Defined in:
- lib/pancake/middleware.rb
Overview
StackMiddleware manages the definition of the middleware stack for a given class. It’s instances are responsible for the definition of a single piece of middleware, and the class is responsible for specifying the full stack for a given class.
When Pancake::Middleware extends a class, an inner class is created in that class called StackMiddleware. That StackMiddleware class inherits from Pancake::Middleware::StackMiddleware.
This is then set is an inheritable inner class on the extended class, such that when it is inherited, the StackMiddleware class is inherited to an inner class of the same name on the child.
Instance Attribute Summary collapse
- #args ⇒ Object private
- #block ⇒ Object private
- #middleware ⇒ Object readonly private
- #name ⇒ Object readonly private
- #options ⇒ Object private
- #stack ⇒ Object private
Class Method Summary collapse
-
.[](name) ⇒ StackMiddleware
Provides access to a named middleware.
-
.map_middleware(name) ⇒ Array<StackMiddleware>
private
Map the middleware for a given <name>ed middleware.
-
.middlewares ⇒ Array<StackMiddleware>
Get the middleware list for this StackMiddleware.
-
.reset! ⇒ Object
Resets this stack middlware.
- .use(mware, *_args, &block) ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
Provides access to a named middleware.
-
#delete! ⇒ Object
Delete this middleware from the current stack.
- #dup ⇒ Object private
-
#initialize(name, stack, options = {}) ⇒ StackMiddleware
constructor
private
A new instance of StackMiddleware.
-
#use(mware, *_args) { ... } ⇒ Object
Specify the actual middleware definition to use.
Constructor Details
#initialize(name, stack, options = {}) ⇒ StackMiddleware
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of StackMiddleware.
240 241 242 |
# File 'lib/pancake/middleware.rb', line 240 def initialize(name, stack, = {}) @name, @stack, @options = name, stack, end |
Instance Attribute Details
#args ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 |
# File 'lib/pancake/middleware.rb', line 157 def args @args end |
#block ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 |
# File 'lib/pancake/middleware.rb', line 157 def block @block end |
#middleware ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 156 157 |
# File 'lib/pancake/middleware.rb', line 155 def middleware @middleware end |
#name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 156 157 |
# File 'lib/pancake/middleware.rb', line 155 def name @name end |
#options ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 |
# File 'lib/pancake/middleware.rb', line 157 def @options end |
#stack ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
157 158 159 |
# File 'lib/pancake/middleware.rb', line 157 def stack @stack end |
Class Method Details
.[](name) ⇒ StackMiddleware
Provides access to a named middleware
217 218 219 |
# File 'lib/pancake/middleware.rb', line 217 def [](name) _mwares[name] end |
.map_middleware(name) ⇒ Array<StackMiddleware>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Map the middleware for a given <name>ed middleware. Applies the before and after groups of middlewares
198 199 200 201 202 203 204 205 206 |
# File 'lib/pancake/middleware.rb', line 198 def map_middleware(name) result = [] _before[name] ||= [] _after[name] ||= [] result << _before[name].map{|n| map_middleware(n)} result << _mwares[name] result << _after[name].map{|n| map_middleware(n)} result.flatten end |
.middlewares ⇒ Array<StackMiddleware>
Get the middleware list for this StackMiddleware
183 184 185 186 187 |
# File 'lib/pancake/middleware.rb', line 183 def middlewares _central_mwares.map do |name| map_middleware(name) end.flatten end |
.reset! ⇒ Object
Resets this stack middlware. Useful for specs
165 166 167 168 169 170 |
# File 'lib/pancake/middleware.rb', line 165 def reset! _central_mwares.clear _mwares.clear _before.clear _after.clear end |
.use(mware, *_args, &block) ⇒ Object
160 161 162 |
# File 'lib/pancake/middleware.rb', line 160 def use(mware, *_args, &block) new(mware).use(mware, *_args, &block) end |
Instance Method Details
#[](name) ⇒ Object
Provides access to a named middleware
227 228 229 |
# File 'lib/pancake/middleware.rb', line 227 def [](name) self.class._mwares[name] end |
#delete! ⇒ Object
Delete this middleware from the current stack
249 250 251 252 253 254 255 |
# File 'lib/pancake/middleware.rb', line 249 def delete! self.class._mwares.delete(name) self.class._before.delete(name) self.class._after.delete(name) self.class._central_mwares.delete(name) self end |
#dup ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
286 287 288 289 290 291 |
# File 'lib/pancake/middleware.rb', line 286 def dup result = super result.args = result.args.map{|element| element.dup} result. = result..dup result end |
#use(mware, *_args) { ... } ⇒ Object
Specify the actual middleware definition to use
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/pancake/middleware.rb', line 267 def use(mware, *_args, &block) @middleware, @args, @block = mware, _args, block @name = @middleware if name.nil? if [:before] raise "#{[:before].inspect} middleware is not defined for this stack" unless self.class._mwares.keys.include?([:before]) self.class._before[[:before]] ||= [] self.class._before[[:before]] << name elsif [:after] raise "#{[:after].inspect} middleware is not defined for this stack" unless self.class._mwares.keys.include?([:after]) self.class._after[[:after]] ||= [] self.class._after[[:after]] << name else self.class._central_mwares << name unless self.class._central_mwares.include?(name) end self.class._mwares[name] = self self end |