1##
2#
3# Represents a gem of name +name+ at +version+ of +platform+. These
4# wrap the data returned from the indexes.
5
6require 'rubygems/platform'
7
8class Gem::NameTuple
9  def initialize(name, version, platform="ruby")
10    @name = name
11    @version = version
12
13    unless platform.kind_of? Gem::Platform
14      platform = "ruby" if !platform or platform.empty?
15    end
16
17    @platform = platform
18  end
19
20  attr_reader :name, :version, :platform
21
22  ##
23  # Turn an array of [name, version, platform] into an array of
24  # NameTuple objects.
25
26  def self.from_list list
27    list.map { |t| new(*t) }
28  end
29
30  ##
31  # Turn an array of NameTuple objects back into an array of
32  # [name, version, platform] tuples.
33
34  def self.to_basic list
35    list.map { |t| t.to_a }
36  end
37
38  ##
39  # A null NameTuple, ie name=nil, version=0
40
41  def self.null
42    new nil, Gem::Version.new(0), nil
43  end
44
45  ##
46  # Indicate if this NameTuple matches the current platform.
47
48  def match_platform?
49    Gem::Platform.match @platform
50  end
51
52  ##
53  # Indicate if this NameTuple is for a prerelease version.
54  def prerelease?
55    @version.prerelease?
56  end
57
58  ##
59  # Return the name that the gemspec file would be
60
61  def spec_name
62    case @platform
63    when nil, 'ruby', ''
64      "#{@name}-#{@version}.gemspec"
65    else
66      "#{@name}-#{@version}-#{@platform}.gemspec"
67    end
68  end
69
70  ##
71  # Convert back to the [name, version, platform] tuple
72
73  def to_a
74    [@name, @version, @platform]
75  end
76
77  def to_s
78    "#<Gem::NameTuple #{@name}, #{@version}, #{@platform}>"
79  end
80
81  def <=> other
82    to_a <=> other.to_a
83  end
84
85  include Comparable
86
87  ##
88  # Compare with +other+. Supports another NameTuple or an Array
89  # in the [name, version, platform] format.
90
91  def == other
92    case other
93    when self.class
94      @name == other.name and
95        @version == other.version and
96        @platform == other.platform
97    when Array
98      to_a == other
99    else
100      false
101    end
102  end
103
104  alias_method :eql?, :==
105
106  def hash
107    to_a.hash
108  end
109
110end
111