Class: Fastup::SearchPath

Inherits:
Object
  • Object
show all
Includes:
XFile
Defined in:
lib/fastup.rb

Instance Method Summary collapse

Methods included from XFile

#directory?, #each_entry

Constructor Details

#initialize(paths) ⇒ SearchPath

Flatten the search path paths by creating a tree of symlinks at dest. For example, if the paths include /usr/bin, and /usr/lib, then dest will be a directory with symlinks bin and lib to the respective directories.



53
54
55
56
# File 'lib/fastup.rb', line 53

def initialize(paths)
  @root = {}
  paths.each{ |path| @root = insert!(path) }
end

Instance Method Details

#lookup(path, root = @root) ⇒ Object

given a path like ‘a/b/c’, determine if it exists in the tree, by trying root[‘b’]

if root is a string, then it should be ‘somedir/a’

if root[‘b’] is a string, then it should be ‘somedir/a/b’

if root[‘b’] is a string, then it should be ’somedir/a/b/c’

in all cases, the return value is ‘somedir/a/b/c’



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

def lookup(path, root=@root)
  case root
  when String
    if path.nil?
      return root
    else
      File.join(root, path)
    end
  when Hash
    head, rest = path.split(File::SEPARATOR, 2)
    lookup(rest, root[head])
  end
end