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 << '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



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

def initialize
  @sources = []
end

Instance Attribute Details

#sourcesObject (readonly)

The sources in this list



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

def sources
  @sources
end

Class Method Details

.from(ary) ⇒ Object

Creates a new SourceList from an array of sources.



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

def self.from(ary)
  list = new

  list.replace ary

  list
end

Instance Method Details

#<<(obj) ⇒ Object

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



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

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

  @sources << src unless @sources.include?(src)
  src
end

#==(other) ⇒ Object

:nodoc:



140
141
142
# File 'lib/rubygems/source_list.rb', line 140

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

#append(obj) ⇒ Object

Appends obj to the end of the source list, moving it if already present. obj may be a Gem::Source, Gem::URI or URI String. Moves obj to the end of the list if already present.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rubygems/source_list.rb', line 85

def append(obj)
  src = case obj
        when Gem::Source
          obj
        else
          Gem::Source.new(obj)
  end

  @sources.delete(src) if @sources.include?(src)
  @sources << src
  src
end

#clearObject

Removes all sources from the SourceList.



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

def clear
  @sources.clear
end

#delete(source) ⇒ Object

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



175
176
177
178
179
180
181
# File 'lib/rubygems/source_list.rb', line 175

def delete(source)
  if source.is_a? 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.



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

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

#each_source(&b) ⇒ Object

Yields each source in the list.



129
130
131
# File 'lib/rubygems/source_list.rb', line 129

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

#empty?Boolean

Returns true if there are no sources in this SourceList.

Returns:

  • (Boolean)


136
137
138
# File 'lib/rubygems/source_list.rb', line 136

def empty?
  @sources.empty?
end

#firstObject

Returns the first source in the list.



156
157
158
# File 'lib/rubygems/source_list.rb', line 156

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)


164
165
166
167
168
169
170
# File 'lib/rubygems/source_list.rb', line 164

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

#initialize_copy(other) ⇒ Object

:nodoc:



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

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

#prepend(obj) ⇒ Object

Prepends obj to the beginning of the source list which may be a Gem::Source, Gem::URI or URI Moves obj to the beginning of the list if already present. String.



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

def prepend(obj)
  src = case obj
        when Gem::Source
          obj
        else
          Gem::Source.new(obj)
  end

  @sources.delete(src) if @sources.include?(src)
  @sources.unshift(src)
  src
end

#replace(other) ⇒ Object

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



102
103
104
105
106
107
108
109
110
# File 'lib/rubygems/source_list.rb', line 102

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.



147
148
149
# File 'lib/rubygems/source_list.rb', line 147

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