Skip to content

Commit

Permalink
refactor to eliminate conditional in self.gemspec_stubs_in
Browse files Browse the repository at this point in the history
This change is going to allow us more easily use more shared strings in the
future.
  • Loading branch information
tenderlove committed Nov 5, 2015
1 parent 98b552f commit d35fc5e
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -740,24 +740,37 @@ def self.each_gemspec(dirs) # :nodoc:
end

def self.gemspec_stubs_in dir, pattern
base_dir = File.dirname dir
Dir[File.join(dir, pattern)].map { |path|
if dir == default_specifications_dir
Gem::StubSpecification.default_gemspec_stub(path, Gem.default_dir)
else
Gem::StubSpecification.gemspec_stub(path, base_dir)
end
}.select(&:valid?)
Dir[File.join(dir, pattern)].map { |path| yield path }.select(&:valid?)
end
private_class_method :gemspec_stubs_in

def self.default_stubs pattern
gemspec_stubs_in(default_specifications_dir, pattern) do |path|
Gem::StubSpecification.default_gemspec_stub(path, Gem.default_dir)
end
end
private_class_method :default_stubs

def self.installed_stubs dirs, pattern
map_stubs(dirs, pattern) do |path, base_dir|
Gem::StubSpecification.gemspec_stub(path, base_dir)
end
end
private_class_method :installed_stubs

if [].respond_to? :flat_map
def self.map_stubs(dirs, pattern) # :nodoc:
dirs.flat_map { |dir| gemspec_stubs_in(dir, pattern) }
dirs.flat_map { |dir|
base_dir = File.dirname dir
gemspec_stubs_in(dir, pattern) { |path| yield path, base_dir }
}
end
else # FIXME: remove when 1.8 is dropped
def self.map_stubs(dirs, pattern) # :nodoc:
dirs.map { |dir| gemspec_stubs_in(dir, pattern) }.flatten 1
dirs.map { |dir|
base_dir = File.dirname dir
gemspec_stubs_in(dir, pattern) { |path| yield path, base_dir }
}.flatten 1
end
end
private_class_method :map_stubs
Expand Down Expand Up @@ -804,7 +817,8 @@ def self.each_spec(dirs) # :nodoc:

def self.stubs
@@stubs ||= begin
stubs = map_stubs([default_specifications_dir] + dirs, "*.gemspec")
pattern = "*.gemspec"
stubs = default_stubs(pattern) + installed_stubs(dirs, pattern)
stubs = uniq_by(stubs) { |stub| stub.full_name }

_resort!(stubs)
Expand All @@ -822,7 +836,8 @@ def self.stubs_for name
if @@stubs || @@stubs_by_name[name]
@@stubs_by_name[name] || []
else
stubs = map_stubs([default_specifications_dir] + dirs, "#{name}-*.gemspec")
pattern = "#{name}-*.gemspec"
stubs = default_stubs(pattern) + installed_stubs(dirs, pattern)
stubs = uniq_by(stubs) { |stub| stub.full_name }.group_by(&:name)
stubs.each_value { |v| sort_by!(v) { |i| i.version } }

Expand Down

0 comments on commit d35fc5e

Please sign in to comment.