summaryrefslogtreecommitdiff
path: root/lib/rubygems/source_list.rb
blob: 7bd8ef0b78934439dcdb72177c7cd7c04a19b4bb (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
require 'rubygems/source'

class Gem::SourceList
  def initialize
    @sources = []
  end

  attr_reader :sources

  def self.from(ary)
    list = new

    if ary
      ary.each do |x|
        list << x
      end
    end

    return list
  end

  def initialize_copy(other)
    @sources = @sources.dup
  end

  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

  def replace(other)
    @sources.clear

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

    self
  end

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

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

  def ==(other)
    to_a == other
  end

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

  alias_method :to_ary, :to_a

  def first
    @sources.first
  end

  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

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