Class: Pathname
- Defined in:
- lib/pleasant_path/pathname.rb,
lib/pleasant_path/json/pathname.rb,
lib/pleasant_path/yaml/pathname.rb
Constant Summary collapse
- NULL =
File::NULLas a Pathname. On POSIX systems, this should be equivalent to Pathname.new(“/dev/null”). Pathname.new(File::NULL)
Instance Method Summary collapse
-
#^(sibling) ⇒ Pathname
Joins the parent (
dirname) of the Pathname with the argument. -
#append_file(source) ⇒ Pathname
Appends the contents of another file to the destination indicated by Pathname.
-
#append_lines(lines) ⇒ Pathname
Appends each object as a string plus a succeeding new line character (
$/) to the file indicated by the Pathname. -
#append_text(text) ⇒ Pathname
Appends given text to the file indicated by the Pathname, and returns the Pathname.
-
#chdir ⇒ Object
Changes the current working directory to the directory indicated by the Pathname.
-
#common_path(other) ⇒ Pathname
Computes the longest path that the Pathname and
otherhave in common. -
#copy(destination) ⇒ Pathname
Copies the file or directory indicated by the Pathname to the given destination, and returns that destination as a Pathname.
-
#copy_into(directory) ⇒ Pathname
Copies the file or directory indicated by the Pathname into the given directory, and returns the resultant path as a Pathname.
-
#delete! ⇒ Pathname
Recursively deletes the directory or file indicated by the Pathname, and returns the Pathname.
-
#dir_empty? ⇒ Boolean
True if the directory indicated by the Pathname contains no other directories or files.
-
#dirs ⇒ Array<Pathname>
Returns the immediate (non-recursive) child directories of the directory indicated by the Pathname.
-
#dirs_r ⇒ Array<Pathname>
Returns the recursively descended child directories of the directory indicated by the Pathname.
-
#edit_lines {|lines| ... } ⇒ Array<String>
Reads the contents of the file indicated by the Pathname into memory as an array of lines, and yields the array to the given block for editing.
-
#edit_text {|text| ... } ⇒ String
Reads the contents of the file indicated by the Pathname into memory as a string, and yields the string to the given block for editing.
-
#existence ⇒ Pathname?
Returns the Pathname if
exist?returns true, otherwise returns nil. -
#files ⇒ Array<Pathname>
Returns the immediate (non-recursive) child files of the directory indicated by the Pathname.
-
#files_r ⇒ Array<Pathname>
Returns the recursively descended child files of the directory indicated by the Pathname.
-
#load_json(options = {}) ⇒ Object
Reads the contents of the file indicated by the Pathname, and parses it as JSON.
-
#load_yaml ⇒ Object
Reads the contents of the file indicated by the Pathname, and parses it as YAML.
-
#make_dir ⇒ Pathname
Alias of Pathname#mkpath, but this method returns the Pathname.
-
#make_dirname ⇒ Pathname
Creates the parent (
dirname) directories of the Pathname if they do not exist, and returns the Pathname. -
#move(destination) ⇒ Pathname
Moves the file or directory indicated by the Pathname to the given destination, and returns that destination as a Pathname.
-
#move_into(directory) ⇒ Pathname
Moves the file or directory indicated by the Pathname into the given directory, and returns the resultant path as a Pathname.
-
#parentname ⇒ Pathname
Returns the
basenameof the Pathname’s parent directory. -
#read_json(options = {}) ⇒ nil, ...
Reads the contents of the file indicated by the Pathname, and parses it as JSON.
-
#read_lines ⇒ Array<String>
Reads from the file indicated by the Pathname all lines, and returns them as an array, end-of-line characters excluded.
-
#read_yaml ⇒ nil, ...
Reads the contents of the file indicated by the Pathname, and parses it as YAML.
-
#rename_basename(new_basename) ⇒ Pathname
Renames the file or directory indicated by the Pathname, but preserves its location as indicated by
dirname. -
#rename_extname(new_extname) ⇒ Pathname
Renames the file extension of the file indicated by the Pathname.
-
#to_pathname ⇒ Pathname
Returns the Pathname unmodified.
-
#touch_file ⇒ Pathname
Updates the modification time (mtime) and access time (atime) of the file indicated by the Pathname, and returns the Pathname.
-
#write_lines(lines) ⇒ Pathname
Writes each object as a string plus a succeeding new line character (
$/) to the file indicated by the Pathname. -
#write_text(text) ⇒ Pathname
Writes given text to the file indicated by the Pathname, and returns the Pathname.
Instance Method Details
#^(sibling) ⇒ Pathname
Joins the parent (dirname) of the Pathname with the argument. The mnemonic for this operator is that the resultant path goes up one directory level from the original, then goes down to the directory specified by the argument.
26 27 28 |
# File 'lib/pleasant_path/pathname.rb', line 26 def ^(sibling) self.dirname / sibling end |
#append_file(source) ⇒ Pathname
Appends the contents of another file to the destination indicated by Pathname. Returns the destination Pathname.
627 628 629 630 |
# File 'lib/pleasant_path/pathname.rb', line 627 def append_file(source) self.open("a"){|destination| IO::copy_stream(source, destination) } self end |
#append_lines(lines) ⇒ Pathname
Appends each object as a string plus a succeeding new line character ($/) to the file indicated by the Pathname. Returns the Pathname. The file is created if it does not exist. Any necessary parent directories are created if they do not exist.
536 537 538 539 |
# File 'lib/pleasant_path/pathname.rb', line 536 def append_lines(lines) self.make_dirname.open("a"){|f| f.write_lines(lines) } self end |
#append_text(text) ⇒ Pathname
Appends given text to the file indicated by the Pathname, and returns the Pathname. The file is created if it does not exist. Any necessary parent directories are created if they do not exist.
496 497 498 499 |
# File 'lib/pleasant_path/pathname.rb', line 496 def append_text(text) self.make_dirname.open("a"){|f| f.write(text) } self end |
#chdir ⇒ Pathname #chdir {|path| ... } ⇒ block_retval
Changes the current working directory to the directory indicated by the Pathname. If a block is given, it is called with the Pathname, and the original working directory is restored after the block exits.
Returns the return value of the block, if one is given. Otherwise, returns the Pathname.
Raises an exception if the directory indicated by the Pathname does not exist.
See also Dir::chdir.
214 215 216 217 218 219 220 221 222 223 |
# File 'lib/pleasant_path/pathname.rb', line 214 def chdir if block_given? Dir.chdir(self) do |dir| yield dir.to_pathname end else Dir.chdir(self) self end end |
#common_path(other) ⇒ Pathname
Computes the longest path that the Pathname and other have in common. See also File.common_path.
73 74 75 |
# File 'lib/pleasant_path/pathname.rb', line 73 def common_path(other) File.common_path([self.to_s, other.to_s]).to_pathname end |
#copy(destination) ⇒ Pathname
Copies the file or directory indicated by the Pathname to the given destination, and returns that destination as a Pathname. Creates any necessary parent directories if they do not exist. See also FileUtils.cp_r.
375 376 377 378 379 380 |
# File 'lib/pleasant_path/pathname.rb', line 375 def copy(destination) destination = destination.to_pathname destination.make_dirname FileUtils.cp_r(self, destination) destination end |
#copy_into(directory) ⇒ Pathname
Copies the file or directory indicated by the Pathname into the given directory, and returns the resultant path as a Pathname. Creates any necessary parent directories if they do not exist.
402 403 404 |
# File 'lib/pleasant_path/pathname.rb', line 402 def copy_into(directory) self.copy(directory / self.basename) end |
#delete! ⇒ Pathname
Recursively deletes the directory or file indicated by the Pathname, and returns the Pathname. Similar to Pathname#rmtree, but does not raise an exception if the file does not exist.
297 298 299 300 |
# File 'lib/pleasant_path/pathname.rb', line 297 def delete! self.rmtree if self.exist? self end |
#dir_empty? ⇒ Boolean
True if the directory indicated by the Pathname contains no other directories or files.
93 94 95 |
# File 'lib/pleasant_path/pathname.rb', line 93 def dir_empty? self.children(false).empty? end |
#dirs ⇒ Array<Pathname>
Returns the immediate (non-recursive) child directories of the directory indicated by the Pathname. Returned Pathnames are prefixed by the original Pathname.
114 115 116 |
# File 'lib/pleasant_path/pathname.rb', line 114 def dirs self.children.tap{|c| c.select!(&:dir?) } end |
#dirs_r ⇒ Array<Pathname>
Returns the recursively descended child directories of the directory indicated by the Pathname. Returned Pathnames are prefixed by the original Pathname.
137 138 139 |
# File 'lib/pleasant_path/pathname.rb', line 137 def dirs_r self.find.select(&:dir?).tap(&:shift) end |
#edit_lines {|lines| ... } ⇒ Array<String>
Reads the contents of the file indicated by the Pathname into memory as an array of lines, and yields the array to the given block for editing. Writes the return value of the block back to the file, overwriting previous contents. The $/ global string specifies what end-of-line characters to use for both reading and writing. Returns the array of lines that comprises the file’s new contents. See also File.edit_lines.
609 610 611 |
# File 'lib/pleasant_path/pathname.rb', line 609 def edit_lines(&block) File.edit_lines(self, &block) end |
#edit_text {|text| ... } ⇒ String
Reads the contents of the file indicated by the Pathname into memory as a string, and yields the string to the given block for editing. Writes the return value of the block back to the file, overwriting previous contents. Returns the file’s new contents. See also File.edit_text.
585 586 587 |
# File 'lib/pleasant_path/pathname.rb', line 585 def edit_text(&block) File.edit_text(self, &block) end |
#existence ⇒ Pathname?
Returns the Pathname if exist? returns true, otherwise returns nil.
52 53 54 |
# File 'lib/pleasant_path/pathname.rb', line 52 def existence self if self.exist? end |
#files ⇒ Array<Pathname>
Returns the immediate (non-recursive) child files of the directory indicated by the Pathname. Returned Pathnames are prefixed by the original Pathname.
158 159 160 |
# File 'lib/pleasant_path/pathname.rb', line 158 def files self.children.tap{|c| c.select!(&:file?) } end |
#files_r ⇒ Array<Pathname>
Returns the recursively descended child files of the directory indicated by the Pathname. Returned Pathnames are prefixed by the original Pathname.
179 180 181 |
# File 'lib/pleasant_path/pathname.rb', line 179 def files_r self.find.select(&:file?) end |
#load_json(options = {}) ⇒ Object
Reads the contents of the file indicated by the Pathname, and parses it as JSON. The parser will use type information embedded in the JSON to deserialize custom types. This is UNSAFE for JSON from an untrusted source. To consume untrusted JSON, use #read_json instead.
For information about available options, see JSON.parse.
For information about serializing custom types to JSON, see the JSON readme.
54 55 56 |
# File 'lib/pleasant_path/json/pathname.rb', line 54 def load_json( = {}) self.open("r"){|f| JSON.load(f, nil, ) } end |
#load_yaml ⇒ Object
Reads the contents of the file indicated by the Pathname, and parses it as YAML. The parser will use type information embedded in the YAML to deserialize custom types. This is UNSAFE for YAML from an untrusted source. To consume untrusted YAML, use #read_yaml instead.
32 33 34 |
# File 'lib/pleasant_path/yaml/pathname.rb', line 32 def load_yaml YAML.load_file(self) end |
#make_dir ⇒ Pathname
Alias of Pathname#mkpath, but this method returns the Pathname.
237 238 239 240 |
# File 'lib/pleasant_path/pathname.rb', line 237 def make_dir self.mkpath self end |
#make_dirname ⇒ Pathname
Creates the parent (dirname) directories of the Pathname if they do not exist, and returns the Pathname.
256 257 258 259 |
# File 'lib/pleasant_path/pathname.rb', line 256 def make_dirname self.dirname.make_dir self end |
#move(destination) ⇒ Pathname
Moves the file or directory indicated by the Pathname to the given destination, and returns that destination as a Pathname. Creates any necessary parent directories if they do not exist. See also FileUtils.mv.
323 324 325 326 327 328 |
# File 'lib/pleasant_path/pathname.rb', line 323 def move(destination) destination = destination.to_pathname destination.make_dirname FileUtils.mv(self, destination) destination end |
#move_into(directory) ⇒ Pathname
Moves the file or directory indicated by the Pathname into the given directory, and returns the resultant path as a Pathname. Creates any necessary parent directories if they do not exist.
350 351 352 |
# File 'lib/pleasant_path/pathname.rb', line 350 def move_into(directory) self.move(directory / self.basename) end |
#parentname ⇒ Pathname
Returns the basename of the Pathname’s parent directory.
36 37 38 |
# File 'lib/pleasant_path/pathname.rb', line 36 def parentname self.dirname.basename end |
#read_json(options = {}) ⇒ nil, ...
Reads the contents of the file indicated by the Pathname, and parses it as JSON. The returned result will be a basic Ruby data structure, namely, one of: nil, true, false, a Numeric, a String, an Array, or a Hash.
For information about available options, see JSON.parse.
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/pleasant_path/json/pathname.rb', line 19 def read_json( = {}) = { quirks_mode: true, allow_nan: true, max_nesting: false, create_additions: false, }.merge() JSON.parse(self.read_text, ) end |
#read_lines ⇒ Array<String>
Reads from the file indicated by the Pathname all lines, and returns them as an array, end-of-line characters excluded. The $/ global string specifies what end-of-line characters to look for. See also IO#read_lines.
(Not to be confused with Pathname#readlines which retains end-of-line characters in every string it returns.)
560 561 562 |
# File 'lib/pleasant_path/pathname.rb', line 560 def read_lines self.open("r"){|f| f.read_lines } end |
#read_yaml ⇒ nil, ...
Reads the contents of the file indicated by the Pathname, and parses it as YAML. The returned result will be a basic Ruby data structure, namely, one of: nil, true, false, a Numeric, a String, an Array, or a Hash.
14 15 16 |
# File 'lib/pleasant_path/yaml/pathname.rb', line 14 def read_yaml self.open("r"){|f| YAML.safe_load(f, [], [], false, self) } end |
#rename_basename(new_basename) ⇒ Pathname
Renames the file or directory indicated by the Pathname, but preserves its location as indicated by dirname. Returns the resultant path as a Pathname.
421 422 423 424 425 |
# File 'lib/pleasant_path/pathname.rb', line 421 def rename_basename(new_basename) new_path = self.dirname / new_basename self.rename(new_path) new_path end |
#rename_extname(new_extname) ⇒ Pathname
Renames the file extension of the file indicated by the Pathname. If the file has no extension, the new extension is appended.
450 451 452 453 454 455 456 457 |
# File 'lib/pleasant_path/pathname.rb', line 450 def rename_extname(new_extname) unless new_extname.start_with?(".") || new_extname.empty? new_extname = ".#{new_extname}" end new_path = self.sub_ext(new_extname) self.rename(new_path) new_path end |
#to_pathname ⇒ Pathname
Returns the Pathname unmodified. Exists for parity with String#to_pathname.
12 13 14 |
# File 'lib/pleasant_path/pathname.rb', line 12 def to_pathname self end |
#touch_file ⇒ Pathname
Updates the modification time (mtime) and access time (atime) of the file indicated by the Pathname, and returns the Pathname. Creates the file and any necessary parent directories if they do not exist. See also FileUtils.touch.
277 278 279 280 281 |
# File 'lib/pleasant_path/pathname.rb', line 277 def touch_file self.make_dirname FileUtils.touch(self) self end |
#write_lines(lines) ⇒ Pathname
Writes each object as a string plus a succeeding new line character ($/) to the file indicated by the Pathname. Returns the Pathname. The file is overwritten if it already exists. Any necessary parent directories are created if they do not exist.
516 517 518 519 |
# File 'lib/pleasant_path/pathname.rb', line 516 def write_lines(lines) self.make_dirname.open("w"){|f| f.write_lines(lines) } self end |
#write_text(text) ⇒ Pathname
Writes given text to the file indicated by the Pathname, and returns the Pathname. The file is overwritten if it already exists. Any necessary parent directories are created if they do not exist.
475 476 477 478 |
# File 'lib/pleasant_path/pathname.rb', line 475 def write_text(text) self.make_dirname.open("w"){|f| f.write(text) } self end |