lib/coach/middleware_validator.rb in coach-0.2.1 vs lib/coach/middleware_validator.rb in coach-0.2.2
- old
+ new
@@ -1,36 +1,42 @@
require "coach/errors"
module Coach
class MiddlewareValidator
- def initialize(middleware, already_provided = [])
+ 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, missing_requirements
+ @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 ||=
- middleware_dependencies.reduce(@already_provided) do |provided, middleware|
- provided + self.class.new(middleware, provided).validated_provides!
- end.flatten.uniq
+ @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