Class: Coach::MiddlewareItem

Inherits:
Object
  • Object
show all
Defined in:
lib/coach/middleware_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(middleware, config = {}) ⇒ MiddlewareItem

Returns a new instance of MiddlewareItem.



10
11
12
13
# File 'lib/coach/middleware_item.rb', line 10

def initialize(middleware, config = {})
  @middleware = middleware
  @config_value = config
end

Instance Attribute Details

#middlewareObject

Returns the value of attribute middleware.



8
9
10
# File 'lib/coach/middleware_item.rb', line 8

def middleware
  @middleware
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/coach/middleware_item.rb', line 8

def parent
  @parent
end

Instance Method Details

#build_middleware(context, successor) ⇒ Object



15
16
17
18
19
20
# File 'lib/coach/middleware_item.rb', line 15

def build_middleware(context, successor)
  @middleware.
    new(context,
        successor&.instrument,
        config)
end

#configObject

Generates config by either cloning our given config (if it’s a hash) else if a lambda value, then will compute the config by calling the lambda with this middlewares parent config.



40
41
42
# File 'lib/coach/middleware_item.rb', line 40

def config
  @config ||= lambda_config? ? @config_value.call(parent.config) : @config_value.clone
end

#set_parent(parent) ⇒ Object

Assigns the parent for this middleware, allowing config inheritance rubocop:disable Naming/AccessorMethodName



30
31
32
33
34
# File 'lib/coach/middleware_item.rb', line 30

def set_parent(parent)
  @parent = parent

  self
end

#validate!Object

Runs validation against the middleware chain, raising if any unmet dependencies are discovered.



24
25
26
# File 'lib/coach/middleware_item.rb', line 24

def validate!
  MiddlewareValidator.new(middleware).validated_provides!
end