Refactor Requirement.expand

This commit is contained in:
Jack Nagel 2013-06-03 15:08:46 -05:00
parent ceb01c3124
commit 3937d2bb84

View File

@ -120,12 +120,13 @@ class Requirement
end end
end end
class << self
# Expand the requirements of dependent recursively, optionally yielding # Expand the requirements of dependent recursively, optionally yielding
# [dependent, req] pairs to allow callers to apply arbitrary filters to # [dependent, req] pairs to allow callers to apply arbitrary filters to
# the list. # the list.
# The default filter, which is applied when a block is not given, omits # The default filter, which is applied when a block is not given, omits
# optionals and recommendeds based on what the dependent has asked for. # optionals and recommendeds based on what the dependent has asked for.
def self.expand(dependent, &block) def expand(dependent, &block)
reqs = ComparableSet.new reqs = ComparableSet.new
formulae = dependent.recursive_dependencies.map(&:to_formula) formulae = dependent.recursive_dependencies.map(&:to_formula)
@ -133,26 +134,18 @@ class Requirement
formulae.map(&:requirements).each do |requirements| formulae.map(&:requirements).each do |requirements|
requirements.each do |req| requirements.each do |req|
prune = catch(:prune) do if prune?(dependent, req, &block)
if block_given? next
yield dependent, req
elsif req.optional? || req.recommended?
Requirement.prune unless dependent.build.with?(req.name)
end
end
next if prune
# TODO: Do this in a cleaner way, perhaps with another type of # TODO: Do this in a cleaner way, perhaps with another type of
# dependency type. # dependency type.
if req.class.default_formula elsif req.class.default_formula
dependent.class.depends_on(req.class.default_formula) dependent.class.depends_on(req.class.default_formula)
next next
end else
reqs << req reqs << req
end end
end end
end
# We special case handling of X11Dependency and its subclasses to # We special case handling of X11Dependency and its subclasses to
# ensure the correct dependencies are present in the final list. # ensure the correct dependencies are present in the final list.
@ -172,8 +165,19 @@ class Requirement
reqs reqs
end end
def prune?(dependent, req, &block)
catch(:prune) do
if block_given?
yield dependent, req
elsif req.optional? || req.recommended?
prune unless dependent.build.with?(req.name)
end
end
end
# Used to prune requirements when calling expand with a block. # Used to prune requirements when calling expand with a block.
def self.prune def prune
throw(:prune, true) throw(:prune, true)
end end
end
end end