merge_path(base, rel)
private
Merges a base path base, with relative path rel, returns
a modified base path.
Show source
def merge_path(base, rel)
base_path = split_path(base)
rel_path = split_path(rel)
base_path << '' if base_path.last == '..'
while i = base_path.index('..')
base_path.slice!(i - 1, 2)
end
if (first = rel_path.first) and first.empty?
base_path.clear
rel_path.shift
end
rel_path.push('') if rel_path.last == '.' || rel_path.last == '..'
rel_path.delete('.')
tmp = []
rel_path.each do |x|
if x == '..' &&
!(tmp.empty? || tmp.last == '..')
tmp.pop
else
tmp << x
end
end
add_trailer_slash = !tmp.empty?
if base_path.empty?
base_path = ['']
elsif add_trailer_slash
base_path.pop
end
while x = tmp.shift
if x == '..'
base_path.pop if base_path.size > 1
else
base_path << x
tmp.each {|t| base_path << t}
add_trailer_slash = false
break
end
end
base_path.push('') if add_trailer_slash
return base_path.join('/')
end