Sha256: cb821ccc829070ff0baecaff252f2a99bc3b6a3b309d6953fdcfe435aad1696b

Contents?: true

Size: 1.13 KB

Versions: 12

Compression:

Stored size: 1.13 KB

Contents

require "coach/errors"
require "coach/middleware_validator"

module Coach
  class MiddlewareItem
    attr_accessor :middleware, :parent

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

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

    # Runs validation against the middleware chain, raising if any unmet dependencies are
    # discovered.
    def validate!
      MiddlewareValidator.new(middleware).validated_provides!
    end

    # Assigns the parent for this middleware, allowing config inheritance
    def set_parent(parent)
      @parent = parent

      self
    end

    # 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.
    def config
      @config ||= lambda_config? ? @config_value.call(parent.config) : @config_value.clone
    end

    private

    def lambda_config?
      @config_value.respond_to?(:call)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
coach-0.5.2 lib/coach/middleware_item.rb
coach-0.5.1 lib/coach/middleware_item.rb
coach-0.5.0 lib/coach/middleware_item.rb
coach-0.4.6 lib/coach/middleware_item.rb
coach-0.4.5 lib/coach/middleware_item.rb
coach-0.4.4 lib/coach/middleware_item.rb
coach-0.4.3 lib/coach/middleware_item.rb
coach-0.4.2 lib/coach/middleware_item.rb
coach-0.4.1 lib/coach/middleware_item.rb
coach-0.4.0 lib/coach/middleware_item.rb
coach-0.3.0 lib/coach/middleware_item.rb
coach-0.2.3 lib/coach/middleware_item.rb