Class: GemMirror::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_mirror/gem.rb

Overview

The Gem class contains data about a Gem such as the name, requirement as well as providing some methods to more easily extract the specific version number.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, requirement = nil) ⇒ Gem

Returns a new instance of Gem.

Parameters:

  • name (String)
  • requirement (Gem::Requirement) (defaults to: nil)


31
32
33
34
35
36
37
38
# File 'lib/gem_mirror/gem.rb', line 31

def initialize(name, requirement = nil)
  requirement ||= ::Gem::Requirement.default

  requirement = ::Gem::Requirement.new(requirement) if requirement.is_a?(String)

  @name        = name
  @requirement = requirement
end

Instance Attribute Details

#nameString (readonly)

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gem_mirror/gem.rb', line 14

class Gem
  attr_reader :name, :requirement

  ##
  # Returns a `Gem::Version` instance based on the specified requirement.
  #
  # @param [Gem::Requirement] requirement
  # @return [Gem::Version]
  #
  def self.version_for(requirement)
    ::Gem::Version.new(requirement.requirements.max.last.version)
  end

  ##
  # @param [String] name
  # @param [Gem::Requirement] requirement
  #
  def initialize(name, requirement = nil)
    requirement ||= ::Gem::Requirement.default

    requirement = ::Gem::Requirement.new(requirement) if requirement.is_a?(String)

    @name        = name
    @requirement = requirement
  end

  ##
  # @return [Gem::Version]
  #
  def version
    @version ||= self.class.version_for(requirement)
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def version?
    version && !version.segments.reject(&:zero?).empty?
  end

  ##
  # Returns the filename of the Gemfile.
  #
  # @param [String] gem_version
  # @return [String]
  #
  def filename(gem_version = nil)
    gem_version ||= version.to_s

    "#{name}-#{gem_version}.gem"
  end
end

#requirementGem::Requirement (readonly)

Returns:

  • (Gem::Requirement)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gem_mirror/gem.rb', line 14

class Gem
  attr_reader :name, :requirement

  ##
  # Returns a `Gem::Version` instance based on the specified requirement.
  #
  # @param [Gem::Requirement] requirement
  # @return [Gem::Version]
  #
  def self.version_for(requirement)
    ::Gem::Version.new(requirement.requirements.max.last.version)
  end

  ##
  # @param [String] name
  # @param [Gem::Requirement] requirement
  #
  def initialize(name, requirement = nil)
    requirement ||= ::Gem::Requirement.default

    requirement = ::Gem::Requirement.new(requirement) if requirement.is_a?(String)

    @name        = name
    @requirement = requirement
  end

  ##
  # @return [Gem::Version]
  #
  def version
    @version ||= self.class.version_for(requirement)
  end

  ##
  # @return [TrueClass|FalseClass]
  #
  def version?
    version && !version.segments.reject(&:zero?).empty?
  end

  ##
  # Returns the filename of the Gemfile.
  #
  # @param [String] gem_version
  # @return [String]
  #
  def filename(gem_version = nil)
    gem_version ||= version.to_s

    "#{name}-#{gem_version}.gem"
  end
end

Class Method Details

.version_for(requirement) ⇒ Gem::Version

Returns a ‘Gem::Version` instance based on the specified requirement.

Parameters:

  • requirement (Gem::Requirement)

Returns:

  • (Gem::Version)


23
24
25
# File 'lib/gem_mirror/gem.rb', line 23

def self.version_for(requirement)
  ::Gem::Version.new(requirement.requirements.max.last.version)
end

Instance Method Details

#filename(gem_version = nil) ⇒ String

Returns the filename of the Gemfile.

Parameters:

  • gem_version (String) (defaults to: nil)

Returns:

  • (String)


60
61
62
63
64
# File 'lib/gem_mirror/gem.rb', line 60

def filename(gem_version = nil)
  gem_version ||= version.to_s

  "#{name}-#{gem_version}.gem"
end

#versionGem::Version

Returns:

  • (Gem::Version)


43
44
45
# File 'lib/gem_mirror/gem.rb', line 43

def version
  @version ||= self.class.version_for(requirement)
end

#version?TrueClass|FalseClass

Returns:

  • (TrueClass|FalseClass)


50
51
52
# File 'lib/gem_mirror/gem.rb', line 50

def version?
  version && !version.segments.reject(&:zero?).empty?
end