Module: Garcon::Pathref

Extended by:
Pathref
Included in:
Pathref, Pathname
Defined in:
lib/garcon/core_ext/pathname.rb

Overview

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

Examples:

A symbol represents a segment to expand
  Pathname.register_path(:conf_dir, '/etc/openldap')
  Pathname.path_to(:conf_dir)            # => '/etc/openldap'
  Pathname.path_to(:conf_dir, ldap.conf) # => '/etc/openldap/ldap.conf'
References aren't expanded until they're read
  Pathname.register_path(:conf_dir, '/etc/openldap')
  Pathname.register_path(:ldap, :conf_dir, 'ldap.conf')
  Pathname.path_to(:ldap)                # => '/etc/openldap/ldap.conf'
If we change the conf_dir, everything under it changes as well
  Pathname.register_path(:conf_dir, '~/.openldap.d')
  Pathname.path_to(:ldap)                # => '/root/openldap.d/ldap.conf'

Constant Summary collapse

ROOT_PATHS =
Hash.new

Instance Method Summary collapse

Instance Method Details

#of(*pathsegs) ⇒ Pathname Also known as: path_to

Returns A single expanded Pathname.

Parameters:

  • pathsegs (Array<[String,Symbol]>)

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

Returns:

  • (Pathname)

    A single expanded Pathname



60
61
62
# File 'lib/garcon/core_ext/pathname.rb', line 60

def of(*pathsegs)
  relpath_to(*pathsegs).expand_path
end

#register_default_paths(handle_paths = {}) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/garcon/core_ext/pathname.rb', line 79

def register_default_paths(handle_paths = {})
  handle_paths.each_pair do |handle, pathsegs|
    unless ROOT_PATHS.has_key?(handle.to_sym)
      register_path(handle, *pathsegs)
    end
  end
end

#register_path(handle, *pathsegs) ⇒ Object



66
67
68
69
# File 'lib/garcon/core_ext/pathname.rb', line 66

def register_path(handle, *pathsegs)
  ArgumentError.arity_at_least!(pathsegs, 1)
  ROOT_PATHS[handle.to_sym] = pathsegs
end

#register_paths(handle_paths = {}) ⇒ Object



72
73
74
75
76
# File 'lib/garcon/core_ext/pathname.rb', line 72

def register_paths(handle_paths = {})
  handle_paths.each_pair do |handle, pathsegs|
    register_path(handle, *pathsegs)
  end
end

#relpath_to(*pathsegs) ⇒ Object Also known as: relative_path_to

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.



97
98
99
100
101
# File 'lib/garcon/core_ext/pathname.rb', line 97

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

#unregister_path(handle) ⇒ Object



88
89
90
# File 'lib/garcon/core_ext/pathname.rb', line 88

def unregister_path(handle)
  ROOT_PATHS.delete handle.to_sym
end