Class: Filepath
- Inherits:
-
Object
- Object
- Filepath
- Includes:
- ContentChanges, ContentInfo, ContentTests, FilesystemChanges, FilesystemInfo, FilesystemTests, MetadataChanges, MetadataInfo, MetadataTests, SearchMethods
- Defined in:
- lib/filepath/filepath.rb
Defined Under Namespace
Modules: ContentChanges, ContentInfo, ContentTests, EnvironmentInfo, FilesystemChanges, FilesystemInfo, FilesystemTests, MetadataChanges, MetadataInfo, MetadataTests, MethodDelegation, SearchMethods
Constant Summary collapse
- SEPARATOR =
'/'.freeze
Class Method Summary collapse
- .getwd ⇒ Object
-
.join(*raw_paths) ⇒ Filepath
Creates a Filepath joining the given segments.
Instance Method Summary collapse
-
#+(extra_path) ⇒ Object
deprecated
Deprecated.
Use the #/ (slash) method instead. This method does not show clearly if a path is being added or if a string should be added to the filename
-
#/(extra_path) ⇒ Filepath
Appends another path to the current path.
-
#==(other) ⇒ boolean
Checks whether two paths are equivalent.
-
#=~(pattern) ⇒ Fixnum?
Matches a pattern against this path.
-
#absolute? ⇒ Boolean
Is this path absolute?.
-
#as_path ⇒ Filepath
The path itself.
-
#ascend(max_depth = nil) {|path| ... } ⇒ Filepath
Iterates over all the path directories, from the current path to the root.
-
#descend(max_depth = nil) {|path| ... } ⇒ Filepath
Iterates over all the directory that lead to the current path.
-
#each_segment {|path| ... } ⇒ Filepath
Iterates over all the path segments, from the leftmost to the rightmost.
-
#extension ⇒ String
(also: #ext)
The extension of the file.
- #extension?(ext = nil) ⇒ Object (also: #ext?)
-
#filename ⇒ Filepath
(also: #basename)
The filename component of the path.
-
#initialize(path) ⇒ Filepath
constructor
A new instance of Filepath.
-
#join(*extra_paths) ⇒ Filepath
Append multiple paths to the current path.
-
#normalized ⇒ Filepath
(also: #normalised)
Simplify paths that contain
.
and..
. -
#parent_dir ⇒ Filepath
The dir that contains the file.
-
#relative? ⇒ Boolean
Is this path relative?.
-
#relative_to(base) ⇒ Filepath
Calculates the relative path from a given directory.
-
#relative_to_file(base_file) ⇒ Filepath
Calculates the relative path from a given file.
-
#root? ⇒ Boolean
Is this path pointing to the root directory?.
-
#to_raw_string ⇒ String
(also: #to_raw_str)
This path converted to a String.
-
#to_s ⇒ String
This path converted to a String.
-
#with_extension(new_ext) ⇒ Object
(also: #replace_extension, #replace_ext, #sub_ext)
Replaces or removes the file extension.
-
#with_filename(new_path) ⇒ Filepath
(also: #with_basename, #replace_filename, #replace_basename)
Replace the path filename with the supplied path.
-
#without_extension ⇒ Filepath
(also: #remove_ext, #remove_extension)
Removes the file extension if present.
Methods included from SearchMethods
#directories, #entries, #files, #find, #links
Methods included from ContentChanges
Methods included from FilesystemTests
Methods included from FilesystemChanges
Methods included from FilesystemInfo
#absolute_path, #real_path, #resolve_link
Methods included from MetadataTests
Constructor Details
Class Method Details
.getwd ⇒ Object
963 964 965 |
# File 'lib/filepath/filepath.rb', line 963 def Filepath.getwd return Dir.getwd.as_path end |
.join(*raw_paths) ⇒ Filepath
Creates a Filepath joining the given segments.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/filepath/filepath.rb', line 25 def Filepath.join(*raw_paths) if (raw_paths.count == 1) && (raw_paths.first.is_a? Array) raw_paths = raw_paths.first end paths = raw_paths.map { |p| p.as_path } segs = [] paths.each { |path| segs += path.segments } return Filepath.new(segs) end |
Instance Method Details
#+(extra_path) ⇒ Object
77 78 79 80 |
# File 'lib/filepath/filepath.rb', line 77 def +(extra_path) warn "Filepath#+ is deprecated, use Filepath#/ instead." return self / extra_path end |
#/(extra_path) ⇒ Filepath
Appends another path to the current path.
57 58 59 |
# File 'lib/filepath/filepath.rb', line 57 def /(extra_path) return Filepath.join(self, extra_path) end |
#==(other) ⇒ boolean
this method compares the normalized versions of the paths
Checks whether two paths are equivalent.
Two paths are equivalent when they have the same normalized segments.
A relative and an absolute path will always be considered different. To compare relative paths to absolute path, expand first the relative path using Filepath::FilesystemInfo#absolute_path or Filepath::FilesystemInfo#real_path.
648 649 650 |
# File 'lib/filepath/filepath.rb', line 648 def ==(other) return self.normalized_segments == other.as_path.normalized_segments end |
#=~(pattern) ⇒ Fixnum?
this method operates on the normalized path
Matches a pattern against this path.
390 391 392 |
# File 'lib/filepath/filepath.rb', line 390 def =~(pattern) return self.to_s =~ pattern end |
#absolute? ⇒ Boolean
Is this path absolute?
FIXME: document what an absolute path is.
420 421 422 |
# File 'lib/filepath/filepath.rb', line 420 def absolute? return @segments.first == SEPARATOR # FIXME: windows, mac end |
#as_path ⇒ Filepath
Returns the path itself.
613 614 615 |
# File 'lib/filepath/filepath.rb', line 613 def as_path self end |
#ascend(max_depth = nil) {|path| ... } ⇒ Filepath
Iterates over all the path directories, from the current path to the root.
527 528 529 |
# File 'lib/filepath/filepath.rb', line 527 def ascend(max_depth = nil, &block) iterate(max_depth, :reverse_each, &block) end |
#descend(max_depth = nil) {|path| ... } ⇒ Filepath
Iterates over all the directory that lead to the current path.
561 562 563 |
# File 'lib/filepath/filepath.rb', line 561 def descend(max_depth = nil, &block) iterate(max_depth, :each, &block) end |
#each_segment {|path| ... } ⇒ Filepath
Iterates over all the path segments, from the leftmost to the rightmost.
491 492 493 494 |
# File 'lib/filepath/filepath.rb', line 491 def each_segment(&block) @segments.each(&block) return self end |
#extension ⇒ String Also known as: ext
The extension of the file.
The extension of a file are the characters after the last dot.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/filepath/filepath.rb', line 243 def extension filename = @segments.last num_dots = filename.count('.') if num_dots.zero? ext = nil elsif filename.start_with?('.') && num_dots == 1 ext = nil elsif filename.end_with?('.') ext = '' else ext = filename.split('.').last end return ext end |
#extension?(ext) ⇒ Object #extension? ⇒ Object Also known as: ext?
272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/filepath/filepath.rb', line 272 def extension?(ext = nil) cur_ext = self.extension if ext.nil? return !cur_ext.nil? else if ext.is_a? Regexp return !cur_ext.match(ext).nil? else return cur_ext == ext end end end |
#filename ⇒ Filepath Also known as: basename
The filename component of the path.
The filename is the component of a path that appears after the last path separator.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/filepath/filepath.rb', line 184 def filename segs = self.normalized_segments if self.root? || segs.empty? return ''.as_path end filename = segs.last return filename.as_path end |
#join(*extra_paths) ⇒ Filepath
Append multiple paths to the current path.
66 67 68 |
# File 'lib/filepath/filepath.rb', line 66 def join(*extra_paths) return Filepath.join(self, *extra_paths) end |
#normalized ⇒ Filepath Also known as: normalised
Simplify paths that contain .
and ..
.
The resulting path will be in normal form.
FIXME: document what normal form is.
460 461 462 |
# File 'lib/filepath/filepath.rb', line 460 def normalized return Filepath.join(self.normalized_segments) end |
#parent_dir ⇒ Filepath
The dir that contains the file
202 203 204 |
# File 'lib/filepath/filepath.rb', line 202 def parent_dir return self / '..' end |
#relative? ⇒ Boolean
Is this path relative?
FIXME: document what a relative path is.
439 440 441 |
# File 'lib/filepath/filepath.rb', line 439 def relative? return !self.absolute? end |
#relative_to(base) ⇒ Filepath
this method operates on the normalized paths
Calculates the relative path from a given directory.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/filepath/filepath.rb', line 114 def relative_to(base) base = base.as_path if self.absolute? != base.absolute? self_abs = self.absolute? ? "absolute" : "relative" base_abs = base.absolute? ? "absolute" : "relative" msg = "cannot compare: " msg += "`#{self}` is #{self_abs} while " msg += "`#{base}` is #{base_abs}" raise ArgumentError, msg end self_segs = self.normalized_segments base_segs = base.normalized_segments base_segs_tmp = base_segs.dup num_same = self_segs.find_index do |seg| base_segs_tmp.delete_at(0) != seg end # find_index returns nil if `self` is a subset of `base` num_same ||= self_segs.length num_parent_dirs = base_segs.length - num_same left_in_self = self_segs[num_same..-1] segs = [".."] * num_parent_dirs + left_in_self normalized_segs = normalized_relative_segs(segs) return Filepath.join(normalized_segs) end |
#relative_to_file(base_file) ⇒ Filepath
Calculates the relative path from a given file.
172 173 174 |
# File 'lib/filepath/filepath.rb', line 172 def relative_to_file(base_file) return relative_to(base_file.as_path.parent_dir) end |
#root? ⇒ Boolean
this method operates on the normalized paths
Is this path pointing to the root directory?
401 402 403 |
# File 'lib/filepath/filepath.rb', line 401 def root? return self.normalized_segments == [SEPARATOR] # FIXME: windows, mac end |
#to_raw_string ⇒ String Also known as: to_raw_str
This path converted to a String.
590 591 592 |
# File 'lib/filepath/filepath.rb', line 590 def to_raw_string @to_raw_string ||= join_segments(@segments) end |
#to_s ⇒ String
this method operates on the normalized path
Returns this path converted to a String.
601 602 603 |
# File 'lib/filepath/filepath.rb', line 601 def to_s to_str end |
#with_extension(new_ext) ⇒ Filepath #with_extension ⇒ Filepath Also known as: replace_extension, replace_ext, sub_ext
Replaces or removes the file extension.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/filepath/filepath.rb', line 330 def with_extension(new_ext) # FIXME: accept block orig_filename = filename.to_s if !self.extension? if new_ext.nil? new_filename = orig_filename else new_filename = orig_filename + '.' + new_ext end else if new_ext.nil? pattern = /\.[^.]*?\Z/ new_filename = orig_filename.sub(pattern, '') else pattern = Regexp.new('.' + extension + '\\Z') new_filename = orig_filename.sub(pattern, '.' + new_ext) end end segs = @segments[0..-2] segs << new_filename return Filepath.new(segs) end |
#with_filename(new_path) ⇒ Filepath Also known as: with_basename, replace_filename, replace_basename
Replace the path filename with the supplied path.
224 225 226 227 |
# File 'lib/filepath/filepath.rb', line 224 def with_filename(new_path) dir = self.parent_dir return dir / new_path end |
#without_extension ⇒ Filepath Also known as: remove_ext, remove_extension
Removes the file extension if present.
372 373 374 |
# File 'lib/filepath/filepath.rb', line 372 def without_extension return with_extension(nil) end |