Factor out logic for finding gemspec

This commit is contained in:
Conrad Irwin 2012-11-14 00:35:12 -08:00
parent cb0cf76489
commit 0968c5ddce
3 changed files with 19 additions and 17 deletions

View File

@ -11,14 +11,8 @@ class Pry
BANNER
def process(gem)
specs = Gem::Specification.respond_to?(:each) ? Gem::Specification.find_all_by_name(gem) : Gem.source_index.find_name(gem)
spec = specs.sort { |a,b| Gem::Version.new(b.version) <=> Gem::Version.new(a.version) }.first
if spec
Dir.chdir(spec.full_gem_path)
Dir.chdir(gem_spec(gem).full_gem_path)
output.puts(Dir.pwd)
else
raise CommandError, "Gem `#{gem}` not found."
end
end
end
end

View File

@ -12,15 +12,9 @@ class Pry
BANNER
def process(gem)
specs = Gem::Specification.respond_to?(:each) ? Gem::Specification.find_all_by_name(gem) : Gem.source_index.find_name(gem)
spec = specs.sort { |a,b| Gem::Version.new(b.version) <=> Gem::Version.new(a.version) }.first
if spec
Dir.chdir(spec.full_gem_path) do
Dir.chdir(gem_spec(gem).full_gem_path) do
invoke_editor(".", 0, false)
end
else
raise CommandError, "Gem `#{gem}` not found."
end
end
end
end

View File

@ -261,7 +261,21 @@ class Pry
Range.new(a, b)
end
# Get the gem spec object for the given gem
# @param [String] gem name
# @return [Gem::Specification]
def gem_spec(gem)
specs = if Gem::Specification.respond_to?(:each)
Gem::Specification.find_all_by_name(gem)
else
Gem.source_index.find_name(gem)
end
spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.first
spec or raise CommandError, "Gem `#{gem}` not found"
end
end
end
end