Module: IMW::Paths

Included in:
Dataset
Defined in:
lib/imw/utils/paths.rb

Overview

Implements methods designed to work with an object’s @paths attributes, adding and deleting symbolic references to paths and expanding calls to path_to from that attribute or (when a miss) from IMW::PATHS.

An including class should therefore define an array attribute @paths.

Instance Method Summary collapse

Instance Method Details

#add_path(sym, *pathsegs) ⇒ String

Adds a symbolic path for expansion by path_to.

add_path :foo, '~/whoa'
add_path :bar, :foo,   'baz'
path_to :bar
=> '~/whoa/baz'

Parameters:

  • sym (Symbol)

    the name of the path to store

  • pathsegs (Symbol, String)

    the path segments to use to define the path to the name

Returns:

  • (String)

    the resulting path



46
47
48
49
# File 'lib/imw/utils/paths.rb', line 46

def add_path sym, *pathsegs
  paths[sym] = pathsegs.flatten
  path_to(sym)
end

#path_to(*pathsegs) ⇒ String

Expands a shorthand workflow path specification to an actual file path. Strings are interpreted literally but symbols are first resolved to the paths they represent.

add_path :foo, '~/whoa'
path_to :foo, 'my_thing'
=> '~/whoa/my_thing'

Parameters:

Returns:

  • (String)

    the resulting expanded path



24
25
26
27
# File 'lib/imw/utils/paths.rb', line 24

def path_to *pathsegs
  path = Pathname.new path_to_helper(*pathsegs)
  path.absolute? ? File.expand_path(path) : path.to_s
end

#pathsHash

Return the presently defined paths for this object.

Returns:



32
33
34
# File 'lib/imw/utils/paths.rb', line 32

def paths
  @paths ||= {}
end

#remove_path(sym) ⇒ Object

Removes a symbolic path for expansion by path_to.

Parameters:

  • sym (Symbol)

    the stored path symbol to remove



54
55
56
# File 'lib/imw/utils/paths.rb', line 54

def remove_path sym
  paths.delete sym if paths.include? sym
end