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, *labels) ⇒ Array<StackMiddleware>
private
Map the middleware for a given <name>ed middleware.
-
.middlewares(*labels) ⇒ Array<StackMiddleware>
Get the middleware list for this StackMiddleware for the given labels.
-
.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.
-
#use_for_labels?(*labels) ⇒ Boolean
private
Checks if this middleware definition should be included from the labels given.
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.
279 280 281 282 |
# File 'lib/pancake/middleware.rb', line 279 def initialize(name, stack, = {}) @name, @stack, @options = name, stack, @options[:labels] ||= [:any] 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.
174 175 176 |
# File 'lib/pancake/middleware.rb', line 174 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.
174 175 176 |
# File 'lib/pancake/middleware.rb', line 174 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.
172 173 174 |
# File 'lib/pancake/middleware.rb', line 172 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.
172 173 174 |
# File 'lib/pancake/middleware.rb', line 172 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.
174 175 176 |
# File 'lib/pancake/middleware.rb', line 174 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.
174 175 176 |
# File 'lib/pancake/middleware.rb', line 174 def stack @stack end |
Class Method Details
.[](name) ⇒ StackMiddleware
Provides access to a named middleware
254 255 256 |
# File 'lib/pancake/middleware.rb', line 254 def [](name) _mwares[name] end |
.map_middleware(name, *labels) ⇒ 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
Constructs the middleware list based on the middleware named :foo, including all :before, and :after groups
232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/pancake/middleware.rb', line 232 def map_middleware(name, *labels) result = [] _before[name] ||= [] _after[name] ||= [] if _mwares[name] && _mwares[name].use_for_labels?(*labels) result << _before[name].map{|n| map_middleware(n)} result << _mwares[name] result << _after[name].map{|n| map_middleware(n)} result.flatten end result end |
.middlewares(*labels) ⇒ Array<StackMiddleware>
Get the middleware list for this StackMiddleware for the given labels
This will include all defined middlewares in the given stack
210 211 212 213 214 |
# File 'lib/pancake/middleware.rb', line 210 def middlewares(*labels) _central_mwares.map do |name| map_middleware(name, *labels) end.flatten end |
.reset! ⇒ Object
Resets this stack middlware. Useful for specs
182 183 184 185 186 187 |
# File 'lib/pancake/middleware.rb', line 182 def reset! _central_mwares.clear _mwares.clear _before.clear _after.clear end |
.use(mware, *_args, &block) ⇒ Object
177 178 179 |
# File 'lib/pancake/middleware.rb', line 177 def use(mware, *_args, &block) new(mware).use(mware, *_args, &block) end |
Instance Method Details
#[](name) ⇒ Object
Provides access to a named middleware
264 265 266 |
# File 'lib/pancake/middleware.rb', line 264 def [](name) self.class._mwares[name] end |
#delete! ⇒ Object
Delete this middleware from the current stack
289 290 291 292 293 294 295 |
# File 'lib/pancake/middleware.rb', line 289 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.
339 340 341 342 343 344 |
# File 'lib/pancake/middleware.rb', line 339 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
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/pancake/middleware.rb', line 307 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 |
#use_for_labels?(*labels) ⇒ Boolean
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.
Checks if this middleware definition should be included from the labels given
333 334 335 336 |
# File 'lib/pancake/middleware.rb', line 333 def use_for_labels?(*labels) return true if labels.empty? || [:labels].nil? || [:labels].include?(:any) !([:labels] & labels).empty? end |