Module: PryGem::Rubygem

Defined in:
lib/pry-gem/rubygem.rb

Overview

Since:

  • 1.0.0

Class Method Summary collapse

Class Method Details

.complete(so_far) ⇒ Array<String>

Completion function for gem-cd and gem-open.

Parameters:

  • so_far (String)

    what the user’s typed so far

Returns:

  • (Array<String>)

    completions

Since:

  • 1.0.0



47
48
49
50
51
52
53
# File 'lib/pry-gem/rubygem.rb', line 47

def complete(so_far)
  if so_far =~ / ([^ ]*)\z/
    list(/\A#{Regexp.last_match(2)}/).map(&:name)
  else
    list.map(&:name)
  end
end

.install(name) ⇒ void

This method returns an undefined value.

Installs a gem with all its dependencies.

Parameters:

  • name (String)

Since:

  • 1.0.0



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pry-gem/rubygem.rb', line 59

def install(name)
  require 'rubygems/dependency_installer'
  gem_config = Gem.configuration['gem']
  gemrc_opts = (gem_config.nil? ? "" : gem_config.split(' '))
  destination = if gemrc_opts.include?('--user-install')
                  Gem.user_dir
                elsif File.writable?(Gem.dir)
                  Gem.dir
                else
                  Gem.user_dir
                end
  installer = Gem::DependencyInstaller.new(install_dir: destination)
  installer.install(name)
rescue Errno::EACCES
  raise CommandError,
        "Insufficient permissions to install #{green(name)}."
rescue Gem::GemNotFoundException
  raise CommandError,
        "Gem #{green(name)} not found. Aborting installation."
else
  Gem.refresh
end

.installed?(name) ⇒ Boolean

Returns:

  • (Boolean)

Since:

  • 1.0.0



7
8
9
10
11
12
13
# File 'lib/pry-gem/rubygem.rb', line 7

def installed?(name)
  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

.list(pattern = /.*/) ⇒ Array<Gem::Specification>

List gems matching a pattern.

Parameters:

  • pattern (Regexp) (defaults to: /.*/)

Returns:

  • (Array<Gem::Specification>)

Since:

  • 1.0.0



35
36
37
38
39
40
41
# File 'lib/pry-gem/rubygem.rb', line 35

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

.spec(name) ⇒ Gem::Specification

Get the gem spec object for the given gem name.

Parameters:

  • name (String)

Returns:

  • (Gem::Specification)

Since:

  • 1.0.0



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pry-gem/rubygem.rb', line 19

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

  first_spec = specs.max_by { |spec| Gem::Version.new(spec.version) }

  first_spec || raise(CommandError, "Gem `#{name}` not found")
end