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.
-
#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.
-
#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 mnenomic 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.
565 566 567 568 |
# File 'lib/pleasant_path/pathname.rb', line 565 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.
474 475 476 477 |
# File 'lib/pleasant_path/pathname.rb', line 474 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.
434 435 436 437 |
# File 'lib/pleasant_path/pathname.rb', line 434 def append_text(text) self.make_dirname.open('a'){|f| f.write(text) } self end |
#common_path(other) ⇒ Pathname
Computes the longest path that the Pathname and other have in common. See also File.common_path.
57 58 59 |
# File 'lib/pleasant_path/pathname.rb', line 57 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.
317 318 319 320 321 322 |
# File 'lib/pleasant_path/pathname.rb', line 317 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.
344 345 346 |
# File 'lib/pleasant_path/pathname.rb', line 344 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.
239 240 241 242 |
# File 'lib/pleasant_path/pathname.rb', line 239 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.
77 78 79 |
# File 'lib/pleasant_path/pathname.rb', line 77 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.
98 99 100 |
# File 'lib/pleasant_path/pathname.rb', line 98 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.
121 122 123 |
# File 'lib/pleasant_path/pathname.rb', line 121 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.
547 548 549 |
# File 'lib/pleasant_path/pathname.rb', line 547 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.
523 524 525 |
# File 'lib/pleasant_path/pathname.rb', line 523 def edit_text(&block) File.edit_text(self, &block) 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.
142 143 144 |
# File 'lib/pleasant_path/pathname.rb', line 142 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.
163 164 165 |
# File 'lib/pleasant_path/pathname.rb', line 163 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.
179 180 181 182 |
# File 'lib/pleasant_path/pathname.rb', line 179 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.
198 199 200 201 |
# File 'lib/pleasant_path/pathname.rb', line 198 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.
265 266 267 268 269 270 |
# File 'lib/pleasant_path/pathname.rb', line 265 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.
292 293 294 |
# File 'lib/pleasant_path/pathname.rb', line 292 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.)
498 499 500 |
# File 'lib/pleasant_path/pathname.rb', line 498 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.
363 364 365 |
# File 'lib/pleasant_path/pathname.rb', line 363 def rename_basename(new_basename) self.move(self.dirname / new_basename) 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.
390 391 392 393 394 395 |
# File 'lib/pleasant_path/pathname.rb', line 390 def rename_extname(new_extname) unless new_extname.start_with?(".") || new_extname.empty? new_extname = ".#{new_extname}" end self.move(self.sub_ext(new_extname)) 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.
219 220 221 222 223 |
# File 'lib/pleasant_path/pathname.rb', line 219 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.
454 455 456 457 |
# File 'lib/pleasant_path/pathname.rb', line 454 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.
413 414 415 416 |
# File 'lib/pleasant_path/pathname.rb', line 413 def write_text(text) self.make_dirname.open('w'){|f| f.write(text) } self end |