1 # frozen_string_literal: true
13 attr_reader :specs, :duplicates, :sources
14 protected :specs, :duplicates
26 def initialize_copy(o)
27 @sources = o.sources.dup
32 o.specs.each do |name, hash|
33 @specs[name] = hash.dup
35 o.duplicates.each do |name, array|
36 @duplicates[name] = array.dup
41 "#<#{self.class}:0x#{object_id} sources=#{sources.map(&:inspect)} specs.size=#{specs.size}>"
49 def search_all(name, &blk)
50 return enum_for(:search_all, name) unless blk
51 specs_by_name(name).each(&blk)
52 @duplicates[name]&.each(&blk)
53 @sources.each {|source| source.search_all(name, &blk) }
56 # Search this index's specs, and any source indexes that this index knows
57 # about, returning all of the results.
59 results = local_search(query)
60 return results unless @sources.any?
62 @sources.each do |source|
63 results = safe_concat(results, source.search(query))
65 results.uniq!(&:full_name) unless results.empty? # avoid modifying frozen EMPTY_SEARCH
69 alias_method :[], :search
71 def local_search(query)
73 when Gem::Specification, RemoteSpecification, LazySpecification, EndpointSpecification then search_by_spec(query)
74 when String then specs_by_name(query)
75 when Array then specs_by_name_and_version(*query)
77 raise "You can't search for a #{query.inspect}."
82 (@specs[spec.name] ||= {}).store(spec.full_name, spec)
84 alias_method :<<, :add
87 return enum_for(:each) unless blk
88 specs.values.each do |spec_sets|
89 spec_sets.values.each(&blk)
91 sources.each {|s| s.each(&blk) }
96 names = specs.keys + sources.map(&:spec_names)
101 def unmet_dependency_names
102 dependency_names.select do |name|
110 spec.dependencies.each do |dep|
111 next if dep.type == :development
118 # Combines indexes proritizing existing specs, like `Hash#reverse_merge!`
119 # Duplicate specs found in `other` are stored in `@duplicates`.
123 exist?(spec) ? add_duplicate(spec) : add(spec)
128 # Combines indexes proritizing specs from `other`, like `Hash#merge!`
129 # Duplicate specs found in `self` are saved in `@duplicates`.
133 if existing = find_by_spec(spec)
134 add_duplicate(existing)
142 @sources.inject(@specs.size) do |size, source|
147 # Whether all the specs in self are in other
150 other_spec = other[spec].first
151 other_spec && dependencies_eql?(spec, other_spec) && spec.source == other_spec.source
155 def dependencies_eql?(spec, other_spec)
156 deps = spec.dependencies.select {|d| d.type != :development }
157 other_deps = other_spec.dependencies.select {|d| d.type != :development }
158 deps.sort == other_deps.sort
161 def add_source(index)
162 raise ArgumentError, "Source must be an index, not #{index.class}" unless index.is_a?(Index)
164 @sources.uniq! # need to use uniq! here instead of checking for the item before adding
169 def safe_concat(a, b)
175 def add_duplicate(spec)
176 (@duplicates[spec.name] ||= []) << spec
179 def specs_by_name_and_version(name, version)
180 results = @specs[name]&.values
181 return EMPTY_SEARCH unless results
182 results.select! {|spec| spec.version == version }
186 def specs_by_name(name)
187 @specs[name]&.values || EMPTY_SEARCH
190 EMPTY_SEARCH = [].freeze
192 def search_by_spec(spec)
193 spec = find_by_spec(spec)
194 spec ? [spec] : EMPTY_SEARCH
197 def find_by_spec(spec)
198 @specs[spec.name]&.fetch(spec.full_name, nil)
202 @specs[spec.name]&.key?(spec.full_name)