Module: Valise::Unpath

Included in:
Item, PathMatcher, SearchRoot, Set, Set::Definer, Set::StemmedDefiner, Stack
Defined in:
lib/valise/utils.rb

Overview

XXX This has been overtaken by std-lib Pathname and should be mostly refactored out

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file_from_backtrace(line) ⇒ Object



28
29
30
# File 'lib/valise/utils.rb', line 28

def file_from_backtrace(line)
  /(.*):\d+/.match(line)[1]
end

.from_here(rel_path, base_path = nil) ⇒ Object



32
33
34
35
# File 'lib/valise/utils.rb', line 32

def from_here(rel_path, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  repath(collapse(unpath(base_path) + unpath(rel_path)))
end

.repath(segments) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/valise/utils.rb', line 106

def repath(segments)
  case segments
  when Array
    return segments.join(::File::Separator)
  when String
    return segments
  end
end

.string_to_segments(string) ⇒ Object



23
24
25
26
# File 'lib/valise/utils.rb', line 23

def string_to_segments(string)
  return string if string.empty?
  string.split(::File::Separator)
end

.unpath(parts) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/valise/utils.rb', line 54

def unpath(parts)
  if Array === parts and parts.length == 1
    parts = parts[0]
  end

  case parts
  when Array
    if (parts.find{|part| not (String === part or Symbol === part)}.nil?)
      parts = parts.map{|part| string_to_segments(part.to_s)}.flatten
    else
      raise ArgumentError, "path must be composed of strings or symbols"
    end
  when String
    parts = string_to_segments(parts)
  when Symbol
    parts = string_to_segments(parts.to_s)
  when ::File
    parts = parts.path
    parts = parts.split(::File::Separator)
  else
    raise ArgumentError, "path must be String, Array of Strings or File"
  end

  if /^~/ =~ parts[0]
    parts = ::File::expand_path(parts[0]).split(::File::Separator) + parts[1..-1]
  end

  return parts
end

.up_to(up_to = nil, base_path = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/valise/utils.rb', line 37

def up_to(up_to = nil, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  up_to ||= "lib"

  abs_path = File::expand_path(base_path)
  base_path = unpath(base_path)
  until base_path.empty? or base_path.last == up_to
    base_path.pop
  end

  if base_path.empty?
    raise "Relative root #{up_to.inspect} not found in #{abs_path.inspect}"
  end

  return repath(base_path)
end

Instance Method Details

#collapse(segments) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/valise/utils.rb', line 84

def collapse(segments)
  collapsed = []
  segments.each do |segment|
    case segment
    when '.'
    when ""
      if collapsed.empty?
        collapsed.push segment
      end
    when '..'
      if collapsed.empty?
        collapsed.push segment
      else
        collapsed.pop
      end
    else
      collapsed.push segment
    end
  end
  collapsed
end

#file_from_backtrace(line) ⇒ Object



28
29
30
# File 'lib/valise/utils.rb', line 28

def file_from_backtrace(line)
  /(.*):\d+/.match(line)[1]
end

#from_here(rel_path, base_path = nil) ⇒ Object



32
33
34
35
# File 'lib/valise/utils.rb', line 32

def from_here(rel_path, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  repath(collapse(unpath(base_path) + unpath(rel_path)))
end

#up_to(up_to = nil, base_path = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/valise/utils.rb', line 37

def up_to(up_to = nil, base_path = nil)
  base_path ||= file_from_backtrace(caller[0])
  up_to ||= "lib"

  abs_path = File::expand_path(base_path)
  base_path = unpath(base_path)
  until base_path.empty? or base_path.last == up_to
    base_path.pop
  end

  if base_path.empty?
    raise "Relative root #{up_to.inspect} not found in #{abs_path.inspect}"
  end

  return repath(base_path)
end