Class: Toys::SourceList

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeSourceList

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.

Parameters:

  • spec (Toys::SourceSpec::Base)

    The source spec to add.

  • high_priority (boolean) (defaults to: false)

    If true, add this source at the top of the priority list. Defaults to false, indicating the new source should be at the bottom of the priority list.

Returns:

  • (self)

Raises:

  • (ArgumentError)

    if the given object is not a source spec.



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.

Yields:

Returns:

  • (self)

    if a block is given.

  • (Enumerator)

    if no block is given.



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.

Returns:

  • (boolean)


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

def empty?
  @sources.empty?
end

#sizeInteger

Returns the size of the list.

Returns:

  • (Integer)


51
52
53
# File 'lib/toys/source_list.rb', line 51

def size
  @sources.size
end