pleasant_path 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/CHANGELOG.md +27 -7
- data/Gemfile +4 -1
- data/README.md +65 -66
- data/Rakefile +0 -13
- data/lib/pleasant_path/enumerable.rb +8 -8
- data/lib/pleasant_path/file.rb +10 -10
- data/lib/pleasant_path/io.rb +5 -5
- data/lib/pleasant_path/json/object.rb +5 -5
- data/lib/pleasant_path/json/pathname.rb +18 -17
- data/lib/pleasant_path/pathname.rb +88 -134
- data/lib/pleasant_path/string.rb +0 -35
- data/lib/pleasant_path/version.rb +1 -1
- data/lib/pleasant_path/yaml/object.rb +4 -4
- data/lib/pleasant_path/yaml/pathname.rb +13 -19
- data/pleasant_path.gemspec +11 -12
- metadata +10 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bf7518ec3e9771e1feeea6211f4aa6e60804166a861d83d481d1a6eb9275874f
|
4
|
+
data.tar.gz: d227b85723683286ee43921625b68e08cfeb656443d4e7ee90d2ff3538f7f817
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12c3fff1b2a1cd134c10176cca26b3d7cf0c8722fa1e3925fa7805bbc652a8886b98bd5d15df96d23609016090339a8088f31f0b66a7444c3af2d1d37b808e1d
|
7
|
+
data.tar.gz: e6d990d3271a9b89e3ae68b31e06c7a520f75b12d0a0baf2a528985551bed69f6700d012d395f91c0577ed769e736ac853373b0088c0097f4c033e58aa1770fe
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
## 2.0.0
|
2
|
+
|
3
|
+
* [BREAKING] Drop support for Ruby < 2.6
|
4
|
+
* [BREAKING] Remove `Pathname#dir_empty?`
|
5
|
+
* Use `Pathname#empty?` instead
|
6
|
+
* [BREAKING] Remove `Pathname#touch_file`
|
7
|
+
* Use `Pathname#make_file` instead
|
8
|
+
* [BREAKING] Remove `String#^`
|
9
|
+
* [BREAKING] Remove `String#glob`
|
10
|
+
* [BREAKING] Cease creating parent directories in `Pathname#move`
|
11
|
+
* [BREAKING] Cease creating parent directories in `Pathname#copy`
|
12
|
+
* [BREAKING] Cease prepending dot in `Pathname#rename_extname`
|
13
|
+
* [BREAKING] Change `Pathname#edit_text` to return the Pathname
|
14
|
+
* [BREAKING] Change `Pathname#edit_lines` to return the Pathname
|
15
|
+
|
16
|
+
|
1
17
|
## 1.3.0
|
2
18
|
|
3
19
|
* Add `Pathname#find_dirs`
|
@@ -6,8 +22,8 @@
|
|
6
22
|
* Add `Pathname#available_name`
|
7
23
|
* Add `Pathname#move_as`
|
8
24
|
* Add `Pathname#copy_as`
|
9
|
-
* Add `eol` parameter to line-oriented methods (
|
10
|
-
`Pathname#
|
25
|
+
* Add `eol` parameter to line-oriented methods (e.g.
|
26
|
+
`Pathname#read_lines`, `Pathname#write_lines`, etc)
|
11
27
|
* Move Array methods to Enumerable
|
12
28
|
* Use `JSON.dump_default_options` and `JSON.load_default_options` in
|
13
29
|
JSON-related API
|
@@ -17,16 +33,20 @@
|
|
17
33
|
|
18
34
|
* Add `Pathname#existence`
|
19
35
|
* Add `Pathname#chdir`
|
20
|
-
* Fix `Object#write_to_yaml` to
|
36
|
+
* Fix `Object#write_to_yaml` to create parent directories as necessary
|
21
37
|
|
22
38
|
|
23
39
|
## 1.1.0
|
24
40
|
|
25
|
-
* Add `Pathname#copy`
|
26
|
-
* Add `Pathname#
|
27
|
-
* Add `
|
41
|
+
* Add `Pathname#copy`
|
42
|
+
* Add `Pathname#copy_into`
|
43
|
+
* Add `Pathname#rename_basename`
|
44
|
+
* Add `Pathname#rename_extname`
|
45
|
+
* Add `File.common_path`
|
46
|
+
* Add `Pathname#common_path`
|
28
47
|
* Add `Pathname::NULL`
|
29
|
-
* Add JSON-related
|
48
|
+
* Add JSON-related API
|
49
|
+
* Add YAML-related API
|
30
50
|
* Fix `File.edit_text` and `File.edit_lines` to properly truncate
|
31
51
|
|
32
52
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# pleasant_path
|
1
|
+
# pleasant_path [](https://travis-ci.org/jonathanhefner/pleasant_path)
|
2
2
|
|
3
3
|
A [fluent API] for pleasant file IO, written as extensions to core Ruby
|
4
4
|
objects. See API listing below, or browse the [full documentation].
|
5
5
|
|
6
6
|
[fluent API]: https://en.wikipedia.org/wiki/Fluent_interface
|
7
|
-
[full documentation]:
|
7
|
+
[full documentation]: https://www.rubydoc.info/gems/pleasant_path/
|
8
8
|
|
9
9
|
|
10
10
|
## Examples
|
@@ -22,58 +22,58 @@ objects. See API listing below, or browse the [full documentation].
|
|
22
22
|
|
23
23
|
The following methods are available:
|
24
24
|
|
25
|
-
- [Pathname](
|
26
|
-
- [::NULL](
|
27
|
-
- [#^](
|
28
|
-
- [#append_file](
|
29
|
-
- [#append_lines](
|
30
|
-
- [#append_text](
|
31
|
-
- [#available_name](
|
32
|
-
- [#chdir](
|
33
|
-
- [#common_path](
|
34
|
-
- [#copy](
|
35
|
-
- [#copy_as](
|
36
|
-
- [#copy_into](
|
37
|
-
- [#delete!](
|
38
|
-
- [#dir?](
|
39
|
-
- [#dirs](
|
40
|
-
- [#dirs_r](
|
41
|
-
- [#edit_lines](
|
42
|
-
- [#edit_text](
|
43
|
-
- [#existence](
|
44
|
-
- [#files](
|
45
|
-
- [#files_r](
|
46
|
-
- [#find_dirs](
|
47
|
-
- [#find_files](
|
48
|
-
- [#make_dir](
|
49
|
-
- [#make_dirname](
|
50
|
-
- [#make_file](
|
51
|
-
- [#move](
|
52
|
-
- [#move_as](
|
53
|
-
- [#move_into](
|
54
|
-
- [#parentname](
|
55
|
-
- [#read_lines](
|
56
|
-
- [#rename_basename](
|
57
|
-
- [#rename_extname](
|
58
|
-
- [#to_pathname](
|
59
|
-
- [#write_lines](
|
60
|
-
- [#write_text](
|
61
|
-
- [String](
|
62
|
-
- [#/](
|
63
|
-
- [#append_to_file](
|
64
|
-
- [#path](
|
65
|
-
- [#to_pathname](
|
66
|
-
- [#write_to_file](
|
67
|
-
- [Enumerable](
|
68
|
-
- [#append_to_file](
|
69
|
-
- [#write_to_file](
|
70
|
-
- [File](
|
71
|
-
- [.common_path](
|
72
|
-
- [.edit_lines](
|
73
|
-
- [.edit_text](
|
74
|
-
- [IO](
|
75
|
-
- [#read_lines](
|
76
|
-
- [#write_lines](
|
25
|
+
- [Pathname](https://www.rubydoc.info/gems/pleasant_path/Pathname)
|
26
|
+
- [::NULL](https://www.rubydoc.info/gems/pleasant_path/Pathname#NULL-constant)
|
27
|
+
- [#^](https://www.rubydoc.info/gems/pleasant_path/Pathname:%5E)
|
28
|
+
- [#append_file](https://www.rubydoc.info/gems/pleasant_path/Pathname:append_file)
|
29
|
+
- [#append_lines](https://www.rubydoc.info/gems/pleasant_path/Pathname:append_lines)
|
30
|
+
- [#append_text](https://www.rubydoc.info/gems/pleasant_path/Pathname:append_text)
|
31
|
+
- [#available_name](https://www.rubydoc.info/gems/pleasant_path/Pathname:available_name)
|
32
|
+
- [#chdir](https://www.rubydoc.info/gems/pleasant_path/Pathname:chdir)
|
33
|
+
- [#common_path](https://www.rubydoc.info/gems/pleasant_path/Pathname:common_path)
|
34
|
+
- [#copy](https://www.rubydoc.info/gems/pleasant_path/Pathname:copy)
|
35
|
+
- [#copy_as](https://www.rubydoc.info/gems/pleasant_path/Pathname:copy_as)
|
36
|
+
- [#copy_into](https://www.rubydoc.info/gems/pleasant_path/Pathname:copy_into)
|
37
|
+
- [#delete!](https://www.rubydoc.info/gems/pleasant_path/Pathname:delete%21)
|
38
|
+
- [#dir?](https://www.rubydoc.info/gems/pleasant_path/Pathname:dir%3F)
|
39
|
+
- [#dirs](https://www.rubydoc.info/gems/pleasant_path/Pathname:dirs)
|
40
|
+
- [#dirs_r](https://www.rubydoc.info/gems/pleasant_path/Pathname:dirs_r)
|
41
|
+
- [#edit_lines](https://www.rubydoc.info/gems/pleasant_path/Pathname:edit_lines)
|
42
|
+
- [#edit_text](https://www.rubydoc.info/gems/pleasant_path/Pathname:edit_text)
|
43
|
+
- [#existence](https://www.rubydoc.info/gems/pleasant_path/Pathname:existence)
|
44
|
+
- [#files](https://www.rubydoc.info/gems/pleasant_path/Pathname:files)
|
45
|
+
- [#files_r](https://www.rubydoc.info/gems/pleasant_path/Pathname:files_r)
|
46
|
+
- [#find_dirs](https://www.rubydoc.info/gems/pleasant_path/Pathname:find_dirs)
|
47
|
+
- [#find_files](https://www.rubydoc.info/gems/pleasant_path/Pathname:find_files)
|
48
|
+
- [#make_dir](https://www.rubydoc.info/gems/pleasant_path/Pathname:make_dir)
|
49
|
+
- [#make_dirname](https://www.rubydoc.info/gems/pleasant_path/Pathname:make_dirname)
|
50
|
+
- [#make_file](https://www.rubydoc.info/gems/pleasant_path/Pathname:make_file)
|
51
|
+
- [#move](https://www.rubydoc.info/gems/pleasant_path/Pathname:move)
|
52
|
+
- [#move_as](https://www.rubydoc.info/gems/pleasant_path/Pathname:move_as)
|
53
|
+
- [#move_into](https://www.rubydoc.info/gems/pleasant_path/Pathname:move_into)
|
54
|
+
- [#parentname](https://www.rubydoc.info/gems/pleasant_path/Pathname:parentname)
|
55
|
+
- [#read_lines](https://www.rubydoc.info/gems/pleasant_path/Pathname:read_lines)
|
56
|
+
- [#rename_basename](https://www.rubydoc.info/gems/pleasant_path/Pathname:rename_basename)
|
57
|
+
- [#rename_extname](https://www.rubydoc.info/gems/pleasant_path/Pathname:rename_extname)
|
58
|
+
- [#to_pathname](https://www.rubydoc.info/gems/pleasant_path/Pathname:to_pathname)
|
59
|
+
- [#write_lines](https://www.rubydoc.info/gems/pleasant_path/Pathname:write_lines)
|
60
|
+
- [#write_text](https://www.rubydoc.info/gems/pleasant_path/Pathname:write_text)
|
61
|
+
- [String](https://www.rubydoc.info/gems/pleasant_path/String)
|
62
|
+
- [#/](https://www.rubydoc.info/gems/pleasant_path/String:%2F)
|
63
|
+
- [#append_to_file](https://www.rubydoc.info/gems/pleasant_path/String:append_to_file)
|
64
|
+
- [#path](https://www.rubydoc.info/gems/pleasant_path/String:path)
|
65
|
+
- [#to_pathname](https://www.rubydoc.info/gems/pleasant_path/String:to_pathname)
|
66
|
+
- [#write_to_file](https://www.rubydoc.info/gems/pleasant_path/String:write_to_file)
|
67
|
+
- [Enumerable](https://www.rubydoc.info/gems/pleasant_path/Enumerable)
|
68
|
+
- [#append_to_file](https://www.rubydoc.info/gems/pleasant_path/Enumerable:append_to_file)
|
69
|
+
- [#write_to_file](https://www.rubydoc.info/gems/pleasant_path/Enumerable:write_to_file)
|
70
|
+
- [File](https://www.rubydoc.info/gems/pleasant_path/File)
|
71
|
+
- [.common_path](https://www.rubydoc.info/gems/pleasant_path/File.common_path)
|
72
|
+
- [.edit_lines](https://www.rubydoc.info/gems/pleasant_path/File.edit_lines)
|
73
|
+
- [.edit_text](https://www.rubydoc.info/gems/pleasant_path/File.edit_text)
|
74
|
+
- [IO](https://www.rubydoc.info/gems/pleasant_path/IO)
|
75
|
+
- [#read_lines](https://www.rubydoc.info/gems/pleasant_path/IO:read_lines)
|
76
|
+
- [#write_lines](https://www.rubydoc.info/gems/pleasant_path/IO:write_lines)
|
77
77
|
|
78
78
|
|
79
79
|
## JSON-related and YAML-related API
|
@@ -90,30 +90,30 @@ require "pleasant_path/json"
|
|
90
90
|
require "pleasant_path/yaml"
|
91
91
|
```
|
92
92
|
|
93
|
-
[JSON module]: https://ruby-
|
94
|
-
[YAML module]: https://ruby-
|
93
|
+
[JSON module]: https://docs.ruby-lang.org/en/master/JSON.html
|
94
|
+
[YAML module]: https://docs.ruby-lang.org/en/master/YAML.html
|
95
95
|
|
96
96
|
The following methods are available:
|
97
97
|
|
98
98
|
- Object
|
99
|
-
- [write_to_json](
|
100
|
-
- [write_to_yaml](
|
99
|
+
- [write_to_json](https://www.rubydoc.info/gems/pleasant_path/Object:write_to_json)
|
100
|
+
- [write_to_yaml](https://www.rubydoc.info/gems/pleasant_path/Object:write_to_yaml)
|
101
101
|
- Pathname
|
102
|
-
- [load_json](
|
103
|
-
- [load_yaml](
|
104
|
-
- [read_json](
|
105
|
-
- [read_yaml](
|
102
|
+
- [load_json](https://www.rubydoc.info/gems/pleasant_path/Pathname:load_json)
|
103
|
+
- [load_yaml](https://www.rubydoc.info/gems/pleasant_path/Pathname:load_yaml)
|
104
|
+
- [read_json](https://www.rubydoc.info/gems/pleasant_path/Pathname:read_json)
|
105
|
+
- [read_yaml](https://www.rubydoc.info/gems/pleasant_path/Pathname:read_yaml)
|
106
106
|
|
107
107
|
|
108
108
|
## Installation
|
109
109
|
|
110
|
-
Install
|
110
|
+
Install the [gem](https://rubygems.org/gems/pleasant_path):
|
111
111
|
|
112
112
|
```bash
|
113
113
|
$ gem install pleasant_path
|
114
114
|
```
|
115
115
|
|
116
|
-
Then require in your Ruby
|
116
|
+
Then require in your Ruby code:
|
117
117
|
|
118
118
|
```ruby
|
119
119
|
require "pleasant_path"
|
@@ -122,8 +122,7 @@ require "pleasant_path"
|
|
122
122
|
|
123
123
|
## Contributing
|
124
124
|
|
125
|
-
Run `rake test` to run the tests.
|
126
|
-
interactive prompt that pre-loads the project code.
|
125
|
+
Run `rake test` to run the tests.
|
127
126
|
|
128
127
|
|
129
128
|
## License
|
data/Rakefile
CHANGED
@@ -1,18 +1,5 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rake/testtask"
|
3
|
-
require "yard"
|
4
|
-
|
5
|
-
|
6
|
-
YARD::Rake::YardocTask.new(:doc) do |t|
|
7
|
-
end
|
8
|
-
|
9
|
-
desc "Launch IRB with this gem pre-loaded"
|
10
|
-
task :irb do
|
11
|
-
require "pleasant_path"
|
12
|
-
require "irb"
|
13
|
-
ARGV.clear
|
14
|
-
IRB.start
|
15
|
-
end
|
16
3
|
|
17
4
|
Rake::TestTask.new(:test) do |t|
|
18
5
|
t.libs << "test"
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module Enumerable
|
2
2
|
|
3
|
-
# Writes each object in the Enumerable as a string plus
|
4
|
-
# (
|
5
|
-
# exists. Creates the file if it does not exist, including
|
6
|
-
# necessary parent directories. Returns the Enumerable.
|
3
|
+
# Writes each object in the Enumerable as a string plus +eol+
|
4
|
+
# (end-of-line) characters to the specified +file+, overwriting the
|
5
|
+
# file if it exists. Creates the file if it does not exist, including
|
6
|
+
# any necessary parent directories. Returns the Enumerable.
|
7
7
|
#
|
8
8
|
# @see Pathname#write_lines
|
9
9
|
#
|
@@ -19,10 +19,10 @@ module Enumerable
|
|
19
19
|
self
|
20
20
|
end
|
21
21
|
|
22
|
-
# Appends each object in the Enumerable as a string plus
|
23
|
-
# (
|
24
|
-
# does not exist, including any necessary parent directories.
|
25
|
-
# the Enumerable.
|
22
|
+
# Appends each object in the Enumerable as a string plus +eol+
|
23
|
+
# (end-of-line) characters to the specified +file+. Creates the file
|
24
|
+
# if it does not exist, including any necessary parent directories.
|
25
|
+
# Returns the Enumerable.
|
26
26
|
#
|
27
27
|
# @see Pathname#append_lines
|
28
28
|
#
|
data/lib/pleasant_path/file.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
class File
|
4
4
|
|
5
|
-
# Returns the longest path that all
|
5
|
+
# Returns the longest path that all given +paths+ have in common.
|
6
6
|
#
|
7
7
|
# @example
|
8
8
|
# File.common_path(["a/b/x", "a/b/y", "a/b/z"]) # == "a/b/"
|
@@ -23,10 +23,10 @@ class File
|
|
23
23
|
short[0, i == short.length ? i : (last + 1)]
|
24
24
|
end
|
25
25
|
|
26
|
-
# Reads the
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
26
|
+
# Reads the file indicated by +filename+, and yields the entire
|
27
|
+
# contents as a String to the given block for editing. Writes the
|
28
|
+
# return value of the block back to the file, overwriting previous
|
29
|
+
# contents. Returns the return value of the block.
|
30
30
|
#
|
31
31
|
# @example Update JSON data file
|
32
32
|
# File.read("data.json") # == '{"nested":{"key":"value"}}'
|
@@ -54,11 +54,11 @@ class File
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
# Reads the
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
57
|
+
# Reads the file indicated by +filename+, and yields the entire
|
58
|
+
# contents as an Array of lines to the given block for editing.
|
59
|
+
# Writes the return value of the block back to the file, overwriting
|
60
|
+
# previous contents. +eol+ (end-of-line) characters are stripped from
|
61
|
+
# each line when reading, and appended to each line when writing.
|
62
62
|
# Returns the return value of the block.
|
63
63
|
#
|
64
64
|
# @example Dedup lines of file
|
data/lib/pleasant_path/io.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
class IO
|
4
4
|
|
5
|
-
# Writes each object in +lines+ as a string plus end-of-line
|
5
|
+
# Writes each object in +lines+ as a string plus +eol+ (end-of-line)
|
6
6
|
# characters to the IO. Returns +lines+, unmodified.
|
7
7
|
#
|
8
8
|
# @example
|
@@ -14,7 +14,7 @@ class IO
|
|
14
14
|
#
|
15
15
|
# @param lines [Enumerable<#to_s>]
|
16
16
|
# @param eol [String]
|
17
|
-
# @return [
|
17
|
+
# @return [lines]
|
18
18
|
def write_lines(lines, eol: $/)
|
19
19
|
lines.each do |line|
|
20
20
|
self.write(line)
|
@@ -24,11 +24,11 @@ class IO
|
|
24
24
|
lines
|
25
25
|
end
|
26
26
|
|
27
|
-
# Reads all lines from the IO, and returns them with
|
28
|
-
# (
|
27
|
+
# Reads all lines from the IO, and returns them with +eol+
|
28
|
+
# (end-of-line) characters stripped.
|
29
29
|
#
|
30
30
|
# @note Not to be confused with +IO#readlines+, which retains
|
31
|
-
# end-of-line
|
31
|
+
# end-of-line characters.
|
32
32
|
#
|
33
33
|
# @example
|
34
34
|
# File.read("in.txt") # == "one\ntwo\n"
|
@@ -6,17 +6,17 @@ class Object
|
|
6
6
|
# Object, unmodified.
|
7
7
|
#
|
8
8
|
# For information about +options+ see
|
9
|
-
# {https://docs.ruby-lang.org/en/
|
9
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#method-i-generate
|
10
10
|
# +JSON.generate+}. By default, this method uses
|
11
|
-
# {https://docs.ruby-lang.org/en/
|
11
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#attribute-c-dump_default_options
|
12
12
|
# +JSON.dump_default_options+}.
|
13
13
|
#
|
14
14
|
# @example
|
15
|
-
# { "key" => "value" }.write_to_json("
|
16
|
-
# File.read("
|
15
|
+
# { "key" => "value" }.write_to_json("file.json") # == { "key" => "value" }
|
16
|
+
# File.read("file.json") # == '{"key":"value"}'
|
17
17
|
#
|
18
18
|
# @param file [String, Pathname]
|
19
|
-
# @param options [Hash
|
19
|
+
# @param options [Hash{Symbol => Object}]
|
20
20
|
# @return [self]
|
21
21
|
def write_to_json(file, options = {})
|
22
22
|
options = JSON.dump_default_options.merge(options)
|
@@ -2,36 +2,37 @@
|
|
2
2
|
|
3
3
|
class Pathname
|
4
4
|
|
5
|
-
#
|
6
|
-
# The returned result will composed of only basic data
|
7
|
-
# +nil+, +true+, +false+, +Numeric+, +String+, +Array+,
|
5
|
+
# Reads the file indicated by the Pathname, and parses the contents as
|
6
|
+
# JSON. The returned result will be composed of only basic data
|
7
|
+
# types, e.g. +nil+, +true+, +false+, +Numeric+, +String+, +Array+,
|
8
|
+
# and +Hash+.
|
8
9
|
#
|
9
10
|
# For information about +options+, see
|
10
|
-
# {https://docs.ruby-lang.org/en/
|
11
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#method-i-parse
|
11
12
|
# +JSON.parse+}. By default, this method uses
|
12
|
-
# {https://docs.ruby-lang.org/en/
|
13
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#attribute-c-load_default_options
|
13
14
|
# +JSON.load_default_options+} plus +create_additions: false+.
|
14
15
|
#
|
15
16
|
# @example
|
16
|
-
# File.write("
|
17
|
+
# File.write("file.json", '{"key": "value"}')
|
17
18
|
#
|
18
|
-
# Pathname.new("
|
19
|
+
# Pathname.new("file.json").read_json # == { "key" => "value" }
|
19
20
|
#
|
20
|
-
# @param options [Hash
|
21
|
+
# @param options [Hash{Symbol => Object}]
|
21
22
|
# @return [nil, true, false, Numeric, String, Symbol, Array, Hash]
|
22
23
|
def read_json(options = {})
|
23
24
|
JSON.load(self, nil, { create_additions: false, **options })
|
24
25
|
end
|
25
26
|
|
26
|
-
#
|
27
|
-
# deserializing arbitrary data types via type information
|
28
|
-
# the JSON. This is *UNSAFE* for JSON from an untrusted
|
29
|
-
# consume untrusted JSON, use {
|
27
|
+
# Reads the file indicated by the Pathname, and parses the contents as
|
28
|
+
# JSON, deserializing arbitrary data types via type information
|
29
|
+
# embedded in the JSON. This is *UNSAFE* for JSON from an untrusted
|
30
|
+
# source. To consume untrusted JSON, use {read_json} instead.
|
30
31
|
#
|
31
32
|
# For information about +options+, see
|
32
|
-
# {https://docs.ruby-lang.org/en/
|
33
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#method-i-parse
|
33
34
|
# +JSON.parse+}. By default, this method uses
|
34
|
-
# {https://docs.ruby-lang.org/en/
|
35
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#attribute-c-load_default_options
|
35
36
|
# +JSON.load_default_options+}.
|
36
37
|
#
|
37
38
|
# For information about serializing custom types to JSON, see the
|
@@ -42,11 +43,11 @@ class Pathname
|
|
42
43
|
# require "json/add/core" # provides Struct#to_json
|
43
44
|
# Point = Struct.new(:x, :y)
|
44
45
|
# point = Point.new(10, 20)
|
45
|
-
# File.write("
|
46
|
+
# File.write("file.json", point.to_json)
|
46
47
|
#
|
47
|
-
# Pathname.new("
|
48
|
+
# Pathname.new("file.json").load_json # == Point.new(10, 20)
|
48
49
|
#
|
49
|
-
# @param options [Hash
|
50
|
+
# @param options [Hash{Symbol => Object}]
|
50
51
|
# @return [Object]
|
51
52
|
def load_json(options = {})
|
52
53
|
JSON.load(self, nil, options)
|
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
class Pathname
|
4
4
|
|
5
|
-
# {https://docs.ruby-lang.org/en/
|
6
|
-
# as a Pathname. On POSIX systems, this should be
|
7
|
-
# +Pathname.new("/dev/null")+.
|
5
|
+
# {https://docs.ruby-lang.org/en/master/File/File/Constants.html#NULL
|
6
|
+
# +File::NULL+} as a Pathname. On POSIX systems, this should be
|
7
|
+
# equivalent to +Pathname.new("/dev/null")+.
|
8
8
|
NULL = Pathname.new(File::NULL)
|
9
9
|
|
10
10
|
# Returns the Pathname unmodified. Exists for parity with
|
@@ -15,7 +15,7 @@ class Pathname
|
|
15
15
|
self
|
16
16
|
end
|
17
17
|
|
18
|
-
# Joins the Pathname +dirname+ with the given +sibling+.
|
18
|
+
# Joins the Pathname's +dirname+ with the given +sibling+.
|
19
19
|
#
|
20
20
|
# The mnemonic for this operator is that the result is formed by going
|
21
21
|
# up one directory level from the original path, then going back down
|
@@ -30,18 +30,17 @@ class Pathname
|
|
30
30
|
self.dirname / sibling
|
31
31
|
end
|
32
32
|
|
33
|
-
# Returns the +basename+ of the
|
33
|
+
# Returns the +basename+ of the Pathname's +dirname+.
|
34
34
|
#
|
35
35
|
# @example
|
36
|
-
# Pathname.new("
|
36
|
+
# Pathname.new("grand/parent/base").parentname # == Pathname.new("parent")
|
37
37
|
#
|
38
38
|
# @return [Pathname]
|
39
39
|
def parentname
|
40
40
|
self.dirname.basename
|
41
41
|
end
|
42
42
|
|
43
|
-
# Returns the Pathname if +exist?+
|
44
|
-
# nil.
|
43
|
+
# Returns the Pathname if +exist?+ is true, otherwise returns nil.
|
45
44
|
#
|
46
45
|
# @example
|
47
46
|
# FileUtils.mkdir("dir1")
|
@@ -84,15 +83,8 @@ class Pathname
|
|
84
83
|
# @return [Boolean]
|
85
84
|
alias :dir? :directory?
|
86
85
|
|
87
|
-
#
|
88
|
-
#
|
89
|
-
# Alias of +Pathname#empty?+.
|
90
|
-
#
|
91
|
-
# @return [Boolean]
|
92
|
-
alias :dir_empty? :empty?
|
93
|
-
|
94
|
-
# Returns the immediate child directories of the directory indicated
|
95
|
-
# by the Pathname. Returned Pathnames are prefixed by the original
|
86
|
+
# Returns the direct child directories of the directory indicated by
|
87
|
+
# the Pathname. Returned Pathnames are prefixed by the original
|
96
88
|
# Pathname.
|
97
89
|
#
|
98
90
|
# @example
|
@@ -142,18 +134,18 @@ class Pathname
|
|
142
134
|
# directory indicated by the Pathname. Iterated Pathnames are
|
143
135
|
# prefixed by the original Pathname, and are in depth-first order.
|
144
136
|
#
|
145
|
-
# If
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
# @see https://docs.ruby-lang.org/en/trunk/Pathname.html#method-i-find Pathname#find
|
137
|
+
# If a block is given, each descendent Pathname is yielded, and this
|
138
|
+
# method returns the original Pathname. Otherwise, an Enumerator is
|
139
|
+
# returned.
|
150
140
|
#
|
151
|
-
# @
|
152
|
-
# @return [Enumerator<Pathname>]
|
141
|
+
# @see https://docs.ruby-lang.org/en/master/Pathname.html#method-i-find Pathname#find
|
153
142
|
#
|
154
143
|
# @overload find_dirs(&block)
|
155
144
|
# @yieldparam descendent [Pathname]
|
156
|
-
# @return [
|
145
|
+
# @return [self]
|
146
|
+
#
|
147
|
+
# @overload find_dirs()
|
148
|
+
# @return [Enumerator<Pathname>]
|
157
149
|
def find_dirs
|
158
150
|
return to_enum(__method__) unless block_given?
|
159
151
|
|
@@ -168,7 +160,7 @@ class Pathname
|
|
168
160
|
self
|
169
161
|
end
|
170
162
|
|
171
|
-
# Returns the
|
163
|
+
# Returns the direct child files of the directory indicated by the
|
172
164
|
# Pathname. Returned Pathnames are prefixed by the original Pathname.
|
173
165
|
#
|
174
166
|
# @example
|
@@ -218,18 +210,18 @@ class Pathname
|
|
218
210
|
# indicated by the Pathname. Iterated Pathnames are prefixed by the
|
219
211
|
# original Pathname, and are in depth-first order.
|
220
212
|
#
|
221
|
-
# If
|
222
|
-
#
|
223
|
-
#
|
213
|
+
# If a block is given, each descendent Pathname is yielded, and this
|
214
|
+
# method returns the original Pathname. Otherwise, an Enumerator is
|
215
|
+
# returned.
|
224
216
|
#
|
225
|
-
# @see https://docs.ruby-lang.org/en/
|
226
|
-
#
|
227
|
-
# @overload find_files()
|
228
|
-
# @return [Enumerator<Pathname>]
|
217
|
+
# @see https://docs.ruby-lang.org/en/master/Pathname.html#method-i-find Pathname#find
|
229
218
|
#
|
230
219
|
# @overload find_files(&block)
|
231
220
|
# @yieldparam descendent [Pathname]
|
232
|
-
# @return [
|
221
|
+
# @return [self]
|
222
|
+
#
|
223
|
+
# @overload find_files()
|
224
|
+
# @return [Enumerator<Pathname>]
|
233
225
|
def find_files
|
234
226
|
return to_enum(__method__) unless block_given?
|
235
227
|
|
@@ -243,10 +235,10 @@ class Pathname
|
|
243
235
|
# Changes the current working directory to the Pathname. If no block
|
244
236
|
# is given, this method returns the Pathname. Otherwise, the block is
|
245
237
|
# called with the Pathname, the original working directory is restored
|
246
|
-
# after the block exits, this method returns the return value of
|
247
|
-
# block.
|
238
|
+
# after the block exits, and this method returns the return value of
|
239
|
+
# the block.
|
248
240
|
#
|
249
|
-
# @see https://docs.ruby-lang.org/en/
|
241
|
+
# @see https://docs.ruby-lang.org/en/master/Dir.html#method-c-chdir Dir.chdir
|
250
242
|
#
|
251
243
|
# @example
|
252
244
|
# FileUtils.mkdir("dir1")
|
@@ -259,12 +251,12 @@ class Pathname
|
|
259
251
|
# Pathname.pwd # == Pathname.new("dir1")
|
260
252
|
#
|
261
253
|
# @overload chdir()
|
262
|
-
# @return [
|
254
|
+
# @return [self]
|
263
255
|
#
|
264
256
|
# @overload chdir(&block)
|
265
257
|
# @yieldparam working_dir [Pathname]
|
266
|
-
# @yieldreturn [Object]
|
267
|
-
# @return [
|
258
|
+
# @yieldreturn [Object]
|
259
|
+
# @return [Object]
|
268
260
|
#
|
269
261
|
# @raise [SystemCallError]
|
270
262
|
# if the Pathname does not point to an existing directory
|
@@ -282,7 +274,7 @@ class Pathname
|
|
282
274
|
# Creates the directory indicated by the Pathname, including any
|
283
275
|
# necessary parent directories. Returns the Pathname.
|
284
276
|
#
|
285
|
-
# @see https://docs.ruby-lang.org/en/
|
277
|
+
# @see https://docs.ruby-lang.org/en/master/Pathname.html#method-i-mkpath Pathname#mkpath
|
286
278
|
#
|
287
279
|
# @example
|
288
280
|
# Dir.exist?("path") # == false
|
@@ -301,8 +293,8 @@ class Pathname
|
|
301
293
|
self
|
302
294
|
end
|
303
295
|
|
304
|
-
# Creates the directory indicated by the Pathname +dirname+,
|
305
|
-
# any necessary parent directories. Returns the Pathname.
|
296
|
+
# Creates the directory indicated by the Pathname's +dirname+,
|
297
|
+
# including any necessary parent directories. Returns the Pathname.
|
306
298
|
#
|
307
299
|
# @example
|
308
300
|
# Dir.exist?("path") # == false
|
@@ -336,40 +328,15 @@ class Pathname
|
|
336
328
|
# Dir.exist?("path/to") # == true
|
337
329
|
# File.exist?("path/to/file") # == true
|
338
330
|
#
|
339
|
-
# @return [
|
331
|
+
# @return [self]
|
340
332
|
# @raise [SystemCallError]
|
341
|
-
# if the Pathname points to an
|
333
|
+
# if the Pathname points to an existing directory, or if any element
|
334
|
+
# of the +dirname+ points to an existing file (non-directory)
|
342
335
|
def make_file
|
343
336
|
self.make_dirname.open("a"){}
|
344
337
|
self
|
345
338
|
end
|
346
339
|
|
347
|
-
# @deprecated Use {Pathname#make_file}.
|
348
|
-
#
|
349
|
-
# Creates the file indicated by the Pathname, including any necessary
|
350
|
-
# parent directories. If the file already exists, its modification
|
351
|
-
# time (mtime) and access time (atime) are updated. Returns the
|
352
|
-
# Pathname.
|
353
|
-
#
|
354
|
-
# @see https://docs.ruby-lang.org/en/trunk/FileUtils.html#method-c-touch FileUtils.touch
|
355
|
-
#
|
356
|
-
# @example
|
357
|
-
# Dir.exist?("path") # == false
|
358
|
-
# Dir.exist?("path/to") # == false
|
359
|
-
#
|
360
|
-
# Pathname.new("path/to/file").touch_file # == Pathname.new("path/to/file")
|
361
|
-
#
|
362
|
-
# Dir.exist?("path") # == true
|
363
|
-
# Dir.exist?("path/to") # == true
|
364
|
-
# File.exist?("path/to/file") # == true
|
365
|
-
#
|
366
|
-
# @return [self]
|
367
|
-
def touch_file
|
368
|
-
self.make_dirname
|
369
|
-
FileUtils.touch(self)
|
370
|
-
self
|
371
|
-
end
|
372
|
-
|
373
340
|
# Recursively deletes the directory or file indicated by the Pathname.
|
374
341
|
# Similar to +Pathname#rmtree+, but does not raise an exception if the
|
375
342
|
# file does not exist. Returns the Pathname.
|
@@ -450,42 +417,36 @@ class Pathname
|
|
450
417
|
end
|
451
418
|
|
452
419
|
# Moves the file or directory indicated by the Pathname to
|
453
|
-
# +destination+, in the same manner as +FileUtils.mv+.
|
454
|
-
# necessary parent directories of the destination. Returns
|
420
|
+
# +destination+, in the same manner as +FileUtils.mv+. Returns
|
455
421
|
# +destination+ as a Pathname.
|
456
422
|
#
|
457
|
-
# @see https://docs.ruby-lang.org/en/
|
423
|
+
# @see https://docs.ruby-lang.org/en/master/FileUtils.html#method-c-mv FileUtils.mv
|
458
424
|
#
|
459
425
|
# @example
|
460
|
-
#
|
461
|
-
#
|
462
|
-
#
|
463
|
-
# File.exist?("other/dir/same_file") # == false
|
426
|
+
# FileUtils.mkpath("dir/files")
|
427
|
+
# FileUtils.touch("dir/files/file1")
|
428
|
+
# FileUtils.mkpath("other_dir")
|
464
429
|
#
|
465
|
-
# Pathname.new("
|
466
|
-
# # == Pathname.new("
|
430
|
+
# Pathname.new("dir/files").move("other_dir/same_files")
|
431
|
+
# # == Pathname.new("other_dir/same_files")
|
467
432
|
#
|
468
|
-
#
|
469
|
-
#
|
470
|
-
# Dir.exist?("other/dir") # == true
|
471
|
-
# File.exist?("other/dir/same_file") # == true
|
433
|
+
# Dir.exist?("dir/files") # == false
|
434
|
+
# File.exist?("other_dir/same_files/file1") # == true
|
472
435
|
#
|
473
436
|
# @param destination [Pathname, String]
|
474
437
|
# @return [Pathname]
|
475
438
|
def move(destination)
|
476
|
-
destination = destination.to_pathname
|
477
|
-
destination.make_dirname
|
478
439
|
FileUtils.mv(self, destination)
|
479
|
-
destination
|
440
|
+
destination.to_pathname
|
480
441
|
end
|
481
442
|
|
482
|
-
# Moves the file or directory indicated by the Pathname to
|
483
|
-
# destination
|
443
|
+
# Moves the file or directory indicated by the Pathname to
|
444
|
+
# +destination+, replacing any existing file or directory.
|
484
445
|
#
|
485
446
|
# If a block is given and a file or directory does exist at the
|
486
447
|
# destination, the block is called with the source and destination
|
487
448
|
# Pathnames, and the return value of the block is used as the new
|
488
|
-
# destination. If the block returns the source Pathname or
|
449
|
+
# destination. If the block returns the source Pathname or nil, the
|
489
450
|
# move is aborted.
|
490
451
|
#
|
491
452
|
# Creates any necessary parent directories of the destination.
|
@@ -574,7 +535,7 @@ class Pathname
|
|
574
535
|
File.rename(self, destination)
|
575
536
|
else
|
576
537
|
destination.delete!
|
577
|
-
self.move(destination)
|
538
|
+
self.move(destination.make_dirname)
|
578
539
|
end
|
579
540
|
end
|
580
541
|
|
@@ -589,7 +550,7 @@ class Pathname
|
|
589
550
|
# resultant destination, the block is called with the source and
|
590
551
|
# destination Pathnames, and the return value of the block is used as
|
591
552
|
# the new destination. If the block returns the source Pathname or
|
592
|
-
#
|
553
|
+
# nil, the move is aborted.
|
593
554
|
#
|
594
555
|
# Creates any necessary parent directories of the destination.
|
595
556
|
# Returns the destination as a Pathname (or the source Pathname in the
|
@@ -643,42 +604,36 @@ class Pathname
|
|
643
604
|
end
|
644
605
|
|
645
606
|
# Copies the file or directory indicated by the Pathname to
|
646
|
-
# +destination+, in the same manner as +FileUtils.cp_r+.
|
647
|
-
# necessary parent directories of the destination. Returns
|
607
|
+
# +destination+, in the same manner as +FileUtils.cp_r+. Returns
|
648
608
|
# +destination+ as a Pathname.
|
649
609
|
#
|
650
|
-
# @see https://docs.ruby-lang.org/en/
|
610
|
+
# @see https://docs.ruby-lang.org/en/master/FileUtils.html#method-c-cp_r FileUtils.cp_r
|
651
611
|
#
|
652
612
|
# @example
|
653
|
-
#
|
654
|
-
#
|
655
|
-
#
|
656
|
-
# File.exist?("other/dir/same_file") # == false
|
613
|
+
# FileUtils.mkpath("dir/files")
|
614
|
+
# FileUtils.touch("dir/files/file1")
|
615
|
+
# FileUtils.mkpath("other_dir")
|
657
616
|
#
|
658
|
-
# Pathname.new("
|
659
|
-
# # == Pathname.new("
|
617
|
+
# Pathname.new("dir/files").copy("other_dir/same_files")
|
618
|
+
# # == Pathname.new("other_dir/same_files")
|
660
619
|
#
|
661
|
-
# File.exist?("
|
662
|
-
#
|
663
|
-
# Dir.exist?("other/dir") # == true
|
664
|
-
# File.exist?("other/dir/same_file") # == true
|
620
|
+
# File.exist?("dir/files/file1") # == true
|
621
|
+
# File.exist?("other_dir/same_files/file1") # == true
|
665
622
|
#
|
666
623
|
# @param destination [Pathname, String]
|
667
624
|
# @return [Pathname]
|
668
625
|
def copy(destination)
|
669
|
-
destination = destination.to_pathname
|
670
|
-
destination.make_dirname
|
671
626
|
FileUtils.cp_r(self, destination)
|
672
|
-
destination
|
627
|
+
destination.to_pathname
|
673
628
|
end
|
674
629
|
|
675
|
-
# Copies the file or directory indicated by the Pathname to
|
676
|
-
# destination
|
630
|
+
# Copies the file or directory indicated by the Pathname to
|
631
|
+
# +destination+, replacing any existing file or directory.
|
677
632
|
#
|
678
633
|
# If a block is given and a file or directory does exist at the
|
679
634
|
# destination, the block is called with the source and destination
|
680
635
|
# Pathnames, and the return value of the block is used as the new
|
681
|
-
# destination. If the block returns the source Pathname or
|
636
|
+
# destination. If the block returns the source Pathname or nil, the
|
682
637
|
# copy is aborted.
|
683
638
|
#
|
684
639
|
# Creates any necessary parent directories of the destination.
|
@@ -760,7 +715,7 @@ class Pathname
|
|
760
715
|
|
761
716
|
if destination
|
762
717
|
destination.delete! unless File.identical?(self, destination)
|
763
|
-
self.copy(destination)
|
718
|
+
self.copy(destination.make_dirname)
|
764
719
|
end
|
765
720
|
|
766
721
|
destination || self
|
@@ -775,7 +730,7 @@ class Pathname
|
|
775
730
|
# resultant destination, the block is called with the source and
|
776
731
|
# destination Pathnames, and the return value of the block is used as
|
777
732
|
# the new destination. If the block returns the source Pathname or
|
778
|
-
#
|
733
|
+
# nil, the copy is aborted.
|
779
734
|
#
|
780
735
|
# Creates any necessary parent directories of the destination.
|
781
736
|
# Returns the destination as a Pathname (or the source Pathname in the
|
@@ -841,7 +796,7 @@ class Pathname
|
|
841
796
|
# resultant destination, the block is called with the source and
|
842
797
|
# destination Pathnames, and the return value of the block is used as
|
843
798
|
# the new destination. If the block returns the source Pathname or
|
844
|
-
#
|
799
|
+
# nil, the rename is aborted.
|
845
800
|
#
|
846
801
|
# Returns the destination as a Pathname (or the source Pathname in the
|
847
802
|
# case that the rename is aborted).
|
@@ -900,7 +855,7 @@ class Pathname
|
|
900
855
|
# resultant destination, the block is called with the source and
|
901
856
|
# destination Pathnames, and the return value of the block is used as
|
902
857
|
# the new destination. If the block returns the source Pathname or
|
903
|
-
#
|
858
|
+
# nil, the rename is aborted.
|
904
859
|
#
|
905
860
|
# Returns the destination as a Pathname (or the source Pathname in the
|
906
861
|
# case that the rename is aborted).
|
@@ -968,9 +923,6 @@ class Pathname
|
|
968
923
|
# @yieldreturn [Pathname, nil]
|
969
924
|
# @return [Pathname]
|
970
925
|
def rename_extname(new_extname, &block)
|
971
|
-
unless new_extname.start_with?(".") || new_extname.empty?
|
972
|
-
new_extname = ".#{new_extname}"
|
973
|
-
end
|
974
926
|
self.move_as(self.sub_ext(new_extname), &block)
|
975
927
|
end
|
976
928
|
|
@@ -1016,7 +968,7 @@ class Pathname
|
|
1016
968
|
self
|
1017
969
|
end
|
1018
970
|
|
1019
|
-
# Writes each object in +lines+ as a string plus end-of-line
|
971
|
+
# Writes each object in +lines+ as a string plus +eol+ (end-of-line)
|
1020
972
|
# characters to the file indicated by the Pathname, overwriting the
|
1021
973
|
# file if it exists. Creates the file if it does not exist, including
|
1022
974
|
# any necessary parent directories. Returns the Pathname.
|
@@ -1037,7 +989,7 @@ class Pathname
|
|
1037
989
|
self
|
1038
990
|
end
|
1039
991
|
|
1040
|
-
# Appends each object in +lines+ as a string plus end-of-line
|
992
|
+
# Appends each object in +lines+ as a string plus +eol+ (end-of-line)
|
1041
993
|
# characters to the file indicated by the Pathname. Creates the file
|
1042
994
|
# if it does not exist, including any necessary parent directories.
|
1043
995
|
# Returns the Pathname.
|
@@ -1064,12 +1016,12 @@ class Pathname
|
|
1064
1016
|
alias :read_text :read
|
1065
1017
|
|
1066
1018
|
# Reads all lines from the file indicated by the Pathname, and returns
|
1067
|
-
# them with
|
1019
|
+
# them with +eol+ (end-of-line) characters stripped.
|
1068
1020
|
#
|
1069
1021
|
# @see IO#read_lines
|
1070
1022
|
#
|
1071
1023
|
# @note Not to be confused with +Pathname#readlines+, which retains
|
1072
|
-
# end-of-line
|
1024
|
+
# end-of-line characters.
|
1073
1025
|
#
|
1074
1026
|
# @example
|
1075
1027
|
# File.read("path/to/file") # == "one\ntwo\n"
|
@@ -1082,10 +1034,10 @@ class Pathname
|
|
1082
1034
|
self.open("r"){|f| f.read_lines(eol: eol) }
|
1083
1035
|
end
|
1084
1036
|
|
1085
|
-
# Reads the
|
1086
|
-
#
|
1087
|
-
#
|
1088
|
-
#
|
1037
|
+
# Reads the file indicated by the Pathname, and yields the entire
|
1038
|
+
# contents as a String to the given block for editing. Writes the
|
1039
|
+
# return value of the block back to the file, overwriting previous
|
1040
|
+
# contents. Returns the Pathname.
|
1089
1041
|
#
|
1090
1042
|
# @see File.edit_text
|
1091
1043
|
#
|
@@ -1103,17 +1055,18 @@ class Pathname
|
|
1103
1055
|
# @yield [text]
|
1104
1056
|
# @yieldparam text [String]
|
1105
1057
|
# @yieldreturn [String]
|
1106
|
-
# @return [
|
1058
|
+
# @return [self]
|
1107
1059
|
def edit_text(&block)
|
1108
1060
|
File.edit_text(self, &block)
|
1061
|
+
self
|
1109
1062
|
end
|
1110
1063
|
|
1111
|
-
# Reads the
|
1112
|
-
# an
|
1113
|
-
#
|
1114
|
-
#
|
1115
|
-
#
|
1116
|
-
# Returns the
|
1064
|
+
# Reads the file indicated by the Pathname, and yields the entire
|
1065
|
+
# contents as an Array of lines to the given block for editing.
|
1066
|
+
# Writes the return value of the block back to the file, overwriting
|
1067
|
+
# previous contents. +eol+ (end-of-line) characters are stripped from
|
1068
|
+
# each line when reading, and appended to each line when writing.
|
1069
|
+
# Returns the Pathname.
|
1117
1070
|
#
|
1118
1071
|
# @see File.edit_lines
|
1119
1072
|
#
|
@@ -1129,9 +1082,10 @@ class Pathname
|
|
1129
1082
|
# @yield [lines]
|
1130
1083
|
# @yieldparam lines [Array<String>]
|
1131
1084
|
# @yieldreturn [Array<String>]
|
1132
|
-
# @return [
|
1085
|
+
# @return [self]
|
1133
1086
|
def edit_lines(eol: $/, &block)
|
1134
1087
|
File.edit_lines(self, eol: eol, &block)
|
1088
|
+
self
|
1135
1089
|
end
|
1136
1090
|
|
1137
1091
|
# Appends the contents of file indicated by +source+ to the file
|
data/lib/pleasant_path/string.rb
CHANGED
@@ -27,41 +27,6 @@ class String
|
|
27
27
|
self.path / child
|
28
28
|
end
|
29
29
|
|
30
|
-
# @deprecated Use {Pathname#^}.
|
31
|
-
#
|
32
|
-
# Constructs a Pathname from the String, and appends +sibling+ to the
|
33
|
-
# +dirname+ of the Pathname.
|
34
|
-
#
|
35
|
-
# The mnemonic for this operator is that the result is formed by going
|
36
|
-
# up one directory level from the original path, then going back down
|
37
|
-
# to +sibling+.
|
38
|
-
#
|
39
|
-
# @see Pathname#^
|
40
|
-
#
|
41
|
-
# @example
|
42
|
-
# "path/to/file1" ^ "file2" # == Pathname.new("path/to/file2")
|
43
|
-
#
|
44
|
-
# @param sibling [Pathname, String]
|
45
|
-
# @return [Pathname]
|
46
|
-
def ^(sibling)
|
47
|
-
self.path ^ sibling
|
48
|
-
end
|
49
|
-
|
50
|
-
# @deprecated Use +Pathname.glob+.
|
51
|
-
#
|
52
|
-
# Returns an array of Pathnames which match the filename pattern
|
53
|
-
# contained in the String.
|
54
|
-
#
|
55
|
-
# @see https://docs.ruby-lang.org/en/trunk/Pathname.html#method-i-glob Pathname.glob
|
56
|
-
#
|
57
|
-
# @example
|
58
|
-
# "*.txt".glob # == Pathname.glob("*.txt")
|
59
|
-
#
|
60
|
-
# @return [Array<Pathname>]
|
61
|
-
def glob
|
62
|
-
Pathname.glob(self)
|
63
|
-
end
|
64
|
-
|
65
30
|
# Writes the String to the specified +file+, overwriting the file if
|
66
31
|
# it exists. Creates the file if it does not exist, including
|
67
32
|
# any necessary parent directories. Returns the String.
|
@@ -8,15 +8,15 @@ class Object
|
|
8
8
|
# Object, unmodified.
|
9
9
|
#
|
10
10
|
# For information about +options+ see
|
11
|
-
# {https://docs.ruby-lang.org/en/
|
11
|
+
# {https://docs.ruby-lang.org/en/master/Psych.html#method-c-dump
|
12
12
|
# +YAML.dump+}.
|
13
13
|
#
|
14
14
|
# @example
|
15
|
-
# { "key" => "value" }.write_to_yaml("
|
16
|
-
# File.read("
|
15
|
+
# { "key" => "value" }.write_to_yaml("file.yaml") # == { "key" => "value" }
|
16
|
+
# File.read("file.yaml") # == "---\nkey: value\n"
|
17
17
|
#
|
18
18
|
# @param file [String, Pathname]
|
19
|
-
# @param options [Hash
|
19
|
+
# @param options [Hash{Symbol => Object}]
|
20
20
|
# @return [self]
|
21
21
|
def write_to_yaml(file, options = {})
|
22
22
|
file.to_pathname.make_dirname.open("w") do |f|
|
@@ -2,38 +2,32 @@
|
|
2
2
|
|
3
3
|
class Pathname
|
4
4
|
|
5
|
-
#
|
6
|
-
# The returned result will composed of only basic data
|
7
|
-
# +nil+, +true+, +false+, +Numeric+, +String+, +Array+,
|
5
|
+
# Reads the file indicated by the Pathname, and parses the contents as
|
6
|
+
# YAML. The returned result will be composed of only basic data
|
7
|
+
# types, e.g. +nil+, +true+, +false+, +Numeric+, +String+, +Array+,
|
8
|
+
# and +Hash+.
|
8
9
|
#
|
9
10
|
# @example
|
10
|
-
# File.write("
|
11
|
+
# File.write("file.yaml", "key: value")
|
11
12
|
#
|
12
|
-
# Pathname.new("
|
13
|
+
# Pathname.new("file.yaml").read_yaml # == { "key" => "value" }
|
13
14
|
#
|
14
15
|
# @return [nil, true, false, Numeric, String, Array, Hash]
|
15
16
|
def read_yaml
|
16
|
-
self.open("r")
|
17
|
-
# HACK fix Ruby 2.6 warning, but still support Ruby < 2.6
|
18
|
-
if Gem::Version.new(Psych::VERSION) >= Gem::Version.new("3.1.0.pre1")
|
19
|
-
YAML.safe_load(f, filename: self)
|
20
|
-
else
|
21
|
-
YAML.safe_load(f, [], [], false, self)
|
22
|
-
end
|
23
|
-
end
|
17
|
+
self.open("r"){|f| YAML.safe_load(f, filename: self) }
|
24
18
|
end
|
25
19
|
|
26
|
-
#
|
27
|
-
# deserializing arbitrary data types via type information
|
28
|
-
# the YAML. This is *UNSAFE* for YAML from an untrusted
|
29
|
-
# consume untrusted YAML, use {
|
20
|
+
# Reads the file indicated by the Pathname, and parses the contents as
|
21
|
+
# YAML, deserializing arbitrary data types via type information
|
22
|
+
# embedded in the YAML. This is *UNSAFE* for YAML from an untrusted
|
23
|
+
# source. To consume untrusted YAML, use {read_yaml} instead.
|
30
24
|
#
|
31
25
|
# @example
|
32
26
|
# Point = Struct.new(:x, :y)
|
33
27
|
# point = Point.new(10, 20)
|
34
|
-
# File.write("
|
28
|
+
# File.write("file.yaml", point.to_yaml)
|
35
29
|
#
|
36
|
-
# Pathname.new("
|
30
|
+
# Pathname.new("file.yaml").load_yaml # == Point.new(10, 20)
|
37
31
|
#
|
38
32
|
# @return [Object]
|
39
33
|
def load_yaml
|
data/pleasant_path.gemspec
CHANGED
@@ -1,27 +1,26 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'pleasant_path/version'
|
1
|
+
require_relative "lib/pleasant_path/version"
|
5
2
|
|
6
3
|
Gem::Specification.new do |spec|
|
7
4
|
spec.name = "pleasant_path"
|
8
5
|
spec.version = PleasantPath::VERSION
|
9
6
|
spec.authors = ["Jonathan Hefner"]
|
10
|
-
spec.email = ["jonathan
|
7
|
+
spec.email = ["jonathan@hefner.pro"]
|
11
8
|
|
12
9
|
spec.summary = %q{A fluent API for pleasant file IO.}
|
13
10
|
spec.homepage = "https://github.com/jonathanhefner/pleasant_path"
|
14
11
|
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = ">= 2.6"
|
15
13
|
|
16
|
-
spec.
|
17
|
-
|
14
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
15
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
16
|
+
spec.metadata["changelog_uri"] = spec.metadata["source_code_uri"] + "/blob/master/CHANGELOG.md"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(__dir__) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
22
|
end
|
19
23
|
spec.bindir = "exe"
|
20
24
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
25
|
spec.require_paths = ["lib"]
|
22
|
-
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.13"
|
24
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
-
spec.add_development_dependency "minitest", "~> 5.0"
|
26
|
-
spec.add_development_dependency "yard", "~> 0.9"
|
27
26
|
end
|
metadata
CHANGED
@@ -1,74 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pleasant_path
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Hefner
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.13'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.13'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: minitest
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '5.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '5.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: yard
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0.9'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0.9'
|
11
|
+
date: 2020-02-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
69
13
|
description:
|
70
14
|
email:
|
71
|
-
- jonathan
|
15
|
+
- jonathan@hefner.pro
|
72
16
|
executables: []
|
73
17
|
extensions: []
|
74
18
|
extra_rdoc_files: []
|
@@ -97,7 +41,10 @@ files:
|
|
97
41
|
homepage: https://github.com/jonathanhefner/pleasant_path
|
98
42
|
licenses:
|
99
43
|
- MIT
|
100
|
-
metadata:
|
44
|
+
metadata:
|
45
|
+
homepage_uri: https://github.com/jonathanhefner/pleasant_path
|
46
|
+
source_code_uri: https://github.com/jonathanhefner/pleasant_path
|
47
|
+
changelog_uri: https://github.com/jonathanhefner/pleasant_path/blob/master/CHANGELOG.md
|
101
48
|
post_install_message:
|
102
49
|
rdoc_options: []
|
103
50
|
require_paths:
|
@@ -106,14 +53,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
53
|
requirements:
|
107
54
|
- - ">="
|
108
55
|
- !ruby/object:Gem::Version
|
109
|
-
version: '
|
56
|
+
version: '2.6'
|
110
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
58
|
requirements:
|
112
59
|
- - ">="
|
113
60
|
- !ruby/object:Gem::Version
|
114
61
|
version: '0'
|
115
62
|
requirements: []
|
116
|
-
rubygems_version: 3.
|
63
|
+
rubygems_version: 3.1.2
|
117
64
|
signing_key:
|
118
65
|
specification_version: 4
|
119
66
|
summary: A fluent API for pleasant file IO.
|