Module: Pancake::Middleware
Overview
Provides a mixin to use on any class to give it middleware management capabilities. This module provides a rich featureset for defining a middleware stack.
Middlware can be set before, or after other middleware, can be tagged / named, and can be declared to only be active in certain types of stacks.
Defined Under Namespace
Classes: StackMiddleware
Class Method Summary collapse
-
.build(app, mwares) ⇒ Object
Build a middleware stack given an application and some middleware classes.
-
.extended(base) ⇒ Object
When extending a base class with the Pancake::Middleware, an inner class StackMiddleware is setup on the base class.
Instance Method Summary collapse
-
#middlewares(*labels) ⇒ Array<StackMiddleware>
An array of middleware specifications in the order they should be used to wrap the application.
-
#stack(name = nil, opts = {}) ⇒ Object
Useful for adding additional information into your middleware stack definition.
-
#use(middleware, *_args) { ... } ⇒ Object
Adds middleware to the current stack definition.
Class Method Details
.build(app, mwares) ⇒ Object
Build a middleware stack given an application and some middleware classes
StackMiddleware instances where each instance
defines a middleware to use in constructing the stack
42 43 44 45 46 |
# File 'lib/pancake/middleware.rb', line 42 def self.build(app, mwares) mwares.reverse.inject(app) do |a, m| m.middleware.new(a, *m.args, &m.block) end end |
.extended(base) ⇒ Object
When extending a base class with the Pancake::Middleware, an inner class StackMiddleware is setup on the base class. This inner class is where all the inforamation is stored on the stack to be defined The inner StackMiddleware class is also set to be inherited with the base class (and all children classes) So that each class gets its own copy and may maintain the base stack from the parent, but edit it in the child.
16 17 18 19 20 21 22 23 24 |
# File 'lib/pancake/middleware.rb', line 16 def self.extended(base) base.class_eval <<-RUBY class StackMiddleware < Pancake::Middleware::StackMiddleware; end RUBY if base.is_a?(Class) base.inheritable_inner_classes :StackMiddleware end super end |
Instance Method Details
#middlewares(*labels) ⇒ Array<StackMiddleware>
Returns An array of middleware specifications in the order they should be used to wrap the application.
62 63 64 65 |
# File 'lib/pancake/middleware.rb', line 62 def middlewares(*labels) labels = labels.flatten self::StackMiddleware.middlewares(*labels) end |
#stack(name = nil, opts = {}) ⇒ Object
Useful for adding additional information into your middleware stack definition
This middleware will be named MyMiddleware, and can be specified with (:before | :after) => MyMiddleware
This middleware will be named :foo and can be specified with (:before | :after) => :foo
This middleware will be named :foo and will be run before the middleware named :bar If :bar is not run, :foo will not be run either
This middleware will be named :foo and will be run after the middleware named :bar If :bar is not run, :foo will not be run either
This middleware will only be run when pancake is set with the :demo, :production or :staging labels
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/pancake/middleware.rb', line 117 def stack(name = nil, opts = {}) if self::StackMiddleware._mwares[name] && mw = self::StackMiddleware._mwares[name] unless mw.stack == self mw = self::StackMiddleware._mwares[name] = self::StackMiddleware._mwares[name].dup end mw else self::StackMiddleware.new(name, self, opts) end end |
#use(middleware, *_args) { ... } ⇒ Object
Adds middleware to the current stack definition
145 146 147 |
# File 'lib/pancake/middleware.rb', line 145 def use(middleware, *_args, &block) stack(middleware).use(middleware, *_args, &block) end |