Class: Coach::Middleware
- Inherits:
-
Object
- Object
- Coach::Middleware
- Defined in:
- lib/coach/middleware.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#next_middleware ⇒ Object
readonly
Returns the value of attribute next_middleware.
Class Method Summary collapse
- .middleware_dependencies ⇒ Object
- .provided ⇒ Object
- .provides(*new_provided) ⇒ Object
- .provides?(requirement) ⇒ Boolean
- .requirements ⇒ Object
- .requires(*new_requirements) ⇒ Object
- .uses(middleware, config = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(context, next_middleware = nil, config = {}) ⇒ Middleware
constructor
Middleware gets access to a shared context, which is populated by other middleware futher up the stack, a reference to the next middleware in the stack, and a config object.
-
#instrument ⇒ Object
Use ActiveSupport to instrument the execution of the subsequent chain.
-
#log_metadata(**values) ⇒ Object
Adds key-values to metadata, to be published with coach events.
-
#provide(args) ⇒ Object
Make values available to middleware further down the stack.
-
#request ⇒ Object
‘request` is always present in context, and we want to give every middleware access to it by default as it’s always present and often used!.
Constructor Details
#initialize(context, next_middleware = nil, config = {}) ⇒ Middleware
Middleware gets access to a shared context, which is populated by other middleware futher up the stack, a reference to the next middleware in the stack, and a config object.
48 49 50 51 52 |
# File 'lib/coach/middleware.rb', line 48 def initialize(context, next_middleware = nil, config = {}) @_context = context @next_middleware = next_middleware @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
43 44 45 |
# File 'lib/coach/middleware.rb', line 43 def config @config end |
#next_middleware ⇒ Object (readonly)
Returns the value of attribute next_middleware.
43 44 45 |
# File 'lib/coach/middleware.rb', line 43 def next_middleware @next_middleware end |
Class Method Details
.middleware_dependencies ⇒ Object
9 10 11 |
# File 'lib/coach/middleware.rb', line 9 def self.middleware_dependencies @middleware_dependencies ||= [] end |
.provided ⇒ Object
13 14 15 |
# File 'lib/coach/middleware.rb', line 13 def self.provided @provided ||= [] end |
.provides(*new_provided) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/coach/middleware.rb', line 17 def self.provides(*new_provided) if new_provided.include?(:_metadata) raise 'Cannot provide :_metadata, Coach uses this internally!' end provided.concat(new_provided) provided.uniq! end |
.provides?(requirement) ⇒ Boolean
26 27 28 |
# File 'lib/coach/middleware.rb', line 26 def self.provides?(requirement) provided.include?(requirement) end |
.requirements ⇒ Object
30 31 32 |
# File 'lib/coach/middleware.rb', line 30 def self.requirements @requirements ||= [] end |
.requires(*new_requirements) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/coach/middleware.rb', line 34 def self.requires(*new_requirements) requirements.concat(new_requirements) requirements.uniq! new_requirements.each do |requirement| define_method(requirement) { @_context[requirement] } end end |
.uses(middleware, config = {}) ⇒ Object
5 6 7 |
# File 'lib/coach/middleware.rb', line 5 def self.uses(middleware, config = {}) middleware_dependencies << MiddlewareItem.new(middleware, config) end |
Instance Method Details
#instrument ⇒ Object
Use ActiveSupport to instrument the execution of the subsequent chain.
74 75 76 77 78 79 80 81 |
# File 'lib/coach/middleware.rb', line 74 def instrument proc do ActiveSupport::Notifications. publish('coach.middleware.start', middleware_event) ActiveSupport::Notifications. instrument('coach.middleware.finish', middleware_event) { call } end end |
#log_metadata(**values) ⇒ Object
Adds key-values to metadata, to be published with coach events.
84 85 86 87 |
# File 'lib/coach/middleware.rb', line 84 def (**values) @_context[:_metadata] ||= {} @_context[:_metadata].merge!(values) end |
#provide(args) ⇒ Object
Make values available to middleware further down the stack. Accepts a hash of name => value pairs. Names must have been declared by calling ‘provides` on the class.
63 64 65 66 67 68 69 70 71 |
# File 'lib/coach/middleware.rb', line 63 def provide(args) args.each do |name, value| unless self.class.provides?(name) raise NameError, "#{self.class} does not provide #{name}" end @_context[name] = value end end |
#request ⇒ Object
‘request` is always present in context, and we want to give every middleware access to it by default as it’s always present and often used!
56 57 58 |
# File 'lib/coach/middleware.rb', line 56 def request @_context[:request] end |