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
- .requires?(provision) ⇒ Boolean
- .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.
54 55 56 57 58 |
# File 'lib/coach/middleware.rb', line 54 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.
49 50 51 |
# File 'lib/coach/middleware.rb', line 49 def config @config end |
#next_middleware ⇒ Object (readonly)
Returns the value of attribute next_middleware.
49 50 51 |
# File 'lib/coach/middleware.rb', line 49 def next_middleware @next_middleware end |
Class Method Details
.middleware_dependencies ⇒ Object
11 12 13 |
# File 'lib/coach/middleware.rb', line 11 def self.middleware_dependencies @middleware_dependencies ||= [] end |
.provided ⇒ Object
15 16 17 |
# File 'lib/coach/middleware.rb', line 15 def self.provided @provided ||= [] end |
.provides(*new_provided) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/coach/middleware.rb', line 19 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
28 29 30 |
# File 'lib/coach/middleware.rb', line 28 def self.provides?(requirement) provided.include?(requirement) end |
.requirements ⇒ Object
32 33 34 |
# File 'lib/coach/middleware.rb', line 32 def self.requirements @requirements ||= [] end |
.requires(*new_requirements) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/coach/middleware.rb', line 36 def self.requires(*new_requirements) requirements.concat(new_requirements) requirements.uniq! new_requirements.each do |requirement| define_method(requirement) { @_context[requirement] } end end |
.requires?(provision) ⇒ Boolean
45 46 47 |
# File 'lib/coach/middleware.rb', line 45 def self.requires?(provision) requirements.include?(provision) end |
.uses(middleware, config = {}) ⇒ Object
7 8 9 |
# File 'lib/coach/middleware.rb', line 7 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.
80 81 82 83 84 85 86 87 |
# File 'lib/coach/middleware.rb', line 80 def instrument proc do ActiveSupport::Notifications.publish("start_middleware.coach", middleware_event) ActiveSupport::Notifications. instrument("finish_middleware.coach", middleware_event) { call } end end |
#log_metadata(**values) ⇒ Object
Adds key-values to metadata, to be published with coach events.
90 91 92 93 |
# File 'lib/coach/middleware.rb', line 90 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.
69 70 71 72 73 74 75 76 77 |
# File 'lib/coach/middleware.rb', line 69 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!
62 63 64 |
# File 'lib/coach/middleware.rb', line 62 def request @_context[:request] end |