lib/build/files/path.rb in build-files-0.2.1 vs lib/build/files/path.rb in build-files-0.2.2
- old
+ new
@@ -57,10 +57,14 @@
relative_offset += 1 unless root.end_with?(File::SEPARATOR)
return full_path.slice(relative_offset..-1)
end
+ def self.[] path
+ self === path ? path : self.new(path.to_s)
+ end
+
# Both paths must be full absolute paths, and path must have root as an prefix.
def initialize(full_path, root = nil, relative_path = nil)
# This is the object identity:
@full_path = full_path
@@ -71,34 +75,30 @@
# Effectively dirname and basename:
@root, _, @relative_path = full_path.rpartition(File::SEPARATOR)
end
end
-
def components
@components ||= @full_path.split(File::SEPARATOR)
end
+ def basename
+ self.components.last
+ end
+
# Ensure the path has an absolute root if it doesn't already:
def to_absolute(root)
if @root == "."
self.rebase(root)
else
self
end
end
attr :root
+ attr :full_path
- def to_str
- @full_path
- end
-
- def to_path
- @full_path
- end
-
def length
@full_path.length
end
def parts
@@ -108,48 +108,63 @@
def relative_path
@relative_path ||= Path.relative_path(@root.to_s, @full_path)
end
def relative_parts
- basename, _, filename = self.relative_path.rpartition(File::SEPARATOR)
+ dirname, _, basename = self.relative_path.rpartition(File::SEPARATOR)
- return basename, filename
+ return dirname, basename
end
- def +(extension)
+ def append(extension)
self.class.new(@full_path + extension, @root)
end
+ def +(path)
+ self.class.new(File.join(@full_path, path), @root)
+ end
+
def rebase(root)
self.class.new(File.join(root, relative_path), root)
end
def with(root: @root, extension: nil)
- self.class.new(File.join(root, extension ? relative_path + extension : relative_path), root)
+ # self.relative_path should be a string so using + to add an extension should be fine.
+ relative_path = extension ? self.relative_path + extension : self.relative_path
+
+ self.class.new(File.join(root, relative_path), root, relative_path)
end
def self.join(root, relative_path)
self.new(File.join(root, relative_path), root)
end
def shortest_path(root)
self.class.shortest_path(self, root)
end
+ def to_str
+ @full_path
+ end
+
+ def to_path
+ @full_path
+ end
+
def to_s
@full_path
end
def inspect
"#{@root.inspect}/#{relative_path.inspect}"
end
def hash
- @full_path.hash
+ [@root, @full_path].hash
end
def eql?(other)
- @full_path.eql?(other.to_s)
+ @root.eql?(other.root) and @full_path.eql?(other.full_path)
end
def ==(other)
self.to_s == other.to_s
end