From 0968c5ddce08f57feb5e6459f9093613558d7869 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Wed, 14 Nov 2012 00:35:12 -0800 Subject: [PATCH] Factor out logic for finding gemspec --- lib/pry/commands/gem_cd.rb | 10 ++-------- lib/pry/commands/gem_open.rb | 10 ++-------- lib/pry/helpers/command_helpers.rb | 16 +++++++++++++++- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/lib/pry/commands/gem_cd.rb b/lib/pry/commands/gem_cd.rb index fcc0c2e8..a1aa1434 100644 --- a/lib/pry/commands/gem_cd.rb +++ b/lib/pry/commands/gem_cd.rb @@ -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) - output.puts(Dir.pwd) - else - raise CommandError, "Gem `#{gem}` not found." - end + Dir.chdir(gem_spec(gem).full_gem_path) + output.puts(Dir.pwd) end end end diff --git a/lib/pry/commands/gem_open.rb b/lib/pry/commands/gem_open.rb index 5bd29d2f..8f443ff0 100644 --- a/lib/pry/commands/gem_open.rb +++ b/lib/pry/commands/gem_open.rb @@ -12,14 +12,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) do - invoke_editor(".", 0, false) - end - else - raise CommandError, "Gem `#{gem}` not found." + Dir.chdir(gem_spec(gem).full_gem_path) do + invoke_editor(".", 0, false) end end end diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 680b1d8d..2af4e10d 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -261,7 +261,21 @@ class Pry Range.new(a, b) end - 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