lib/build/files/path.rb in build-files-1.6.0 vs lib/build/files/path.rb in build-files-1.7.0

- old
+ new

@@ -20,10 +20,14 @@ module Build module Files # Represents a file path with an absolute root and a relative offset: class Path + def self.current + self.new(::Dir.pwd) + end + def self.split(path) # Effectively dirname and basename: dirname, separator, filename = path.rpartition(File::SEPARATOR) filename, dot, extension = filename.rpartition('.') @@ -42,10 +46,18 @@ else path.split(File::SEPARATOR) end end + def self.root(path) + if Path === path + path.root + else + File.dirname(path) + end + end + # Return the shortest relative path to get to path from root. Root should be a directory with which you are computing the relative path. def self.shortest_path(path, root) path_components = Path.components(path) root_components = Path.components(root) @@ -102,9 +114,20 @@ @components ||= @full_path.split(File::SEPARATOR).freeze end def basename self.parts.last + end + + def parent + root = @root + full_path = File.dirname(@full_path) + + while root.size > full_path.size + root = Path.root(root) + end + + self.class.new(full_path, root) end def start_with?(*args) @full_path.start_with?(*args) end