Sha256: c073053dcee0ac9c988b9fb32280e6c70b3f9cab43a9003b31a2d914285abc28

Contents?: true

Size: 1.39 KB

Versions: 14

Compression:

Stored size: 1.39 KB

Contents

require "coach/errors"

module Coach
  class MiddlewareValidator
    def initialize(middleware, previous_middlewares = [], already_provided = [])
      @middleware = middleware
      @previous_middlewares = previous_middlewares.clone
      @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, @previous_middlewares, missing_requirements
        )
      end

      @middleware.provided + provided_by_chain
    end

    private

    attr_reader :previous_middlewares

    def missing_requirements
      @middleware.requirements - provided_by_chain
    end

    def provided_by_chain
      @provided_by_chain ||= begin
        initial = [@already_provided, @previous_middlewares]
        middleware_dependencies.reduce(initial) do |(provided, previous), middleware|
          validator = self.class.new(middleware, previous, provided)
          [provided + validator.validated_provides!, previous + [middleware]]
        end.first.flatten.uniq
      end
    end

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

Version data entries

14 entries across 14 versions & 1 rubygems

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