Class: Gem::SourceList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubygems/source_list.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSourceList

Creates a new SourceList



23
24
25
# File 'lib/rubygems/source_list.rb', line 23

def initialize
  @sources = []
end

Instance Attribute Details

#sourcesObject (readonly)

The sources in this list



30
31
32
# File 'lib/rubygems/source_list.rb', line 30

def sources
  @sources
end

Class Method Details

.from(ary) ⇒ Object

Creates a new SourceList from an array of sources.



35
36
37
38
39
40
41
# File 'lib/rubygems/source_list.rb', line 35

def self.from(ary)
  list = new

  list.replace ary

  return list
end

Instance Method Details

#<<(obj) ⇒ Object

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rubygems/source_list.rb', line 51

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

#==(other) ⇒ Object

:nodoc:



107
108
109
# File 'lib/rubygems/source_list.rb', line 107

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

#clearObject

Removes all sources from the SourceList.



82
83
84
# File 'lib/rubygems/source_list.rb', line 82

def clear
  @sources.clear
end

#delete(source) ⇒ Object

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



142
143
144
145
146
147
148
# File 'lib/rubygems/source_list.rb', line 142

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

#eachObject

Yields each source URI in the list.



89
90
91
# File 'lib/rubygems/source_list.rb', line 89

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

#each_source(&b) ⇒ Object

Yields each source in the list.



96
97
98
# File 'lib/rubygems/source_list.rb', line 96

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

#empty?Boolean

Returns true if there are no sources in this SourceList.

Returns:

  • (Boolean)


103
104
105
# File 'lib/rubygems/source_list.rb', line 103

def empty?
  @sources.empty?
end

#firstObject

Returns the first source in the list.



123
124
125
# File 'lib/rubygems/source_list.rb', line 123

def first
  @sources.first
end

#include?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'lib/rubygems/source_list.rb', line 131

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

#initialize_copy(other) ⇒ Object

:nodoc:



43
44
45
# File 'lib/rubygems/source_list.rb', line 43

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

#replace(other) ⇒ Object

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



69
70
71
72
73
74
75
76
77
# File 'lib/rubygems/source_list.rb', line 69

def replace(other)
  clear

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

  self
end

#to_aObject Also known as: to_ary

Returns an Array of source URI Strings.



114
115
116
# File 'lib/rubygems/source_list.rb', line 114

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