lib/io_streams/path.rb in iostreams-1.0.0.beta vs lib/io_streams/path.rb in iostreams-1.0.0.beta2
- old
+ new
@@ -1,8 +1,8 @@
module IOStreams
class Path < IOStreams::Stream
- attr_reader :path
+ attr_accessor :path
def initialize(path)
raise(ArgumentError, 'Path cannot be nil') if path.nil?
raise(ArgumentError, "Path must be a string: #{path.inspect}, class: #{path.class}") unless path.is_a?(String)
@@ -16,15 +16,15 @@
def join(*elements)
return self if elements.empty?
elements = elements.collect(&:to_s)
relative = ::File.join(*elements)
- if relative.start_with?(path)
- self.class.new(relative)
- else
- self.class.new(::File.join(path, relative))
- end
+
+ new_path = dup
+ new_path.streams = nil
+ new_path.path = relative.start_with?(path) ? relative : ::File.join(path, relative)
+ new_path
end
def relative?
!absolute?
end
@@ -101,23 +101,24 @@
delete
target
end
# Returns [IOStreams::Path] the directory for this file.
- # Returns `nil` if no `file_name` was set.
#
# If `path` does not include a directory name then "." is returned.
#
# IOStreams.path("test.rb").directory #=> "."
# IOStreams.path("a/b/d/test.rb").directory #=> "a/b/d"
# IOStreams.path(".a/b/d/test.rb").directory #=> ".a/b/d"
# IOStreams.path("foo.").directory #=> "."
# IOStreams.path("test").directory #=> "."
# IOStreams.path(".profile").directory #=> "."
def directory
- file_name = streams.file_name
- self.class.new(::File.dirname(file_name)) if file_name
+ new_path = dup
+ new_path.streams = nil
+ new_path.path = ::File.dirname(path)
+ new_path
end
# When path is a file, deletes this file.
# When path is a directory, attempts to delete this directory. If the directory contains
# any children it will fail.
@@ -164,15 +165,15 @@
# - empty?
# - find(ignore_error: true) - Find.find
# Paths are sortable by name
def <=>(other)
- path <=> other.to_s
+ path <=> other.path
end
# Compare by path name, ignore streams
def ==(other)
- path == other.to_s
+ path == other.path
end
def inspect
str = "#<#{self.class.name}:#{path}"
str << " @streams=#{streams.streams.inspect}" if streams.streams