Module: Valise::Unpath

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

Instance Method Summary collapse

Instance Method Details

#collapse(segments) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/valise/utils.rb', line 55

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

#repath(segments) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/valise/utils.rb', line 69

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

#string_to_segments(string) ⇒ Object



21
22
23
# File 'lib/valise/utils.rb', line 21

def string_to_segments(string)
  string.split(::File::Separator)
end

#unpath(parts) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/valise/utils.rb', line 25

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