pleasant_path 1.3.0 → 3.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/CHANGELOG.md +33 -7
- data/Gemfile +5 -1
- data/README.md +74 -85
- 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 +8 -9
- data/lib/pleasant_path/json/pathname.rb +19 -18
- 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 +14 -20
- data/pleasant_path.gemspec +12 -12
- metadata +15 -61
- data/.gitignore +0 -9
- data/.travis.yml +0 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5e772187275bf4dea8ba22a68a973e127586a5b9c212cd47bfcf38269c8c5f66
|
|
4
|
+
data.tar.gz: 59e4a89da059811da874ed435c8b060e4fab05ce22765a7bf3c3a122063e7edd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0f23bc5959b08ea5bd2a803ccf42379ce9229d6ab6624c8b2c2f7d287d18861e9b868c5e232fd703fc8a759381a4a1b69851efc2341293ec90fee7c61ab2818a
|
|
7
|
+
data.tar.gz: 6a98f6d9ba1f9b2cba59623b3124deb7b978eb63e0fe9e37b45930541feae2cba04113e1cae4cfcbc102dbecc5212736be218de396ddf4f79805b3c61971f8f6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,25 @@
|
|
|
1
|
+
## 3.0.0
|
|
2
|
+
|
|
3
|
+
* [BREAKING] Drop support for Ruby < 3.4
|
|
4
|
+
* [BREAKING] Require `json` >= 2.11
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
## 2.0.0
|
|
8
|
+
|
|
9
|
+
* [BREAKING] Drop support for Ruby < 2.6
|
|
10
|
+
* [BREAKING] Remove `Pathname#dir_empty?`
|
|
11
|
+
* Use `Pathname#empty?` instead
|
|
12
|
+
* [BREAKING] Remove `Pathname#touch_file`
|
|
13
|
+
* Use `Pathname#make_file` instead
|
|
14
|
+
* [BREAKING] Remove `String#^`
|
|
15
|
+
* [BREAKING] Remove `String#glob`
|
|
16
|
+
* [BREAKING] Cease creating parent directories in `Pathname#move`
|
|
17
|
+
* [BREAKING] Cease creating parent directories in `Pathname#copy`
|
|
18
|
+
* [BREAKING] Cease prepending dot in `Pathname#rename_extname`
|
|
19
|
+
* [BREAKING] Change `Pathname#edit_text` to return the Pathname
|
|
20
|
+
* [BREAKING] Change `Pathname#edit_lines` to return the Pathname
|
|
21
|
+
|
|
22
|
+
|
|
1
23
|
## 1.3.0
|
|
2
24
|
|
|
3
25
|
* Add `Pathname#find_dirs`
|
|
@@ -6,8 +28,8 @@
|
|
|
6
28
|
* Add `Pathname#available_name`
|
|
7
29
|
* Add `Pathname#move_as`
|
|
8
30
|
* Add `Pathname#copy_as`
|
|
9
|
-
* Add `eol` parameter to line-oriented methods (
|
|
10
|
-
`Pathname#
|
|
31
|
+
* Add `eol` parameter to line-oriented methods (e.g.
|
|
32
|
+
`Pathname#read_lines`, `Pathname#write_lines`, etc)
|
|
11
33
|
* Move Array methods to Enumerable
|
|
12
34
|
* Use `JSON.dump_default_options` and `JSON.load_default_options` in
|
|
13
35
|
JSON-related API
|
|
@@ -17,16 +39,20 @@
|
|
|
17
39
|
|
|
18
40
|
* Add `Pathname#existence`
|
|
19
41
|
* Add `Pathname#chdir`
|
|
20
|
-
* Fix `Object#write_to_yaml` to
|
|
42
|
+
* Fix `Object#write_to_yaml` to create parent directories as necessary
|
|
21
43
|
|
|
22
44
|
|
|
23
45
|
## 1.1.0
|
|
24
46
|
|
|
25
|
-
* Add `Pathname#copy`
|
|
26
|
-
* Add `Pathname#
|
|
27
|
-
* Add `
|
|
47
|
+
* Add `Pathname#copy`
|
|
48
|
+
* Add `Pathname#copy_into`
|
|
49
|
+
* Add `Pathname#rename_basename`
|
|
50
|
+
* Add `Pathname#rename_extname`
|
|
51
|
+
* Add `File.common_path`
|
|
52
|
+
* Add `Pathname#common_path`
|
|
28
53
|
* Add `Pathname::NULL`
|
|
29
|
-
* Add JSON-related
|
|
54
|
+
* Add JSON-related API
|
|
55
|
+
* Add YAML-related API
|
|
30
56
|
* Fix `File.edit_text` and `File.edit_lines` to properly truncate
|
|
31
57
|
|
|
32
58
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# pleasant_path
|
|
2
2
|
|
|
3
|
-
A [fluent API] for pleasant file IO, written as extensions to core
|
|
4
|
-
objects. See API listing below, or browse the [full documentation].
|
|
3
|
+
A [fluent API][] for pleasant file IO, written as extensions to core
|
|
4
|
+
Ruby 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,110 +22,99 @@ objects. See API listing below, or browse the [full documentation].
|
|
|
22
22
|
|
|
23
23
|
The following methods are available:
|
|
24
24
|
|
|
25
|
-
- [Pathname](
|
|
26
|
-
- [
|
|
27
|
-
- [
|
|
28
|
-
- [
|
|
29
|
-
- [
|
|
30
|
-
- [
|
|
31
|
-
- [
|
|
32
|
-
- [
|
|
33
|
-
- [
|
|
34
|
-
- [
|
|
35
|
-
- [
|
|
36
|
-
- [
|
|
37
|
-
- [
|
|
38
|
-
- [
|
|
39
|
-
- [
|
|
40
|
-
- [
|
|
41
|
-
- [
|
|
42
|
-
- [
|
|
43
|
-
- [
|
|
44
|
-
- [
|
|
45
|
-
- [
|
|
46
|
-
- [
|
|
47
|
-
- [
|
|
48
|
-
- [
|
|
49
|
-
- [
|
|
50
|
-
- [
|
|
51
|
-
- [
|
|
52
|
-
- [
|
|
53
|
-
- [
|
|
54
|
-
- [
|
|
55
|
-
- [
|
|
56
|
-
- [
|
|
57
|
-
- [
|
|
58
|
-
- [
|
|
59
|
-
- [
|
|
60
|
-
- [
|
|
61
|
-
- [String](
|
|
62
|
-
- [
|
|
63
|
-
- [
|
|
64
|
-
- [
|
|
65
|
-
- [
|
|
66
|
-
- [
|
|
67
|
-
- [Enumerable](
|
|
68
|
-
- [
|
|
69
|
-
- [
|
|
70
|
-
- [File](
|
|
71
|
-
- [
|
|
72
|
-
- [
|
|
73
|
-
- [
|
|
74
|
-
- [IO](
|
|
75
|
-
- [
|
|
76
|
-
- [
|
|
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
|
|
80
80
|
|
|
81
|
-
|
|
82
|
-
files, using the [JSON module] and [YAML module] that are part of
|
|
83
|
-
standard library. Because Ruby does not load these modules by
|
|
84
|
-
|
|
85
|
-
default either. To load these
|
|
86
|
-
standard library modules, use:
|
|
81
|
+
`pleasant_path` also includes methods for interacting with JSON and YAML
|
|
82
|
+
files, using the [JSON module][] and [YAML module][] that are part of
|
|
83
|
+
Ruby's standard library. Because Ruby does not load these modules by
|
|
84
|
+
default, `pleasant_path` does not load its JSON-related and YAML-related
|
|
85
|
+
API by default either. To load these `pleasant_path` APIs **and** the
|
|
86
|
+
relevant standard library modules, use:
|
|
87
87
|
|
|
88
88
|
```ruby
|
|
89
89
|
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
|
-
- Object
|
|
99
|
-
- [write_to_json](
|
|
100
|
-
- [write_to_yaml](
|
|
101
|
-
- Pathname
|
|
102
|
-
- [load_json](
|
|
103
|
-
- [load_yaml](
|
|
104
|
-
- [read_json](
|
|
105
|
-
- [read_yaml](
|
|
98
|
+
- `Object`
|
|
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
|
+
- `Pathname`
|
|
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
|
|
111
|
-
|
|
112
|
-
```bash
|
|
113
|
-
$ gem install pleasant_path
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
Then require in your Ruby script:
|
|
117
|
-
|
|
118
|
-
```ruby
|
|
119
|
-
require "pleasant_path"
|
|
120
|
-
```
|
|
110
|
+
Install the [`pleasant_path` gem](https://rubygems.org/gems/pleasant_path).
|
|
121
111
|
|
|
122
112
|
|
|
123
113
|
## Contributing
|
|
124
114
|
|
|
125
|
-
Run `rake test` to run the tests.
|
|
126
|
-
interactive prompt that pre-loads the project code.
|
|
115
|
+
Run `rake test` to run the tests.
|
|
127
116
|
|
|
128
117
|
|
|
129
118
|
## License
|
|
130
119
|
|
|
131
|
-
[MIT License](
|
|
120
|
+
[MIT License](LICENSE.txt)
|
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,21 +6,20 @@ class Object
|
|
|
6
6
|
# Object, unmodified.
|
|
7
7
|
#
|
|
8
8
|
# For information about +options+ see
|
|
9
|
-
# {https://docs.ruby-lang.org/en/
|
|
10
|
-
# +JSON.generate+}. By default, this method uses
|
|
11
|
-
# {https://docs.ruby-lang.org/en/
|
|
12
|
-
# +JSON.
|
|
9
|
+
# {https://docs.ruby-lang.org/en/master/JSON.html#method-i-generate
|
|
10
|
+
# +JSON.generate+}. By default, this method uses the default options
|
|
11
|
+
# for {https://docs.ruby-lang.org/en/master/JSON.html#method-i-dump
|
|
12
|
+
# +JSON.dump+}.
|
|
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
|
-
|
|
23
|
-
file.to_pathname.write_text(self.to_json(options))
|
|
22
|
+
file.to_pathname.write_text(JSON.dump(self, nil, nil, options))
|
|
24
23
|
self
|
|
25
24
|
end
|
|
26
25
|
|
|
@@ -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,14 +43,14 @@ 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
|
-
JSON.load(self, nil, options)
|
|
53
|
+
JSON.load(self, nil, { create_additions: true, **options })
|
|
53
54
|
end
|
|
54
55
|
|
|
55
56
|
end
|