summaryrefslogtreecommitdiff
path: root/jni/ruby/lib/rubygems/source_list.rb
blob: e01f11cc1e21461cc514ab434841f3899d3e8964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'rubygems/source'

##
# The SourceList represents the sources rubygems has been configured to use.
# A source may be created from an array of sources:
#
#   Gem::SourceList.from %w[https://rubygems.example https://internal.example]
#
# Or by adding them:
#
#   sources = Gem::SourceList.new
#   sources.add 'https://rubygems.example'
#
# The most common way to get a SourceList is Gem.sources.

class Gem::SourceList

  include Enumerable

  ##
  # Creates a new SourceList

  def initialize
    @sources = []
  end

  ##
  # The sources in this list

  attr_reader :sources

  ##
  # Creates a new SourceList from an array of sources.

  def self.from(ary)
    list = new

    list.replace ary

    return list
  end

  def initialize_copy(other) # :nodoc:
    @sources = @sources.dup
  end

  ##
  # Appends +obj+ to the source list which may be a Gem::Source, URI or URI
  # String.

  def <<(obj)
    src = case obj
          when URI
            Gem::Source.new(obj)
          when Gem::Source
            obj
          else
            Gem::Source.new(URI.parse(obj))
          end

    @sources << src
    src
  end

  ##
  # Replaces this SourceList with the sources in +other+  See #<< for
  # acceptable items in +other+.

  def replace(other)
    clear

    other.each do |x|
      self << x
    end

    self
  end

  ##
  # Removes all sources from the SourceList.

  def clear
    @sources.clear
  end

  ##
  # Yields each source URI in the list.

  def each
    @sources.each { |s| yield s.uri.to_s }
  end

  ##
  # Yields each source in the list.

  def each_source(&b)
    @sources.each(&b)
  end

  ##
  # Returns true if there are no sources in this SourceList.

  def empty?
    @sources.empty?
  end

  def == other # :nodoc:
    to_a == other
  end

  ##
  # Returns an Array of source URI Strings.

  def to_a
    @sources.map { |x| x.uri.to_s }
  end

  alias_method :to_ary, :to_a

  ##
  # Returns the first source in the list.

  def first
    @sources.first
  end

  ##
  # Returns true if this source list includes +other+ which may be a
  # Gem::Source or a source URI.

  def include?(other)
    if other.kind_of? Gem::Source
      @sources.include? other
    else
      @sources.find { |x| x.uri.to_s == other.to_s }
    end
  end

  ##
  # Deletes +source+ from the source list which may be a Gem::Source or a URI.

  def delete source
    if source.kind_of? Gem::Source
      @sources.delete source
    else
      @sources.delete_if { |x| x.uri.to_s == source.to_s }
    end
  end
end