From 3c60ee88e50dc6ef41664683e3c88596d1356034 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Wed, 9 Jan 2013 00:24:53 +0200 Subject: [PATCH] Extract out gem related code to Pry::Rubygem And fix some typos, and prettify some lines. Signed-off-by: Kyrylo Silin --- lib/pry.rb | 1 + lib/pry/command.rb | 2 +- lib/pry/commands/gem_cd.rb | 4 +-- lib/pry/commands/gem_list.rb | 2 +- lib/pry/commands/gem_open.rb | 4 +-- lib/pry/commands/install_command.rb | 2 +- lib/pry/helpers/base_helpers.rb | 7 +--- lib/pry/helpers/command_helpers.rb | 37 ------------------- lib/pry/rubygem.rb | 56 +++++++++++++++++++++++++++++ 9 files changed, 65 insertions(+), 50 deletions(-) create mode 100644 lib/pry/rubygem.rb diff --git a/lib/pry.rb b/lib/pry.rb index 2ad87cd3..77269a4b 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -249,3 +249,4 @@ require 'pry/cli' require 'pry/pager' require 'pry/terminal_info' require 'pry/editor' +require 'pry/rubygem' diff --git a/lib/pry/command.rb b/lib/pry/command.rb index 311f3bec..d1ea6673 100644 --- a/lib/pry/command.rb +++ b/lib/pry/command.rb @@ -412,7 +412,7 @@ class Pry def call_safely(*args) unless dependencies_met? gems_needed = Array(command_options[:requires_gem]) - gems_not_installed = gems_needed.select { |g| !gem_installed?(g) } + gems_not_installed = gems_needed.select { |g| !Rubygem.installed?(g) } output.puts "\nThe command '#{command_name}' is #{text.bold("unavailable")} because it requires the following gems to be installed: #{(gems_not_installed.join(", "))}" output.puts "-" output.puts "Type `install-command #{command_name}` to install the required gems and activate this command." diff --git a/lib/pry/commands/gem_cd.rb b/lib/pry/commands/gem_cd.rb index 714269bd..de0ae02e 100644 --- a/lib/pry/commands/gem_cd.rb +++ b/lib/pry/commands/gem_cd.rb @@ -12,12 +12,12 @@ class Pry BANNER def process(gem) - Dir.chdir(gem_spec(gem).full_gem_path) + Dir.chdir(Rubygem.spec(gem).full_gem_path) output.puts(Dir.pwd) end def complete(str) - gem_complete(str) + Rubygem.complete(str) end end diff --git a/lib/pry/commands/gem_list.rb b/lib/pry/commands/gem_list.rb index 3c82879f..0918d9ac 100644 --- a/lib/pry/commands/gem_list.rb +++ b/lib/pry/commands/gem_list.rb @@ -13,7 +13,7 @@ class Pry def process(pattern = nil) pattern = Regexp.compile(pattern || '') - gems = gem_list(pattern).group_by(&:name) + gems = Rubygem.list(pattern).group_by(&:name) gems.each do |gem, specs| specs.sort! do |a,b| diff --git a/lib/pry/commands/gem_open.rb b/lib/pry/commands/gem_open.rb index 3a0c79e9..b5c16f14 100644 --- a/lib/pry/commands/gem_open.rb +++ b/lib/pry/commands/gem_open.rb @@ -13,13 +13,13 @@ class Pry BANNER def process(gem) - Dir.chdir(gem_spec(gem).full_gem_path) do + Dir.chdir(Rubygem.spec(gem).full_gem_path) do Pry::Editor.invoke_editor(".", 0, false) end end def complete(str) - gem_complete(str) + Rubygem.complete(str) end end diff --git a/lib/pry/commands/install_command.rb b/lib/pry/commands/install_command.rb index 41c08b5e..a2721e06 100644 --- a/lib/pry/commands/install_command.rb +++ b/lib/pry/commands/install_command.rb @@ -24,7 +24,7 @@ class Pry gems_to_install = Array(command.options[:requires_gem]) gems_to_install.each do |g| - next if gem_installed?(g) + next if Rubygem.installed?(g) output.puts "Installing `#{g}` gem..." begin diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index 853030d1..573e3a97 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -31,11 +31,6 @@ class Pry command_match.last if command_match end - def gem_installed?(gem_name) - require 'rubygems' - Gem::Specification.respond_to?(:find_all_by_name) ? !Gem::Specification.find_all_by_name(gem_name).empty? : Gem.source_index.find_name(gem_name).first - end - def not_a_real_file?(file) file =~ /(\(.*\))|<.*>/ || file =~ /__unknown__/ || file == "" || file == "-e" end @@ -43,7 +38,7 @@ class Pry def command_dependencies_met?(options) return true if !options[:requires_gem] Array(options[:requires_gem]).all? do |g| - gem_installed?(g) + Rubygem.installed?(g) end end diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 50bb148b..ad683afb 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -147,43 +147,6 @@ 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 - - # List gems matching a pattern - # @param [Regexp] pattern - # @return [Array] - def gem_list(pattern=/.*/) - if Gem::Specification.respond_to?(:each) - Gem::Specification.select{|spec| spec.name =~ pattern } - else - Gem.source_index.gems.values.select{|spec| spec.name =~ pattern } - end - end - - # Completion function for gem-cd and gem-open - # @param [String] so_far what the user's typed so far - # @return [Array] completions - def gem_complete(so_far) - if so_far =~ / ([^ ]*)\z/ - gem_list(%r{\A#{$2}}).map(&:name) - else - gem_list.map(&:name) - end - end end end end diff --git a/lib/pry/rubygem.rb b/lib/pry/rubygem.rb new file mode 100644 index 00000000..7d4c2dfb --- /dev/null +++ b/lib/pry/rubygem.rb @@ -0,0 +1,56 @@ +class Pry + module Rubygem + + class << self + def installed?(name) + require 'rubygems' + if Gem::Specification.respond_to?(:find_all_by_name) + Gem::Specification.find_all_by_name(name).any? + else + Gem.source_index.find_name(name).first + end + end + + # Get the gem spec object for the given gem name. + # + # @param [String] name + # @return [Gem::Specification] + def spec(name) + specs = if Gem::Specification.respond_to?(:each) + Gem::Specification.find_all_by_name(name) + else + Gem.source_index.find_name(name) + end + + spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.first + + spec or raise CommandError, "Gem `#{name}` not found" + end + + # List gems matching a pattern. + # + # @param [Regexp] pattern + # @return [Array] + def list(pattern = /.*/) + if Gem::Specification.respond_to?(:each) + Gem::Specification.select{|spec| spec.name =~ pattern } + else + Gem.source_index.gems.values.select{|spec| spec.name =~ pattern } + end + end + + # Completion function for gem-cd and gem-open. + # + # @param [String] so_far what the user's typed so far + # @return [Array] completions + def complete(so_far) + if so_far =~ / ([^ ]*)\z/ + self.list(%r{\A#{$2}}).map(&:name) + else + self.list.map(&:name) + end + end + end + + end +end