module Gorillib::Pathref

Constants

ROOT_PATHS

Public Instance Methods

of(*pathsegs) click to toggle source

Expand a path with late-evaluated segments. Calls expand_path – ‘~’ becomes $HOME, ‘..’ is expanded, etc.

@example A symbol represents a segment to expand

Pathname.register_path(:conf_dir, '/etc/delorean')
Pathname.path_to(:conf_dir)                  # '/etc/delorean'
Pathname.path_to(:conf_dir, modacity.conf)   # '/etc/delorean/modacity.conf'

@example References aren’t expanded until they’re read

Pathname.register_path(:conf_dir, '/etc/delorean')
Pathname.register_path(:modacity, :conf_dir, 'modacity.conf')
Pathname.path_to(:modacity)                  # '/etc/delorean/modacity.conf'
# if we change the conf_dir, everything under it changes as well
Pathname.register_path(:conf_dir, '~/.delorean.d')
Pathname.path_to(:modacity)                  # '/home/flip/delorean.d/modacity.conf'

@example References can be relative, and can hold symbols themselves

Pathname.register_path(:conf_dir, '/etc', :appname, :environment)
Pathname.register_path(:appname, 'happy_app')
Pathname.register_path(:environment, 'dev')
Pathname.path_to(:conf_dir)                  # '/etc/happy_app/dev'

@param [Array<>] pathsegs

any mixture of strings (literal sub-paths) and symbols (interpreted as references)

@return [Pathname] A single expanded Pathname

# File lib/gorillib/pathname.rb, line 55
def of(*pathsegs)
  relpath_to(*pathsegs).expand_path
end
Also aliased as: path_to
path_to(*pathsegs)
Alias for: of
register_default_paths(handle_paths = {}) click to toggle source
# File lib/gorillib/pathname.rb, line 19
def register_default_paths(handle_paths = {})
  handle_paths.each_pair do |handle, pathsegs|
    register_path(handle, *pathsegs) unless ROOT_PATHS.has_key?(handle.to_sym)
  end
end
register_path(handle, *pathsegs) click to toggle source
# File lib/gorillib/pathname.rb, line 10
def register_path(handle, *pathsegs)
  ArgumentError.arity_at_least!(pathsegs, 1)
  ROOT_PATHS[handle.to_sym] = pathsegs
end
register_paths(handle_paths = {}) click to toggle source
# File lib/gorillib/pathname.rb, line 15
def register_paths(handle_paths = {})
  handle_paths.each_pair{|handle, pathsegs| register_path(handle, *pathsegs) }
end
relative_path_to(*pathsegs)
Alias for: relpath_to
relpath_to(*pathsegs) click to toggle source

Expand a path with late-evaluated segments @see ‘.path_to`

Calls cleanpath (removing ‘//` double slashes and useless `..`s), but does not reference the filesystem or make paths absolute

# File lib/gorillib/pathname.rb, line 67
def relpath_to(*pathsegs)
  ArgumentError.arity_at_least!(pathsegs, 1)
  pathsegs = pathsegs.flatten.map{|ps| expand_pathseg(ps) }.flatten
  self.new(File.join(*pathsegs)).cleanpath(true)
end
Also aliased as: relative_path_to
unregister_path(handle) click to toggle source
# File lib/gorillib/pathname.rb, line 25
def unregister_path(handle)
  ROOT_PATHS.delete handle.to_sym
end

Protected Instance Methods

expand_pathseg(handle) click to toggle source

Recursively expand a path handle @return [Array<String>] an array of path segments, suitable for .join

# File lib/gorillib/pathname.rb, line 77
def expand_pathseg(handle)
  return handle unless handle.is_a?(Symbol)
  pathsegs = ROOT_PATHS[handle] or raise ArgumentError, "Don't know how to expand path reference '#{handle.inspect}'."
  pathsegs.map{|ps| expand_pathseg(ps) }.flatten
end