Class: Tap::Env::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/tap/env/path.rb

Constant Summary collapse

FILE =
'tap.yml'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, map = {}) ⇒ Path

Creates a new Path relative to the base.



40
41
42
43
44
45
# File 'lib/tap/env/path.rb', line 40

def initialize(base, map={})
  @base = File.expand_path(base)
  @map = {}
  
  map.each_pair {|type, paths| self[type] = paths }
end

Instance Attribute Details

#baseObject (readonly)

The path base.



34
35
36
# File 'lib/tap/env/path.rb', line 34

def base
  @base
end

#mapObject (readonly)

A mapping of types to paths.



37
38
39
# File 'lib/tap/env/path.rb', line 37

def map
  @map
end

Class Method Details

.escape(str) ⇒ Object



26
27
28
# File 'lib/tap/env/path.rb', line 26

def escape(str)
  "'#{str.gsub("'", "\\\\'")}'"
end

.join(paths) ⇒ Object



18
19
20
# File 'lib/tap/env/path.rb', line 18

def join(paths)
  paths.join(':')
end

.load(path_file) ⇒ Object



22
23
24
# File 'lib/tap/env/path.rb', line 22

def load(path_file)
  Root.trivial?(path_file) ? {} : (YAML.load_file(path_file) || {})
end

.split(str, dir = Dir.pwd) ⇒ Object

Splits the path string along ‘:’ boundaries and expands each resulting fragments relative to dir. Duplicate paths are removed. Returns the resulting paths.

An array of pre-split paths may also be provided as an input.



11
12
13
14
15
16
# File 'lib/tap/env/path.rb', line 11

def split(str, dir=Dir.pwd)
  paths = str.kind_of?(String) ? str.split(':') : str
  paths.collect! {|path| File.expand_path(path, dir) } if dir
  paths.uniq!
  paths
end

Instance Method Details

#==(another) ⇒ Object



59
60
61
62
63
# File 'lib/tap/env/path.rb', line 59

def ==(another)
  another.kind_of?(Path) &&
  base == another.base &&
  map == another.map
end

#[](type) ⇒ Object

Returns an array of expanded paths associated with the type; by default the type expanded under base.



49
50
51
# File 'lib/tap/env/path.rb', line 49

def [](type)
  map[type] ||= [File.expand_path(type.to_s, base)]
end

#[]=(type, paths) ⇒ Object

Sets the path for the type. Paths are split and expanded relative to base (see Path.split).



55
56
57
# File 'lib/tap/env/path.rb', line 55

def []=(type, paths)
  map[type] = Path.split(paths, base)
end

#to_sObject

Returns the base path.



66
67
68
# File 'lib/tap/env/path.rb', line 66

def to_s
  base
end