Class: Toys::SourceList
- Inherits:
-
Object
- Object
- Toys::SourceList
- Defined in:
- lib/toys/source_list.rb
Overview
An ordered collection of root sources for the Loader, held as unresolved SourceSpec objects. Use this class to build a list of sources, and then pass it to the Loader constructor.
Each spec added to the list occupies its own priority level, and resolves to a single root source. This is an invariant the Loader relies on.
A source list resolves nothing. The specs it holds describe sources; the Loader turns them into SourceInfo objects when it first needs them.
Not thread-safe. Callers should ensure that access is single-threaded or synchronized.
Instance Method Summary collapse
-
#add(spec, high_priority: false) ⇒ self
Add a source spec to the list, assigning it the next priority.
-
#each_with_priority {|Toys::SourceSpec::Base, Integer| ... } ⇒ self, Enumerator
Iterate over the source specs and their assigned priorities, in the order in which they were added.
-
#empty? ⇒ boolean
Determines whether the list is empty.
-
#initialize ⇒ SourceList
constructor
Create an empty source list.
-
#size ⇒ Integer
Returns the size of the list.
Constructor Details
#initialize ⇒ SourceList
Create an empty source list.
23 24 25 26 |
# File 'lib/toys/source_list.rb', line 23 def initialize @sources = [] @max_priority = @min_priority = 0 end |
Instance Method Details
#add(spec, high_priority: false) ⇒ self
Add a source spec to the list, assigning it the next priority.
80 81 82 83 84 85 86 87 |
# File 'lib/toys/source_list.rb', line 80 def add(spec, high_priority: false) raise ::ArgumentError, "Illegal source spec: #{spec.inspect}" unless spec.is_a?(SourceSpec::Base) priority = high_priority ? @max_priority + 1 : @min_priority - 1 @sources << [spec, priority].freeze @max_priority = priority if priority > @max_priority @min_priority = priority if priority < @min_priority self end |
#each_with_priority {|Toys::SourceSpec::Base, Integer| ... } ⇒ self, Enumerator
Iterate over the source specs and their assigned priorities, in the order in which they were added.
63 64 65 66 67 |
# File 'lib/toys/source_list.rb', line 63 def each_with_priority return to_enum(:each_with_priority) unless block_given? @sources.each { |pair| yield pair[0], pair[1] } self end |
#empty? ⇒ boolean
Determines whether the list is empty.
42 43 44 |
# File 'lib/toys/source_list.rb', line 42 def empty? @sources.empty? end |
#size ⇒ Integer
Returns the size of the list.
51 52 53 |
# File 'lib/toys/source_list.rb', line 51 def size @sources.size end |