123456789_123456789_123456789_123456789_123456789_

Class: Gem::NameTuple

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Comparable
Inherits: Object
Defined in: lib/rubygems/name_tuple.rb

Overview

Represents a gem of name #name at #version of #platform, optionally carrying a #content_address and #ruby_abi for content-addressable gems. These wrap the data returned from the indexes.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(name, version, platform = Gem::Platform::RUBY, content_address: nil, ruby_abi: nil) ⇒ NameTuple

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 10

def initialize(name, version, platform = Gem::Platform::RUBY, content_address: nil, ruby_abi: nil)
  @name = name
  @version = version

  platform &&= platform.to_s
  platform = Gem::Platform::RUBY if !platform || platform.empty?
  @platform = platform
  @content_address = content_address unless content_address.nil?
  @ruby_abi = ruby_abi unless ruby_abi.nil?
end

Class Method Details

.from_list(list)

Turn an array of tuples into an array of NameTuple objects. Accepts:

  • NameTuple objects (passed through as-is)
  • 3-element arrays: [name, version, platform]
  • 5-element arrays: [name, version, platform, content_address, ruby_abi]
[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 29

def self.from_list(list)
  list.map do |tuple|
    case tuple
    when Gem::NameTuple
      tuple
    when Array
      case tuple.length
      when 3, 5
        new(tuple[0], tuple[1], tuple[2], content_address: tuple[3], ruby_abi: tuple[4])
      else
        raise ArgumentError, "Expected a 3- or 5-element tuple, got #{tuple.length}"
      end
    else
      raise ArgumentError, "Expected a Gem::NameTuple or Array, got #{tuple.class}"
    end
  end
end

.null

A null NameTuple, ie name=nil, version=0

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 58

def self.null
  new nil, Gem::Version.new(0), nil
end

.to_basic(list)

Turn an array of NameTuple objects back into an array of [name, version, platform] tuples.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 51

def self.to_basic(list)
  list.map {|tuple| [tuple.name, tuple.version, tuple.platform] }
end

Instance Attribute Details

#content_address (readonly)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 21

attr_reader :name, :version, :platform, :content_address, :ruby_abi

#match_platform?Boolean (readonly)

Indicate if this NameTuple matches the current platform.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 80

def match_platform?
  Gem::Platform.match_gem? @platform, @name
end

#name (readonly)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 21

attr_reader :name, :version, :platform, :content_address, :ruby_abi

#platform (readonly)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 21

attr_reader :name, :version, :platform, :content_address, :ruby_abi

#prerelease?Boolean (readonly)

Indicate if this NameTuple is for a prerelease version.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 86

def prerelease?
  @version.prerelease?
end

#ruby_abi (readonly)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 21

attr_reader :name, :version, :platform, :content_address, :ruby_abi

#version (readonly)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 21

attr_reader :name, :version, :platform, :content_address, :ruby_abi

Instance Method Details

#<=>(other)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 128

def <=>(other)
  sort_key <=> other.sort_key
end

#==(other) Also known as: #eql?

Compare with other. Supports another NameTuple or an Array in the [name, version, platform, content_address, ruby_abi] format.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 142

def ==(other)
  case other
  when self.class
    @name == other.name &&
      @version == other.version &&
      @platform == other.platform &&
      @content_address == other.content_address &&
      @ruby_abi == other.ruby_abi
  when Array
    to_a == other
  else
    false
  end
end

#deconstruct

Alias for #to_a.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 110

alias_method :deconstruct, :to_a

#deconstruct_keys(keys)

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 112

def deconstruct_keys(keys)
  {
    name: @name,
    version: @version,
    platform: @platform,
    content_address: @content_address,
    ruby_abi: @ruby_abi,
  }
end

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 157

alias_method :eql?, :==

#full_name

Returns the full name (name-version) of this ::Gem. Platform information is included if it is not the default Ruby platform. This mimics the behavior of Gem::Specification#full_name.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 67

def full_name
  if @content_address
    "#{@name}-#{@version}-#{@content_address}"
  elsif @platform.nil? || @platform.empty? || @platform == Gem::Platform::RUBY
    "#{@name}-#{@version}"
  else
    "#{@name}-#{@version}-#{@platform}"
  end
end

#hash

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 159

def hash
  to_a.hash
end

#inspect Also known as: #to_s

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 122

def inspect # :nodoc:
  "#<Gem::NameTuple name=#{@name} version=#{@version} platform=#{@platform} content_address=#{@content_address} ruby_abi=#{@ruby_abi}>"
end

#sort_key

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 132

def sort_key # :nodoc:
  [@name, @version, Gem::Platform.sort_priority(@platform), @content_address.to_s, @ruby_abi.to_s]
end

#spec_name

Return the name that the gemspec file would be

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 93

def spec_name
  "#{full_name}.gemspec"
end

#to_a Also known as: #deconstruct

Convert back to the tuple array. Returns [name, version, platform] for non-content-addressable gems, or [name, version, platform, content_address, ruby_abi] for content-addressable gems.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 102

def to_a
  if @content_address
    [@name, @version, @platform, @content_address, @ruby_abi]
  else
    [@name, @version, @platform]
  end
end

#to_s

This method is for internal use only.

Alias for #inspect.

[ GitHub ]

  
# File 'lib/rubygems/name_tuple.rb', line 126

alias_method :to_s, :inspect # :nodoc: