Module: Pod::Command::GemHelper

Defined in:
lib/pod/command/gem_helper.rb

Overview

This module is used by Command::PluginsHelper to download the Gem Specification data, check if a Gem is installed, and provide info on all versions of a Gem.

Class Method Summary collapse

Class Method Details

.cacheGemIndexCache

Getter for GemIndexCache

Returns:



18
19
20
# File 'lib/pod/command/gem_helper.rb', line 18

def self.cache
  @cache ||= GemIndexCache.new
end

.download_and_cache_specsObject

Instantiate a cache and download the spec index if it has not already been done.



25
26
27
# File 'lib/pod/command/gem_helper.rb', line 25

def self.download_and_cache_specs
  cache.download_and_cache_specs
end

.gem_installed?(gem_name, version_string = nil) ⇒ Bool

Tells if a gem is installed

Parameters:

  • gem_name (String)

    The name of the plugin gem to test

  • version_string (String) (defaults to: nil)

    An optional version string, used to check if a specific version of a gem is installed

Returns:

  • (Bool)

    true if the gem is installed, false otherwise.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pod/command/gem_helper.rb', line 40

def self.gem_installed?(gem_name, version_string = nil)
  version = Gem::Version.new(version_string) if version_string

  if Gem::Specification.respond_to?(:find_all_by_name)
    gems = Gem::Specification.find_all_by_name(gem_name)
    return !gems.empty? unless version
    gems.each { |gem| return true if gem.version == version }
    false
  else
    dep = Gem::Dependency.new(gem_name, version_string)
    !Gem.source_index.search(dep).empty?
  end
end

.installed_version(gem_name) ⇒ String

Get the version of a gem that is installed locally. If more than one version is installed, this returns the first version found, which MAY not be the highest/newest version.

Returns:

  • (String)

    The version of the gem that is installed, or nil if it is not installed.



61
62
63
64
65
66
67
68
69
# File 'lib/pod/command/gem_helper.rb', line 61

def self.installed_version(gem_name)
  if Gem::Specification.respond_to?(:find_all_by_name)
    gem = Gem::Specification.find_all_by_name(gem_name).first
  else
    dep = Gem::Dependency.new(gem_name)
    gem = Gem.source_index.search(dep).first
  end
  gem ? gem.version.to_s : nil
end

.versions_string(plugin_name, index_cache = @cache) ⇒ String

Create a string containing all versions of a plugin, colored to indicate if a specific version is installed locally.

Parameters:

  • plugin_name (String)

    The name of the plugin gem

  • index_cache (GemIndexCache) (defaults to: @cache)

    Optional index cache can be passed in, otherwise the module instance is used.

Returns:

  • (String)

    a string containing a comma separated concatenation of all versions of a plugin that were found on rubygems.org



86
87
88
89
90
91
# File 'lib/pod/command/gem_helper.rb', line 86

def self.versions_string(plugin_name, index_cache = @cache)
  name_tuples = index_cache.specs_with_name(plugin_name)
  sorted_versions = name_tuples.sort_by(&:version)
  version_strings = colorize_versions(sorted_versions)
  version_strings.join ', '
end