Class: LittleGhost::PathSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/little_ghost/path_set.rb

Overview

PathSet keeps prompt or skill lookup roots in deterministic search order. It is immutable, so appending a path does not change a running configuration.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths = []) ⇒ PathSet

Accepts path strings and Lookup::Root objects.



13
14
15
# File 'lib/little_ghost/path_set.rb', line 13

def initialize(paths = [])
  @paths = Array(paths).map { |path| path.is_a?(Lookup::Root) ? path : Lookup::Root.new(path:) }.freeze
end

Instance Attribute Details

#pathsObject (readonly)

The immutable Lookup::Root values in search order.



10
11
12
# File 'lib/little_ghost/path_set.rb', line 10

def paths
  @paths
end

Instance Method Details

#+(other) ⇒ Object

Appends other in a new path set; neither input is mutated.



28
29
30
# File 'lib/little_ghost/path_set.rb', line 28

def +(other)
  PathSet.new([*paths, *Array(other)])
end

#each(&block) ⇒ Object

Yields each Lookup::Root in order.



18
19
20
# File 'lib/little_ghost/path_set.rb', line 18

def each(&block)
  paths.each(&block)
end

#to_aObject

Copies the roots into a mutable array.



23
24
25
# File 'lib/little_ghost/path_set.rb', line 23

def to_a
  paths.dup
end