Class: Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/rake-plus/path.rb

Overview

Patching Pathame to return self.class.new objects instead of Pathname.new

Direct Known Subclasses

Path

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



5
6
7
8
# File 'lib/rake-plus/path.rb', line 5

def +(other)
  other = self.class.new(other) unless other.kind_of?(Pathname)
  self.class.new(plus(@path, other.to_s))
end

#join(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rake-plus/path.rb', line 10

def join(*args)
  args.unshift self
  result = args.pop
  result = self.class.new(result) unless result.kind_of?(Pathname)
  return result if result.absolute?
  args.reverse_each {|arg|
    arg = self.class.new(arg) unless arg.kind_of?(Pathname)
    result = arg + result
    return result if result.absolute?
  }
  result
end

#relative_path_from(base_directory) ⇒ Object



23
24
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
54
55
56
57
# File 'lib/rake-plus/path.rb', line 23

def relative_path_from(base_directory)
  dest_directory = self.cleanpath.to_s
  base_directory = base_directory.cleanpath.to_s
  dest_prefix = dest_directory
  dest_names = []
  while r = chop_basename(dest_prefix)
    dest_prefix, basename = r
    dest_names.unshift basename if basename != '.'
  end
  base_prefix = base_directory
  base_names = []
  while r = chop_basename(base_prefix)
    base_prefix, basename = r
    base_names.unshift basename if basename != '.'
  end
  unless SAME_PATHS[dest_prefix, base_prefix]
    raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
  end
  while !dest_names.empty? &&
        !base_names.empty? &&
        SAME_PATHS[dest_names.first, base_names.first]
    dest_names.shift
    base_names.shift
  end
  if base_names.include? '..'
    raise ArgumentError, "base_directory has ..: #{base_directory.inspect}"
  end
  base_names.fill('..')
  relpath_names = base_names + dest_names
  if relpath_names.empty?
    self.class.new('.')
  else
    self.class.new(File.join(*relpath_names))
  end
end