2013-02-27 18:20:57 -05:00
|
|
|
class Gem::AvailableSet
|
|
|
|
Tuple = Struct.new(:spec, :source)
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def initialize
|
|
|
|
@set = []
|
|
|
|
@sorted = nil
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
attr_reader :set
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def add(spec, source)
|
|
|
|
@set << Tuple.new(spec, source)
|
|
|
|
@sorted = nil
|
|
|
|
self
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def <<(o)
|
|
|
|
case o
|
|
|
|
when Gem::AvailableSet
|
|
|
|
s = o.set
|
|
|
|
when Array
|
|
|
|
s = o.map do |sp,so|
|
|
|
|
if !sp.kind_of?(Gem::Specification) or !so.kind_of?(Gem::Source)
|
|
|
|
raise TypeError, "Array must be in [[spec, source], ...] form"
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-02-27 18:20:57 -05:00
|
|
|
|
|
|
|
Tuple.new(sp,so)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-02-27 18:20:57 -05:00
|
|
|
else
|
|
|
|
raise TypeError, "must be a Gem::AvailableSet"
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
@set += s
|
|
|
|
@sorted = nil
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
self
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def empty?
|
|
|
|
@set.empty?
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def all_specs
|
|
|
|
@set.map { |t| t.spec }
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def match_platform!
|
|
|
|
@set.reject! { |t| !Gem::Platform.match(t.spec.platform) }
|
|
|
|
@sorted = nil
|
|
|
|
self
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def sorted
|
|
|
|
@sorted ||= @set.sort do |a,b|
|
|
|
|
i = b.spec <=> a.spec
|
|
|
|
i != 0 ? i : (a.source <=> b.source)
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
2013-02-27 18:20:57 -05:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def size
|
|
|
|
@set.size
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def source_for(spec)
|
|
|
|
f = @set.find { |t| t.spec == spec }
|
|
|
|
f.source
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def pick_best!
|
|
|
|
return self if empty?
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
@set = [sorted.first]
|
|
|
|
@sorted = nil
|
|
|
|
self
|
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
def remove_installed!(dep)
|
|
|
|
@set.reject! do |t|
|
|
|
|
# already locally installed
|
|
|
|
Gem::Specification.any? do |installed_spec|
|
|
|
|
dep.name == installed_spec.name and
|
|
|
|
dep.requirement.satisfied_by? installed_spec.version
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-27 18:20:57 -05:00
|
|
|
@sorted = nil
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def inject_into_list(dep_list)
|
|
|
|
@set.each { |t| dep_list.add t.spec }
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
end
|