Sha256: df5671c1e611daf81e22d65c4b089f78c23ea54116b9ad21b8755f54c5bef135

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

require "coach/errors"

module Coach
  class MiddlewareValidator
    def initialize(middleware, already_provided = [])
      @middleware = middleware
      @already_provided = already_provided
    end

    # Aggregates provided keys from the given middleware, and all the middleware it uses.
    # Can raise at any level assuming a used middleware is missing a required dependency.
    def validated_provides!
      if missing_requirements.any?
        raise Coach::Errors::MiddlewareDependencyNotMet.new(
          @middleware, missing_requirements
        )
      end

      @middleware.provided + provided_by_chain
    end

    private

    def missing_requirements
      @middleware.requirements - provided_by_chain
    end

    def provided_by_chain
      @provided_by_chain ||=
        middleware_dependencies.reduce(@already_provided) do |provided, middleware|
          provided + self.class.new(middleware, provided).validated_provides!
        end.flatten.uniq
    end

    def middleware_dependencies
      @middleware_dependencies ||= @middleware.middleware_dependencies.map(&:middleware)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
coach-0.2.1 lib/coach/middleware_validator.rb
coach-0.2.0 lib/coach/middleware_validator.rb
coach-0.0.2 lib/coach/middleware_validator.rb
coach-0.0.1 lib/coach/middleware_validator.rb
coach-0.0.0 lib/coach/middleware_validator.rb