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
- SEPARATOR =
'/'.freeze
Class Method Summary (collapse)
- + (Object) getwd
-
+ (FilePath) join(*raw_paths)
Creates a FilePath joining the given segments.
Instance Method Summary (collapse)
-
- (Object) +(extra_path)
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
-
- (FilePath) /(extra_path)
Appends another path to the current path.
-
- (boolean) ==(other)
Checks whether two paths are equivalent.
-
- (Fixnum?) =~(pattern)
Matches a pattern against this path.
-
- (Boolean) absolute?
Is this path absolute?.
-
- (FilePath) as_path
The path itself.
-
- (FilePath) ascend(max_depth = nil) {|path| ... }
Iterates over all the path directories, from the current path to the root.
-
- (FilePath) descend(max_depth = nil) {|path| ... }
Iterates over all the directory that lead to the current path.
-
- (FilePath) each_segment {|path| ... }
Iterates over all the path segments, from the leftmost to the rightmost.
-
- (String) extension
(also: #ext)
The extension of the file.
- - (Object) extension?(ext = nil) (also: #ext?)
-
- (FilePath) filename
(also: #basename)
The filename component of the path.
-
- (FilePath) initialize(path)
constructor
A new instance of FilePath.
-
- (FilePath) join(*extra_paths)
Append multiple paths to the current path.
-
- (FilePath) normalized
(also: #normalised)
Simplify paths that contain
.and... -
- (FilePath) parent_dir
The dir that contains the file.
-
- (Boolean) relative?
Is this path relative?.
-
- (FilePath) relative_to(base)
Calculates the relative path from a given directory.
-
- (FilePath) relative_to_file(base_file)
Calculates the relative path from a given file.
-
- (Boolean) root?
Is this path pointing to the root directory?.
-
- (String) to_raw_string
(also: #to_raw_str)
This path converted to a String.
-
- (String) to_s
This path converted to a String.
-
- (Object) with_extension(new_ext)
(also: #replace_extension, #replace_ext, #sub_ext)
Replaces or removes the file extension.
-
- (FilePath) with_filename(new_path)
(also: #with_basename, #replace_filename, #replace_basename)
Replace the path filename with the supplied path.
-
- (FilePath) without_extension
(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
- (FilePath) initialize(path)
A new instance of FilePath
8 9 10 11 12 13 14 15 16 |
# File 'lib/filepath/filepath.rb', line 8 def initialize(path) if path.is_a? FilePath @segments = path.segments elsif path.is_a? Array @segments = path else @segments = split_path_string(path.to_str) end end |
Class Method Details
+ (Object) getwd
964 965 966 |
# File 'lib/filepath/filepath.rb', line 964 def FilePath.getwd return Dir.getwd.as_path end |
+ (FilePath) join(*raw_paths)
Creates a FilePath joining the given segments.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/filepath/filepath.rb', line 26 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
- (Object) +(extra_path)
78 79 80 81 |
# File 'lib/filepath/filepath.rb', line 78 def +(extra_path) warn "FilePath#+ is deprecated, use FilePath#/ instead." return self / extra_path end |
- (FilePath) /(extra_path)
Appends another path to the current path.
58 59 60 |
# File 'lib/filepath/filepath.rb', line 58 def /(extra_path) return FilePath.join(self, extra_path) end |
- (boolean) ==(other)
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.
649 650 651 |
# File 'lib/filepath/filepath.rb', line 649 def ==(other) return self.normalized_segments == other.as_path.normalized_segments end |
- (Fixnum?) =~(pattern)
this method operates on the normalized path
Matches a pattern against this path.
391 392 393 |
# File 'lib/filepath/filepath.rb', line 391 def =~(pattern) return self.to_s =~ pattern end |
- (Boolean) absolute?
Is this path absolute?
FIXME: document what an absolute path is.
421 422 423 |
# File 'lib/filepath/filepath.rb', line 421 def absolute? return @segments.first == SEPARATOR # FIXME: windows, mac end |
- (FilePath) as_path
The path itself.
614 615 616 |
# File 'lib/filepath/filepath.rb', line 614 def as_path self end |
- (FilePath) ascend(max_depth = nil) {|path| ... }
Iterates over all the path directories, from the current path to the root.
528 529 530 |
# File 'lib/filepath/filepath.rb', line 528 def ascend(max_depth = nil, &block) iterate(max_depth, :reverse_each, &block) end |
- (FilePath) descend(max_depth = nil) {|path| ... }
Iterates over all the directory that lead to the current path.
562 563 564 |
# File 'lib/filepath/filepath.rb', line 562 def descend(max_depth = nil, &block) iterate(max_depth, :each, &block) end |
- (FilePath) each_segment {|path| ... }
Iterates over all the path segments, from the leftmost to the rightmost.
492 493 494 495 |
# File 'lib/filepath/filepath.rb', line 492 def each_segment(&block) @segments.each(&block) return self end |
- (String) extension Also known as: ext
The extension of the file.
The extension of a file are the characters after the last dot.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/filepath/filepath.rb', line 244 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 |
- (Object) extension?(ext) - (Object) extension? Also known as: ext?
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/filepath/filepath.rb', line 273 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 |
- (FilePath) filename 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.
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/filepath/filepath.rb', line 185 def filename segs = self.normalized_segments if self.root? || segs.empty? return ''.as_path end filename = segs.last return filename.as_path end |
- (FilePath) join(*extra_paths)
Append multiple paths to the current path.
67 68 69 |
# File 'lib/filepath/filepath.rb', line 67 def join(*extra_paths) return FilePath.join(self, *extra_paths) end |
- (FilePath) normalized Also known as: normalised
Simplify paths that contain . and ...
The resulting path will be in normal form.
FIXME: document what normal form is.
461 462 463 |
# File 'lib/filepath/filepath.rb', line 461 def normalized return FilePath.join(self.normalized_segments) end |
- (FilePath) parent_dir
The dir that contains the file
203 204 205 |
# File 'lib/filepath/filepath.rb', line 203 def parent_dir return self / '..' end |
- (Boolean) relative?
Is this path relative?
FIXME: document what a relative path is.
440 441 442 |
# File 'lib/filepath/filepath.rb', line 440 def relative? return !self.absolute? end |
- (FilePath) relative_to(base)
this method operates on the normalized paths
Calculates the relative path from a given directory.
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 145 |
# File 'lib/filepath/filepath.rb', line 115 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 |
- (FilePath) relative_to_file(base_file)
Calculates the relative path from a given file.
173 174 175 |
# File 'lib/filepath/filepath.rb', line 173 def relative_to_file(base_file) return relative_to(base_file.as_path.parent_dir) end |
- (Boolean) root?
this method operates on the normalized paths
Is this path pointing to the root directory?
402 403 404 |
# File 'lib/filepath/filepath.rb', line 402 def root? return self.normalized_segments == [SEPARATOR] # FIXME: windows, mac end |
- (String) to_raw_string Also known as: to_raw_str
This path converted to a String.
591 592 593 |
# File 'lib/filepath/filepath.rb', line 591 def to_raw_string @to_raw_string ||= join_segments(@segments) end |
- (String) to_s
this method operates on the normalized path
This path converted to a String
602 603 604 |
# File 'lib/filepath/filepath.rb', line 602 def to_s to_str end |
- (FilePath) with_extension(new_ext) - (FilePath) with_extension Also known as: replace_extension, replace_ext, sub_ext
Replaces or removes the file extension.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/filepath/filepath.rb', line 331 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 |
- (FilePath) with_filename(new_path) Also known as: with_basename, replace_filename, replace_basename
Replace the path filename with the supplied path.
225 226 227 228 |
# File 'lib/filepath/filepath.rb', line 225 def with_filename(new_path) dir = self.parent_dir return dir / new_path end |
- (FilePath) without_extension Also known as: remove_ext, remove_extension
Removes the file extension if present.
373 374 375 |
# File 'lib/filepath/filepath.rb', line 373 def without_extension return with_extension(nil) end |