Sha256: 9556af6ef2c69ca78802555d1dff826cba1a7eb8a7f2c4baa95a484a857c16bc

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 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
    # rubocop:disable Naming/AccessorMethodName
    def set_parent(parent)
      @parent = parent

      self
    end
    # rubocop:enable Naming/AccessorMethodName

    # 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

1 entries across 1 versions & 1 rubygems

Version Path
coach-1.0.0 lib/coach/middleware_item.rb