summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConrad Irwin <conrad.irwin@gmail.com>2012-11-14 00:35:12 -0800
committerConrad Irwin <conrad.irwin@gmail.com>2012-11-14 01:50:13 -0800
commit0968c5ddce08f57feb5e6459f9093613558d7869 (patch)
tree439196600eae299be9deff0aff26de513f2bac5e
parentcb0cf76489774ac57947790bf0eec4403e17b45e (diff)
downloadpry-0968c5ddce08f57feb5e6459f9093613558d7869.tar.gz
Factor out logic for finding gemspec
-rw-r--r--lib/pry/commands/gem_cd.rb10
-rw-r--r--lib/pry/commands/gem_open.rb10
-rw-r--r--lib/pry/helpers/command_helpers.rb16
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