refinements 7.1.0 → 7.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/{LICENSE.md → LICENSE.adoc} +29 -30
- data/README.adoc +373 -0
- data/lib/refinements.rb +1 -1
- data/lib/refinements/arrays.rb +4 -1
- data/lib/refinements/big_decimals.rb +0 -1
- data/lib/refinements/files.rb +1 -0
- data/lib/refinements/hashes.rb +45 -6
- data/lib/refinements/identity.rb +4 -15
- data/lib/refinements/pathnames.rb +29 -0
- data/lib/refinements/strings.rb +23 -28
- metadata +40 -39
- metadata.gz.sig +0 -0
- data/README.md +0 -263
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b33464363d548d0cc888f7f3b1efac924eb2490a3846353589d892900d03de0d
|
4
|
+
data.tar.gz: da1d401505c092c0b2e8850f476affb1932b79db770b4f11c263300e208f3457
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c8072c3a3d8348e871ed72c7519295fb4ade95e2463bf56424f42689909dc7113805fbc817012196a395442af811f4d215a5bc89cf8952bd4506b0a4cd49536
|
7
|
+
data.tar.gz: 7797a5f506eb95d5999234c6e506ef9b0f4b949cfa41f831fcf883425fea7cb3da3b3ff3ef72f5c218236c006c38aeb2a957127ccf5177748fc3cac4291498ba
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/{LICENSE.md → LICENSE.adoc}
RENAMED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
= Apache License
|
2
2
|
|
3
3
|
Version 2.0, January 2004
|
4
4
|
|
@@ -6,7 +6,7 @@ http://www.apache.org/licenses
|
|
6
6
|
|
7
7
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
8
8
|
|
9
|
-
|
9
|
+
== 1. Definitions
|
10
10
|
|
11
11
|
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by
|
12
12
|
Sections 1 through 9 of this document.
|
@@ -54,14 +54,14 @@ Contribution."
|
|
54
54
|
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a
|
55
55
|
Contribution has been received by Licensor and subsequently incorporated within the Work.
|
56
56
|
|
57
|
-
|
57
|
+
== 2. Grant of Copyright License
|
58
58
|
|
59
59
|
Subject to the terms and conditions of this License, each Contributor hereby grants to You a
|
60
60
|
perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
61
61
|
reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and
|
62
62
|
distribute the Work and such Derivative Works in Source or Object form.
|
63
63
|
|
64
|
-
|
64
|
+
== 3. Grant of Patent License
|
65
65
|
|
66
66
|
Subject to the terms and conditions of this License, each Contributor hereby grants to You a
|
67
67
|
perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
@@ -74,39 +74,38 @@ a Contribution incorporated within the Work constitutes direct or contributory p
|
|
74
74
|
then any patent licenses granted to You under this License for that Work shall terminate as of the
|
75
75
|
date such litigation is filed.
|
76
76
|
|
77
|
-
|
77
|
+
== 4. Redistribution
|
78
78
|
|
79
79
|
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with
|
80
80
|
or without modifications, and in Source or Object form, provided that You meet the following
|
81
81
|
conditions:
|
82
82
|
|
83
|
-
|
83
|
+
. You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
84
84
|
|
85
|
-
|
86
|
-
|
85
|
+
. You must cause any modified files to carry prominent notices stating that You changed the files;
|
86
|
+
and
|
87
87
|
|
88
|
-
|
89
|
-
|
90
|
-
|
88
|
+
. You must retain, in the Source form of any Derivative Works that You distribute, all copyright,
|
89
|
+
patent, trademark, and attribution notices from the Source form of the Work, excluding those
|
90
|
+
notices that do not pertain to any part of the Derivative Works; and
|
91
91
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
construed as modifying the License.
|
92
|
+
. If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works
|
93
|
+
that You distribute must include a readable copy of the attribution notices contained within such
|
94
|
+
NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in
|
95
|
+
at least one of the following places: within a NOTICE text file distributed as part of the
|
96
|
+
Derivative Works; within the Source form or documentation, if provided along with the Derivative
|
97
|
+
Works; or, within a display generated by the Derivative Works, if and wherever such third-party
|
98
|
+
notices normally appear. The contents of the NOTICE file are for informational purposes only and
|
99
|
+
do not modify the License. You may add Your own attribution notices within Derivative Works that
|
100
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such
|
101
|
+
additional attribution notices cannot be construed as modifying the License.
|
103
102
|
|
104
103
|
You may add Your own copyright statement to Your modifications and may provide additional or
|
105
104
|
different license terms and conditions for use, reproduction, or distribution of Your modifications,
|
106
105
|
or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of
|
107
106
|
the Work otherwise complies with the conditions stated in this License.
|
108
107
|
|
109
|
-
|
108
|
+
== 5. Submission of Contributions
|
110
109
|
|
111
110
|
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the
|
112
111
|
Work by You to the Licensor shall be under the terms and conditions of this License, without any
|
@@ -114,13 +113,13 @@ additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
114
113
|
the terms of any separate license agreement you may have executed with Licensor regarding such
|
115
114
|
Contributions.
|
116
115
|
|
117
|
-
|
116
|
+
== 6. Trademarks
|
118
117
|
|
119
118
|
This License does not grant permission to use the trade names, trademarks, service marks, or product
|
120
119
|
names of the Licensor, except as required for reasonable and customary use in describing the origin
|
121
120
|
of the Work and reproducing the content of the NOTICE file.
|
122
121
|
|
123
|
-
|
122
|
+
== 7. Disclaimer of Warranty
|
124
123
|
|
125
124
|
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each
|
126
125
|
Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
@@ -129,7 +128,7 @@ TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. Y
|
|
129
128
|
responsible for determining the appropriateness of using or redistributing the Work and assume any
|
130
129
|
risks associated with Your exercise of permissions under this License.
|
131
130
|
|
132
|
-
|
131
|
+
== 8. Limitation of Liability
|
133
132
|
|
134
133
|
In no event and under no legal theory, whether in tort (including negligence), contract, or
|
135
134
|
otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or
|
@@ -139,7 +138,7 @@ License or out of the use or inability to use the Work (including but not limite
|
|
139
138
|
loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial
|
140
139
|
damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
141
140
|
|
142
|
-
|
141
|
+
== 9. Accepting Warranty or Additional Liability
|
143
142
|
|
144
143
|
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee
|
145
144
|
for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights
|
@@ -151,11 +150,11 @@ additional liability.
|
|
151
150
|
|
152
151
|
END OF TERMS AND CONDITIONS
|
153
152
|
|
154
|
-
Copyright
|
153
|
+
Copyright link:https://www.alchemists.io[Alchemists].
|
155
154
|
|
156
155
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
|
157
|
-
compliance with the License. You may obtain a
|
158
|
-
the License.
|
156
|
+
compliance with the License. You may obtain a link:https://www.apache.org/licenses/LICENSE-2.0[copy]
|
157
|
+
of the License.
|
159
158
|
|
160
159
|
Unless required by applicable law or agreed to in writing, software distributed under the License is
|
161
160
|
distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
data/README.adoc
ADDED
@@ -0,0 +1,373 @@
|
|
1
|
+
:toc: macro
|
2
|
+
:toclevels: 5
|
3
|
+
:figure-caption!:
|
4
|
+
|
5
|
+
= Refinements
|
6
|
+
|
7
|
+
[link=http://badge.fury.io/rb/refinements]
|
8
|
+
image::https://badge.fury.io/rb/refinements.svg[Gem Version]
|
9
|
+
[link=https://circleci.com/gh/bkuhlmann/refinements]
|
10
|
+
image::https://circleci.com/gh/bkuhlmann/refinements.svg?style=svg[Circle CI Status]
|
11
|
+
|
12
|
+
A collection of refinements (enhancements) to core Ruby objects.
|
13
|
+
|
14
|
+
toc::[]
|
15
|
+
|
16
|
+
== Features
|
17
|
+
|
18
|
+
* *Arrays*:
|
19
|
+
** `#compress` - Removes `nil` and empty values without modifying itself.
|
20
|
+
** `#compress!` - Removes `nil` and empty values while modifying itself.
|
21
|
+
** `#ring` - Answers a circular array which can enumerate before, current, after elements.
|
22
|
+
* *BigDecimals*:
|
23
|
+
** `#inspect` - Allows one to inspect a big decimal with numeric representation.
|
24
|
+
* *DateTimes*:
|
25
|
+
** `.utc` - Answers new DateTime object for current UTC date/time.
|
26
|
+
* *Files*:
|
27
|
+
** `#rewrite` - When given a file path and a block, it provides the contents of the recently read
|
28
|
+
file for manipulation and immediate writing back to the same file.
|
29
|
+
* *Hashes*:
|
30
|
+
** `.infinite` - Answers new hash where missing keys, even deeply nested, answer an empty hash.
|
31
|
+
** `.with_default` - Answers new hash where every top-level missing key has the same default value.
|
32
|
+
** `#except` - Answers new hash with given keys removed without modifying itself.
|
33
|
+
** `#except!` - Answers new hash with given keys removed while modifying itself.
|
34
|
+
** `#symbolize_keys` - Converts keys to symbols without modifying itself.
|
35
|
+
** `#symbolize_keys!` - Converts keys to symbols while modifying itself.
|
36
|
+
** `#deep_merge` - Merges deeply nested hashes together without modifying itself.
|
37
|
+
** `#deep_merge!` - Merges deeply nested hashes together while modifying itself.
|
38
|
+
** `#deep_symbolize_keys` - Symbolizes keys of nested hash without modifying itself. Does not handle
|
39
|
+
nested arrays, though.
|
40
|
+
** `#deep_symbolize_keys!` - Symbolizes keys of nested hash while modifying itself. Does not handle
|
41
|
+
nested arrays, though.
|
42
|
+
** `#recurse` - Applies block to nested hash. Does not handle nested arrays, though.
|
43
|
+
** `#rekey` - Transforms keys per mapping (size of mapping can vary) without modifying itself.
|
44
|
+
** `#rekey!` - Transforms keys per mapping (size of mapping can vary) while modifying itself.
|
45
|
+
** `#reverse_merge` - Merges calling hash into passed in hash without modifying itself.
|
46
|
+
** `#reverse_merge!` - Merges calling hash into passed in hash while modifying itself.
|
47
|
+
** `#use` - Passes each hash value as a block argument for further processing.
|
48
|
+
* *Pathnames*:
|
49
|
+
** `Pathname` - Conversion function (refined from `Kernel`) which can cast `nil` into a pathname.
|
50
|
+
** `#name` - Answers file name without extension.
|
51
|
+
** `#copy` - Copies file from current location to new location.
|
52
|
+
** `#directories` - Answers all or filtered directories for current path.
|
53
|
+
** `#extensions` - Answers file extensions as an array.
|
54
|
+
** `#files` - Answers all or filtered files for current path.
|
55
|
+
** `#relative_parent_from` - Answers relative path from parent directory. This is a complement to
|
56
|
+
`#relative_path_from`.
|
57
|
+
** `#make_ancestors` - Ensures all ancestor directories are created for a path.
|
58
|
+
** `#rewrite` - When given a block, it provides the contents of the recently read file for
|
59
|
+
manipulation and immediate writing back to the same file.
|
60
|
+
** `#touch` - Updates access and modification times for path. Defaults to current time.
|
61
|
+
* *Strings*:
|
62
|
+
** `#first` - Answers first character of a string or first set of characters if given a number.
|
63
|
+
** `#last` - Answers last character of a string or last set of characters if given a number.
|
64
|
+
** `#blank?` - Answers `true`/`false` based on whether string is blank or not
|
65
|
+
(i.e. `<space>`, `\n`, `\t`, `\r`).
|
66
|
+
** `#up` - Answers string with only first letter upcased.
|
67
|
+
** `#down` - Answers string with only first letter downcased.
|
68
|
+
** `#camelcase` - Answers a camelcased string.
|
69
|
+
** `#snakecase` - Answers a snakecased string.
|
70
|
+
** `#titleize` - Answers titleized string.
|
71
|
+
** `#to_bool` - Answers string as a boolean.
|
72
|
+
|
73
|
+
== Requirements
|
74
|
+
|
75
|
+
. https://www.ruby-lang.org[Ruby].
|
76
|
+
. A solid understanding of link:https://www.alchemists.io/articles/ruby_refinements[Ruby refinements
|
77
|
+
and lexical scope].
|
78
|
+
|
79
|
+
== Setup
|
80
|
+
|
81
|
+
=== Production
|
82
|
+
|
83
|
+
To install, run:
|
84
|
+
|
85
|
+
[source,bash]
|
86
|
+
----
|
87
|
+
gem install refinements
|
88
|
+
----
|
89
|
+
|
90
|
+
Add the following to your Gemfile file:
|
91
|
+
|
92
|
+
[source,ruby]
|
93
|
+
----
|
94
|
+
gem "refinements"
|
95
|
+
----
|
96
|
+
|
97
|
+
=== Development
|
98
|
+
|
99
|
+
To contribute, run:
|
100
|
+
|
101
|
+
[source,bash]
|
102
|
+
----
|
103
|
+
git clone https://github.com/bkuhlmann/refinements.git
|
104
|
+
cd refinements
|
105
|
+
bin/setup
|
106
|
+
----
|
107
|
+
|
108
|
+
You can also use the IRB console for direct access to all objects:
|
109
|
+
|
110
|
+
[source,bash]
|
111
|
+
----
|
112
|
+
bin/console
|
113
|
+
----
|
114
|
+
|
115
|
+
== Usage
|
116
|
+
|
117
|
+
=== Requires
|
118
|
+
|
119
|
+
If all refinements are not desired, add the following to your `+Gemfile+` instead:
|
120
|
+
|
121
|
+
[source,ruby]
|
122
|
+
----
|
123
|
+
gem "refinements", require: false
|
124
|
+
----
|
125
|
+
|
126
|
+
…then require the specific refinement, as needed. Example:
|
127
|
+
|
128
|
+
[source,ruby]
|
129
|
+
----
|
130
|
+
require "refinements/arrays"
|
131
|
+
require "refinements/big_decimals"
|
132
|
+
require "refinements/date_times"
|
133
|
+
require "refinements/files"
|
134
|
+
require "refinements/hashes"
|
135
|
+
require "refinements/pathnames"
|
136
|
+
require "refinements/strings"
|
137
|
+
----
|
138
|
+
|
139
|
+
=== Using
|
140
|
+
|
141
|
+
Much like including/extending a module, you’ll need modify your object(s) to use the refinement(s):
|
142
|
+
|
143
|
+
[source,ruby]
|
144
|
+
----
|
145
|
+
class Example
|
146
|
+
using Refinements::Arrays
|
147
|
+
using Refinements::BigDecimals
|
148
|
+
using Refinements::DateTimes
|
149
|
+
using Refinements::Files
|
150
|
+
using Refinements::Hashes
|
151
|
+
using Refinements::Pathnames
|
152
|
+
using Refinements::Strings
|
153
|
+
end
|
154
|
+
----
|
155
|
+
|
156
|
+
=== Examples
|
157
|
+
|
158
|
+
The following sections demonstrate how each refinement enriches your objects with new capabilities.
|
159
|
+
|
160
|
+
==== Array
|
161
|
+
|
162
|
+
[source,ruby]
|
163
|
+
----
|
164
|
+
example = ["An", nil, "", "Example"]
|
165
|
+
example.compress # => ["An", "Example"]
|
166
|
+
example # => ["An", nil, "", "Example"]
|
167
|
+
|
168
|
+
example = ["An", nil, "", "Example"]
|
169
|
+
example.compress! # => ["An", "Example"]
|
170
|
+
example # => ["An", "Example"]
|
171
|
+
|
172
|
+
example = [1, 2, 3]
|
173
|
+
example.ring # => #<Enumerator: ...>
|
174
|
+
example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
|
175
|
+
# [3 1 2]
|
176
|
+
# [1 2 3]
|
177
|
+
# [2 3 1]
|
178
|
+
----
|
179
|
+
|
180
|
+
==== Big Decimal
|
181
|
+
|
182
|
+
[source,ruby]
|
183
|
+
----
|
184
|
+
BigDecimal.new("5.0E-10").inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"
|
185
|
+
----
|
186
|
+
|
187
|
+
==== DateTime
|
188
|
+
|
189
|
+
[source,ruby]
|
190
|
+
----
|
191
|
+
DateTime.utc # => #<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>
|
192
|
+
----
|
193
|
+
|
194
|
+
==== File
|
195
|
+
|
196
|
+
[source,ruby]
|
197
|
+
----
|
198
|
+
File.rewrite("/test.txt") { |content| content.gsub "[placeholder]", "example" }
|
199
|
+
----
|
200
|
+
|
201
|
+
==== Hash
|
202
|
+
|
203
|
+
[source,ruby]
|
204
|
+
----
|
205
|
+
example = Hash.infinite
|
206
|
+
example[:a] # => {}
|
207
|
+
example[:a][:b][:c] # => {}
|
208
|
+
|
209
|
+
example = Hash.with_default ""
|
210
|
+
example[:a] # => ""
|
211
|
+
example = Hash.with_default []
|
212
|
+
example[:b] # => []
|
213
|
+
|
214
|
+
example = {a: 1, b: 2, c: 3}
|
215
|
+
example.except :a, :b # => {c: 3}
|
216
|
+
example # => {a: 1, b: 2, c: 3}
|
217
|
+
|
218
|
+
example = {a: 1, b: 2, c: 3}
|
219
|
+
example.except! :a, :b # => {c: 3}
|
220
|
+
example # => {c: 3}
|
221
|
+
|
222
|
+
example = {"a" => 1, "b" => 2}
|
223
|
+
example.symbolize_keys # => {a: 1, b: 2}
|
224
|
+
example # => {"a" => 1, "b" => 2}
|
225
|
+
|
226
|
+
example = {"a" => 1, "b" => 2}
|
227
|
+
example.symbolize_keys! # => {a: 1, b: 2}
|
228
|
+
example # => {a: 1, b: 2}
|
229
|
+
|
230
|
+
example = {a: 1, b: 2, c: 3}
|
231
|
+
example.slice :a, :c # => {a: 1, c: 3}
|
232
|
+
example # => {a: 1, b: 2, c: 3}
|
233
|
+
|
234
|
+
example = {a: 1, b: 2, c: 3}
|
235
|
+
example.slice! :a, :c # => {a: 1, c: 3}
|
236
|
+
example # => {a: 1, c: 3}
|
237
|
+
|
238
|
+
example = {a: "A", b: {one: "One", two: "Two"}}
|
239
|
+
example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
|
240
|
+
example # => {a: "A", b: {one: "One", two: "Two"}}
|
241
|
+
|
242
|
+
example = {a: "A", b: {one: "One", two: "Two"}}
|
243
|
+
example.deep_merge! b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
|
244
|
+
example # => {a: "A", b: {one: 1, two: "Two"}}
|
245
|
+
|
246
|
+
example = {"a" => {"b" => 2}}
|
247
|
+
example.deep_symbolize_keys # => {a: {b: 1}}
|
248
|
+
example # => {"a" => {"b" => 2}}
|
249
|
+
|
250
|
+
example = {"a" => {"b" => 2}}
|
251
|
+
example.deep_symbolize_keys! # => {a: {b: 1}}
|
252
|
+
example # => {a: {b: 1}}
|
253
|
+
|
254
|
+
example = {"a" => {"b" => 1}}
|
255
|
+
example.recurse(&:symbolize_keys) # => {a: {b: 1}}
|
256
|
+
example.recurse(&:invert) # => {{"b" => 1} => "a"}
|
257
|
+
|
258
|
+
example = {a: 1, b: 2, c: 3}
|
259
|
+
example.rekey a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
|
260
|
+
example # => {a: 1, b: 2, c: 3}
|
261
|
+
|
262
|
+
example = {a: 1, b: 2, c: 3}
|
263
|
+
example.rekey! a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
|
264
|
+
example # => {amber: 1, blue: 2, c: 3}
|
265
|
+
|
266
|
+
example = {a: 1, b: 2}
|
267
|
+
example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
|
268
|
+
example # => {a: 1, b: 2}
|
269
|
+
|
270
|
+
example = {a: 1, b: 2}
|
271
|
+
example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
|
272
|
+
example # => {a: 1, b: 2, c: 3}
|
273
|
+
|
274
|
+
example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
|
275
|
+
example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"
|
276
|
+
----
|
277
|
+
|
278
|
+
==== Pathname
|
279
|
+
|
280
|
+
[source,ruby]
|
281
|
+
----
|
282
|
+
Pathname(nil) # => Pathname("")
|
283
|
+
|
284
|
+
Pathname("example.txt").name # => Pathname("example")
|
285
|
+
|
286
|
+
Pathname("input.txt").copy Pathname("output.txt")
|
287
|
+
|
288
|
+
Pathname("/example").directories # => [Pathname("a"), Pathname("b")]
|
289
|
+
Pathname("/example").directories "a*" # => [Pathname("a")]
|
290
|
+
|
291
|
+
Pathname("example.txt.erb").extensions # => [".txt", ".erb"]
|
292
|
+
|
293
|
+
Pathname("/example").files # => [Pathname("a.txt"), Pathname("a.png")]
|
294
|
+
Pathname("/example").files "*.png" # => [Pathname("a.png")]
|
295
|
+
|
296
|
+
Pathname("/one/two/three").relative_parent_from("/one") # => Pathname "two"
|
297
|
+
|
298
|
+
Pathname("/one/two").make_ancestors
|
299
|
+
Pathname("/one").exist? # => true
|
300
|
+
Pathname("/one/two").exist? # => false
|
301
|
+
|
302
|
+
Pathname("/test.txt").rewrite { |content| content.sub "[placeholder]", "example" }
|
303
|
+
|
304
|
+
Pathname("example.txt").touch
|
305
|
+
Pathname("example.txt").touch at: Time.now - 1
|
306
|
+
----
|
307
|
+
|
308
|
+
==== String
|
309
|
+
|
310
|
+
[source,ruby]
|
311
|
+
----
|
312
|
+
"example".first # => "e"
|
313
|
+
"example".first 4 # => "exam"
|
314
|
+
|
315
|
+
"instant".last # => "t"
|
316
|
+
"instant".last 3 # => "ant"
|
317
|
+
|
318
|
+
" \n\t\r".blank? # => true
|
319
|
+
|
320
|
+
"example".up # => "Example"
|
321
|
+
|
322
|
+
"EXAMPLE".down # => "eXAMPLE"
|
323
|
+
|
324
|
+
"this_is_an_example".camelcase # => "ThisIsAnExample"
|
325
|
+
|
326
|
+
"ThisIsAnExample".snakecase # => "this_is_an_example"
|
327
|
+
|
328
|
+
"ThisIsAnExample".titleize # => "This Is An Example"
|
329
|
+
|
330
|
+
"true".to_bool # => true
|
331
|
+
"yes".to_bool # => true
|
332
|
+
"1".to_bool # => true
|
333
|
+
"".to_bool # => false
|
334
|
+
"example".to_bool # => false
|
335
|
+
----
|
336
|
+
|
337
|
+
== Tests
|
338
|
+
|
339
|
+
To test, run:
|
340
|
+
|
341
|
+
[source,bash]
|
342
|
+
----
|
343
|
+
bundle exec rake
|
344
|
+
----
|
345
|
+
|
346
|
+
== Versioning
|
347
|
+
|
348
|
+
Read link:https://semver.org[Semantic Versioning] for details. Briefly, it means:
|
349
|
+
|
350
|
+
* Major (X.y.z) - Incremented for any backwards incompatible public API changes.
|
351
|
+
* Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
|
352
|
+
* Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
|
353
|
+
|
354
|
+
== Code of Conduct
|
355
|
+
|
356
|
+
Please note that this project is released with a link:CODE_OF_CONDUCT.adoc[CODE OF CONDUCT]. By
|
357
|
+
participating in this project you agree to abide by its terms.
|
358
|
+
|
359
|
+
== Contributions
|
360
|
+
|
361
|
+
Read link:CONTRIBUTING.adoc[CONTRIBUTING] for details.
|
362
|
+
|
363
|
+
== License
|
364
|
+
|
365
|
+
Read link:LICENSE.adoc[LICENSE] for details.
|
366
|
+
|
367
|
+
== History
|
368
|
+
|
369
|
+
Read link:CHANGES.adoc[CHANGES] for details.
|
370
|
+
|
371
|
+
== Credits
|
372
|
+
|
373
|
+
Engineered by link:https://www.alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|
data/lib/refinements.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
require "refinements/identity"
|
4
4
|
require "refinements/arrays"
|
5
|
-
require "refinements/date_times"
|
6
5
|
require "refinements/big_decimals"
|
6
|
+
require "refinements/date_times"
|
7
7
|
require "refinements/files"
|
8
8
|
require "refinements/hashes"
|
9
9
|
require "refinements/pathnames"
|
data/lib/refinements/arrays.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Refinements
|
4
|
-
# Refinements for arrays.
|
5
4
|
module Arrays
|
6
5
|
refine Array do
|
7
6
|
def compress
|
@@ -11,6 +10,10 @@ module Refinements
|
|
11
10
|
def compress!
|
12
11
|
replace compress
|
13
12
|
end
|
13
|
+
|
14
|
+
def ring &block
|
15
|
+
[last, *self, first].each_cons 3, &block
|
16
|
+
end
|
14
17
|
end
|
15
18
|
end
|
16
19
|
end
|
data/lib/refinements/files.rb
CHANGED
data/lib/refinements/hashes.rb
CHANGED
@@ -1,8 +1,17 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Refinements
|
4
|
-
# Refinements for hashes.
|
5
4
|
module Hashes
|
5
|
+
refine Hash.singleton_class do
|
6
|
+
def infinite
|
7
|
+
new { |new_hash, missing_key| new_hash[missing_key] = new(&new_hash.default_proc) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def with_default value
|
11
|
+
new { |new_hash, missing_key| new_hash[missing_key] = value }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
6
15
|
refine Hash do
|
7
16
|
def except *keys
|
8
17
|
reject { |key, _value| keys.include? key }
|
@@ -13,22 +22,22 @@ module Refinements
|
|
13
22
|
end
|
14
23
|
|
15
24
|
def symbolize_keys
|
16
|
-
|
25
|
+
reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
|
17
26
|
end
|
18
27
|
|
19
28
|
def symbolize_keys!
|
20
|
-
|
21
|
-
self
|
29
|
+
replace symbolize_keys
|
22
30
|
end
|
23
31
|
|
24
32
|
def deep_merge other
|
25
33
|
dup.deep_merge! other
|
26
34
|
end
|
27
35
|
|
28
|
-
# :reek:FeatureEnvy
|
29
36
|
def deep_merge! other
|
37
|
+
clazz = self.class
|
38
|
+
|
30
39
|
merge! other do |_key, this_value, other_value|
|
31
|
-
if this_value.is_a?(
|
40
|
+
if this_value.is_a?(clazz) && other_value.is_a?(clazz)
|
32
41
|
this_value.deep_merge other_value
|
33
42
|
else
|
34
43
|
other_value
|
@@ -36,11 +45,41 @@ module Refinements
|
|
36
45
|
end
|
37
46
|
end
|
38
47
|
|
48
|
+
def deep_symbolize_keys
|
49
|
+
recurse(&:symbolize_keys)
|
50
|
+
end
|
51
|
+
|
52
|
+
def deep_symbolize_keys!
|
53
|
+
recurse(&:symbolize_keys!)
|
54
|
+
end
|
55
|
+
|
56
|
+
def recurse &block
|
57
|
+
return self unless block_given?
|
58
|
+
|
59
|
+
transform = yield self
|
60
|
+
|
61
|
+
transform.each do |key, value|
|
62
|
+
transform[key] = value.recurse(&block) if value.is_a? self.class
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def rekey mapping = {}
|
67
|
+
return self if mapping.empty?
|
68
|
+
|
69
|
+
transform_keys { |key| mapping[key] || key }
|
70
|
+
end
|
71
|
+
|
72
|
+
def rekey! mapping = {}
|
73
|
+
replace rekey(mapping)
|
74
|
+
end
|
75
|
+
|
39
76
|
def reverse_merge other
|
77
|
+
warn "[DEPRECATION]: #reverse_merge is deprecated, use #merge instead."
|
40
78
|
other.merge self
|
41
79
|
end
|
42
80
|
|
43
81
|
def reverse_merge! other
|
82
|
+
warn "[DEPRECATION]: #reverse_merge! is deprecated, use #merge! instead."
|
44
83
|
merge!(other) { |_key, old_value, _new_value| old_value }
|
45
84
|
end
|
46
85
|
|
data/lib/refinements/identity.rb
CHANGED
@@ -3,20 +3,9 @@
|
|
3
3
|
module Refinements
|
4
4
|
# Gem identity information.
|
5
5
|
module Identity
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
def self.label
|
11
|
-
"Refinements"
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.version
|
15
|
-
"7.1.0"
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.version_label
|
19
|
-
"#{label} #{version}"
|
20
|
-
end
|
6
|
+
NAME = "refinements"
|
7
|
+
LABEL = "Refinements"
|
8
|
+
VERSION = "7.6.0"
|
9
|
+
VERSION_LABEL = "#{LABEL} #{VERSION}"
|
21
10
|
end
|
22
11
|
end
|
@@ -4,6 +4,14 @@ require "pathname"
|
|
4
4
|
|
5
5
|
module Refinements
|
6
6
|
module Pathnames
|
7
|
+
refine Kernel do
|
8
|
+
def Pathname object
|
9
|
+
return super(String(object)) unless object
|
10
|
+
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
7
15
|
refine Pathname do
|
8
16
|
def name
|
9
17
|
basename extname
|
@@ -15,6 +23,27 @@ module Refinements
|
|
15
23
|
self
|
16
24
|
end
|
17
25
|
|
26
|
+
def directories pattern = "*"
|
27
|
+
glob(pattern).select(&:directory?).sort
|
28
|
+
end
|
29
|
+
|
30
|
+
def extensions
|
31
|
+
basename.to_s.split(/(?=\.)+/).tap(&:shift)
|
32
|
+
end
|
33
|
+
|
34
|
+
def files pattern = "*"
|
35
|
+
glob(pattern).select(&:file?).sort
|
36
|
+
end
|
37
|
+
|
38
|
+
def relative_parent_from root
|
39
|
+
relative_path_from(root).parent
|
40
|
+
end
|
41
|
+
|
42
|
+
def make_ancestors
|
43
|
+
dirname.mkpath
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
18
47
|
def rewrite
|
19
48
|
read.then { |content| write yield(content) if block_given? }
|
20
49
|
self
|
data/lib/refinements/strings.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Refinements
|
4
|
-
# Refinements for strings.
|
5
4
|
module Strings
|
6
5
|
refine String.singleton_class do
|
7
6
|
def delimiters
|
8
|
-
%r([a-z][A-Z]|\s
|
7
|
+
%r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+)
|
9
8
|
end
|
10
9
|
end
|
11
10
|
|
@@ -18,20 +17,18 @@ module Refinements
|
|
18
17
|
return self[0] if max.zero?
|
19
18
|
return "" if max.negative?
|
20
19
|
|
21
|
-
self[
|
20
|
+
self[..(max - 1)]
|
22
21
|
end
|
23
22
|
|
24
|
-
# :reek:TooManyStatements
|
25
23
|
def last number = 0
|
26
24
|
return self if empty?
|
27
25
|
|
28
26
|
min = Integer number
|
29
|
-
max = size - 1
|
30
27
|
|
31
|
-
return self[
|
28
|
+
return self[size - 1] if min.zero?
|
32
29
|
return "" if min.negative?
|
33
30
|
|
34
|
-
self[(min + 1)..
|
31
|
+
self[(min + 1)..]
|
35
32
|
end
|
36
33
|
|
37
34
|
def blank?
|
@@ -51,33 +48,31 @@ module Refinements
|
|
51
48
|
end
|
52
49
|
|
53
50
|
def camelcase
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
up
|
60
|
-
end
|
51
|
+
return up unless match? self.class.delimiters
|
52
|
+
|
53
|
+
split(%r(\s*-\s*|\s*/\s*|\s*:+\s*)).then { |parts| combine parts, :up, "::" }
|
54
|
+
.then { |text| text.split(/\s*_\s*|\s+/) }
|
55
|
+
.then { |parts| combine parts, :up }
|
61
56
|
end
|
62
57
|
|
63
58
|
def snakecase
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
downcase
|
70
|
-
end
|
59
|
+
return downcase unless match? self.class.delimiters
|
60
|
+
|
61
|
+
split(%r(\s*-\s*|\s*/\s*|\s*:+\s*)).then { |parts| combine parts, :down, "/" }
|
62
|
+
.then { |text| text.split(/(?=[A-Z])|\s*_\s*|\s+/) }
|
63
|
+
.then { |parts| combine parts, :down, "_" }
|
71
64
|
end
|
72
65
|
|
73
66
|
def titleize
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
67
|
+
return capitalize unless match? self.class.delimiters
|
68
|
+
|
69
|
+
split(/(?=[A-Z])|\s*_\s*|\s*-\s*|\s+/).then { |parts| combine parts, :up, " " }
|
70
|
+
.then { |text| text.split %r(\s*/\s*|\s*:+\s*) }
|
71
|
+
.then { |parts| combine parts, :up, "/" }
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_bool
|
75
|
+
%w[true yes on t y 1].include? downcase.strip
|
81
76
|
end
|
82
77
|
|
83
78
|
private
|
metadata
CHANGED
@@ -1,18 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: refinements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 7.
|
4
|
+
version: 7.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain:
|
11
11
|
- |
|
12
12
|
-----BEGIN CERTIFICATE-----
|
13
|
-
MIIC/
|
14
|
-
|
15
|
-
|
13
|
+
MIIC/jCCAeagAwIBAgIBAzANBgkqhkiG9w0BAQsFADAlMSMwIQYDVQQDDBpicm9v
|
14
|
+
a2UvREM9YWxjaGVtaXN0cy9EQz1pbzAeFw0yMDAzMTUxNDQ1MzJaFw0yMTAzMTUx
|
15
|
+
NDQ1MzJaMCUxIzAhBgNVBAMMGmJyb29rZS9EQz1hbGNoZW1pc3RzL0RDPWlvMIIB
|
16
16
|
IjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA6l1qpXTiomH1RfMRloyw7MiE
|
17
17
|
xyVx/x8Yc3EupdH7uhNaTXQGyORN6aOY//1QXXMHIZ9tW74nZLhesWMSUMYy0XhB
|
18
18
|
brs+KkurHnc9FnEJAbG7ebGvl/ncqZt72nQvaxpDxvuCBHgJAz+8i5wl6FhLw+oT
|
@@ -20,15 +20,15 @@ cert_chain:
|
|
20
20
|
D5vkU0YlAm1r98BymuJlcQ1qdkVEI1d48ph4kcS0S0nv1RiuyVb6TCAR3Nu3VaVq
|
21
21
|
3fPzZKJLZBx67UvXdbdicWPiUR75elI4PXpLIic3xytaF52ZJYyKZCNZJhNwfQID
|
22
22
|
AQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU0nzow9vc
|
23
|
-
2CdikiiE3fJhP/
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
2CdikiiE3fJhP/gY4ggwDQYJKoZIhvcNAQELBQADggEBAIHhAlD3po4sTYqacXaQ
|
24
|
+
XI9jIhrfMy//2PgbHWcETtlJPBeNUbbSNBABcllUHKqYsVDlSvSmss034KSWNR8F
|
25
|
+
bF1GcloicyvcCC4y6IoW4it0COAcdeaaxkxiBSgKdQFpff9REnDlIKK4uQ9lLxIo
|
26
|
+
Y2G5xubiziKZkyfWFuSr67PIjW3Bu673D1JVBArhA1qbgQmYQcy1CkGOjo+iO8Nf
|
27
|
+
7u/QSfBHb+r/bXhKscDgPpnKwbUmvgO2+94zJG9KsrmIydlzYfsD09aXKx0t6Xy4
|
28
|
+
2XV8FRa7/JimI07sPLC13eLY3xd/aYTi85Z782KIA4j0G8XEEWAX0ouBhlXPocZv
|
29
|
+
QWc=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2020-
|
31
|
+
date: 2020-07-04 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler-audit
|
@@ -50,28 +50,28 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '14.
|
53
|
+
version: '14.2'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '14.
|
60
|
+
version: '14.2'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
|
-
name: git-
|
62
|
+
name: git-lint
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - "~>"
|
66
66
|
- !ruby/object:Gem::Version
|
67
|
-
version: '
|
67
|
+
version: '1.0'
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: !ruby/object:Gem::Requirement
|
71
71
|
requirements:
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
|
-
version: '
|
74
|
+
version: '1.0'
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
76
|
name: guard-rspec
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -92,28 +92,28 @@ dependencies:
|
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '0.
|
95
|
+
version: '0.13'
|
96
96
|
type: :development
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: '0.
|
102
|
+
version: '0.13'
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: pry-byebug
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|
106
106
|
requirements:
|
107
107
|
- - "~>"
|
108
108
|
- !ruby/object:Gem::Version
|
109
|
-
version: '3.
|
109
|
+
version: '3.9'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
112
|
version_requirements: !ruby/object:Gem::Requirement
|
113
113
|
requirements:
|
114
114
|
- - "~>"
|
115
115
|
- !ruby/object:Gem::Version
|
116
|
-
version: '3.
|
116
|
+
version: '3.9'
|
117
117
|
- !ruby/object:Gem::Dependency
|
118
118
|
name: rake
|
119
119
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,14 +134,14 @@ dependencies:
|
|
134
134
|
requirements:
|
135
135
|
- - "~>"
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version: '
|
137
|
+
version: '6.0'
|
138
138
|
type: :development
|
139
139
|
prerelease: false
|
140
140
|
version_requirements: !ruby/object:Gem::Requirement
|
141
141
|
requirements:
|
142
142
|
- - "~>"
|
143
143
|
- !ruby/object:Gem::Version
|
144
|
-
version: '
|
144
|
+
version: '6.0'
|
145
145
|
- !ruby/object:Gem::Dependency
|
146
146
|
name: rspec
|
147
147
|
requirement: !ruby/object:Gem::Requirement
|
@@ -162,14 +162,14 @@ dependencies:
|
|
162
162
|
requirements:
|
163
163
|
- - "~>"
|
164
164
|
- !ruby/object:Gem::Version
|
165
|
-
version: '0.
|
165
|
+
version: '0.83'
|
166
166
|
type: :development
|
167
167
|
prerelease: false
|
168
168
|
version_requirements: !ruby/object:Gem::Requirement
|
169
169
|
requirements:
|
170
170
|
- - "~>"
|
171
171
|
- !ruby/object:Gem::Version
|
172
|
-
version: '0.
|
172
|
+
version: '0.83'
|
173
173
|
- !ruby/object:Gem::Dependency
|
174
174
|
name: rubocop-performance
|
175
175
|
requirement: !ruby/object:Gem::Requirement
|
@@ -204,14 +204,14 @@ dependencies:
|
|
204
204
|
requirements:
|
205
205
|
- - "~>"
|
206
206
|
- !ruby/object:Gem::Version
|
207
|
-
version: '1.
|
207
|
+
version: '1.39'
|
208
208
|
type: :development
|
209
209
|
prerelease: false
|
210
210
|
version_requirements: !ruby/object:Gem::Requirement
|
211
211
|
requirements:
|
212
212
|
- - "~>"
|
213
213
|
- !ruby/object:Gem::Version
|
214
|
-
version: '1.
|
214
|
+
version: '1.39'
|
215
215
|
- !ruby/object:Gem::Dependency
|
216
216
|
name: simplecov
|
217
217
|
requirement: !ruby/object:Gem::Requirement
|
@@ -226,17 +226,17 @@ dependencies:
|
|
226
226
|
- - "~>"
|
227
227
|
- !ruby/object:Gem::Version
|
228
228
|
version: '0.18'
|
229
|
-
description:
|
229
|
+
description:
|
230
230
|
email:
|
231
231
|
- brooke@alchemists.io
|
232
232
|
executables: []
|
233
233
|
extensions: []
|
234
234
|
extra_rdoc_files:
|
235
|
-
- README.
|
236
|
-
- LICENSE.
|
235
|
+
- README.adoc
|
236
|
+
- LICENSE.adoc
|
237
237
|
files:
|
238
|
-
- LICENSE.
|
239
|
-
- README.
|
238
|
+
- LICENSE.adoc
|
239
|
+
- README.adoc
|
240
240
|
- lib/refinements.rb
|
241
241
|
- lib/refinements/arrays.rb
|
242
242
|
- lib/refinements/big_decimals.rb
|
@@ -246,14 +246,15 @@ files:
|
|
246
246
|
- lib/refinements/identity.rb
|
247
247
|
- lib/refinements/pathnames.rb
|
248
248
|
- lib/refinements/strings.rb
|
249
|
-
homepage: https://
|
249
|
+
homepage: https://www.alchemists.io/projects/refinements
|
250
250
|
licenses:
|
251
251
|
- Apache-2.0
|
252
252
|
metadata:
|
253
|
-
source_code_uri: https://github.com/bkuhlmann/refinements
|
254
|
-
changelog_uri: https://github.com/bkuhlmann/refinements/blob/master/CHANGES.md
|
255
253
|
bug_tracker_uri: https://github.com/bkuhlmann/refinements/issues
|
256
|
-
|
254
|
+
changelog_uri: https://www.alchemists.io/projects/refinements/changes.html
|
255
|
+
documentation_uri: https://www.alchemists.io/projects/refinements
|
256
|
+
source_code_uri: https://github.com/bkuhlmann/refinements
|
257
|
+
post_install_message:
|
257
258
|
rdoc_options: []
|
258
259
|
require_paths:
|
259
260
|
- lib
|
@@ -268,8 +269,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
268
269
|
- !ruby/object:Gem::Version
|
269
270
|
version: '0'
|
270
271
|
requirements: []
|
271
|
-
rubygems_version: 3.1.
|
272
|
-
signing_key:
|
272
|
+
rubygems_version: 3.1.4
|
273
|
+
signing_key:
|
273
274
|
specification_version: 4
|
274
275
|
summary: A collection of refinements to core Ruby objects.
|
275
276
|
test_files: []
|
metadata.gz.sig
CHANGED
Binary file
|
data/README.md
DELETED
@@ -1,263 +0,0 @@
|
|
1
|
-
<p align="center">
|
2
|
-
<img src="refinements.png" alt="Refinements Icon"/>
|
3
|
-
</p>
|
4
|
-
|
5
|
-
# Refinements
|
6
|
-
|
7
|
-
[![Gem Version](https://badge.fury.io/rb/refinements.svg)](http://badge.fury.io/rb/refinements)
|
8
|
-
[![Code Climate Maintainability](https://api.codeclimate.com/v1/badges/98be62123a95d5ac32d2/maintainability)](https://codeclimate.com/github/bkuhlmann/refinements/maintainability)
|
9
|
-
[![Code Climate Test Coverage](https://api.codeclimate.com/v1/badges/98be62123a95d5ac32d2/test_coverage)](https://codeclimate.com/github/bkuhlmann/refinements/test_coverage)
|
10
|
-
[![Circle CI Status](https://circleci.com/gh/bkuhlmann/refinements.svg?style=svg)](https://circleci.com/gh/bkuhlmann/refinements)
|
11
|
-
|
12
|
-
A collection of refinements (enhancements) to core Ruby objects.
|
13
|
-
|
14
|
-
<!-- Tocer[start]: Auto-generated, don't remove. -->
|
15
|
-
|
16
|
-
## Table of Contents
|
17
|
-
|
18
|
-
- [Features](#features)
|
19
|
-
- [Requirements](#requirements)
|
20
|
-
- [Setup](#setup)
|
21
|
-
- [Usage](#usage)
|
22
|
-
- [Requires](#requires)
|
23
|
-
- [Using](#using)
|
24
|
-
- [Examples](#examples)
|
25
|
-
- [Array](#array)
|
26
|
-
- [DateTime](#datetime)
|
27
|
-
- [Big Decimal](#big-decimal)
|
28
|
-
- [File](#file)
|
29
|
-
- [Hash](#hash)
|
30
|
-
- [Pathname](#pathname)
|
31
|
-
- [String](#string)
|
32
|
-
- [Tests](#tests)
|
33
|
-
- [Versioning](#versioning)
|
34
|
-
- [Code of Conduct](#code-of-conduct)
|
35
|
-
- [Contributions](#contributions)
|
36
|
-
- [License](#license)
|
37
|
-
- [History](#history)
|
38
|
-
- [Credits](#credits)
|
39
|
-
|
40
|
-
<!-- Tocer[finish]: Auto-generated, don't remove. -->
|
41
|
-
|
42
|
-
## Features
|
43
|
-
|
44
|
-
- Provides Array refinements:
|
45
|
-
- `#compress` - Removes `nil` and empty values without modifying itself.
|
46
|
-
- `#compress!` - Removes `nil` and empty values while modifying itself.
|
47
|
-
- Provides DateTime refinements:
|
48
|
-
- `.utc` - Answers new DateTime object for current UTC date/time.
|
49
|
-
- Provides BigDecimal refinements:
|
50
|
-
- `#inspect` - Allows one to inspect a big decimal with numeric representation.
|
51
|
-
- Provides File refinements:
|
52
|
-
- `#name` - Answers the *name* of file without extension.
|
53
|
-
- `#copy` - Copies an existing file to new file. Both directory structures must exist.
|
54
|
-
- `#rewrite` - When given a file path and a block, it provides the contents of the recently read
|
55
|
-
file for manipulation and immediate writing back to the same file.
|
56
|
-
- `#touch` - Updates the access and modified times of an existing file or creates the file when
|
57
|
-
not existing.
|
58
|
-
- Provides Hash refinements:
|
59
|
-
- `#except` - Answers new hash with given keys removed without modifying calling hash.
|
60
|
-
- `#except!` - Answers new hash with given keys removed while modifying calling hash.
|
61
|
-
- `#symbolize_keys` - Converts keys to symbols without modifying itself.
|
62
|
-
- `#symbolize_keys!` - Converts keys to symbols while modifying itself.
|
63
|
-
- `#deep_merge` - Merges deeply nested hashes together without modifying itself.
|
64
|
-
- `#deep_merge!` - Merges deeply nested hashes together while modifying itself.
|
65
|
-
- `#reverse_merge` - Merges calling hash into passed in hash without modifying calling hash.
|
66
|
-
- `#reverse_merge!` - Merges calling hash into passed in hash while modifying calling hash.
|
67
|
-
- Provides Pathname refinements:
|
68
|
-
- `#rewrite` - When given a block, it provides the contents of the recently read file for
|
69
|
-
manipulation and immediate writing back to the same file.
|
70
|
-
- Provides String refinements:
|
71
|
-
- `#first` - Answers first character of a string or first set of characters if given a number.
|
72
|
-
- `#last` - Answers last character of a string or last set of characters if given a number.
|
73
|
-
- `#blank?` - Answers `true`/`false` based on whether string is blank or not (i.e. `<space>`,
|
74
|
-
`\n`, `\t`, `\r`).
|
75
|
-
- `#up` - Answers string with only first letter upcased.
|
76
|
-
- `#down` - Answers string with only first letter downcased.
|
77
|
-
- `#camelcase` - Answers a camelcased string. Example: "ThisIsCamelcase".
|
78
|
-
- `#snakecase` - Answers a snakecased string. Example: "this_is_snakecase".
|
79
|
-
- `#titleize` - Answers titleized string. Example: "This Is Titleized".
|
80
|
-
- `#use` - Provides hash value computation, via a block, by using
|
81
|
-
only the keys as arguments to the block.
|
82
|
-
|
83
|
-
## Requirements
|
84
|
-
|
85
|
-
1. [Ruby 2.7.x](https://www.ruby-lang.org).
|
86
|
-
1. A solid understanding of [Ruby refinements and lexical scope](https://www.youtube.com/watch?v=qXC9Gk4dCEw).
|
87
|
-
|
88
|
-
## Setup
|
89
|
-
|
90
|
-
Type the following to install:
|
91
|
-
|
92
|
-
gem install refinements
|
93
|
-
|
94
|
-
Add the following to your Gemfile file:
|
95
|
-
|
96
|
-
gem "refinements"
|
97
|
-
|
98
|
-
## Usage
|
99
|
-
|
100
|
-
### Requires
|
101
|
-
|
102
|
-
If all refinements are not desired, add the following to your `Gemfile` instead:
|
103
|
-
|
104
|
-
gem "refinements", require: false
|
105
|
-
|
106
|
-
...then require the specific refinement, as needed. Example:
|
107
|
-
|
108
|
-
require "refinements/arrays"
|
109
|
-
require "refinements/date_times"
|
110
|
-
require "refinements/big_decimals"
|
111
|
-
require "refinements/files"
|
112
|
-
require "refinements/hashes"
|
113
|
-
require "refinements/pathnames"
|
114
|
-
require "refinements/strings"
|
115
|
-
|
116
|
-
### Using
|
117
|
-
|
118
|
-
Much like including/extending a module, you'll need modify your object(s) to use the refinement(s):
|
119
|
-
|
120
|
-
class Example
|
121
|
-
using Refinements::Arrays
|
122
|
-
using Refinements::DateTimes
|
123
|
-
using Refinements::BigDecimals
|
124
|
-
using Refinements::Files
|
125
|
-
using Refinements::Hashes
|
126
|
-
using Refinements::Pathnames
|
127
|
-
using Refinements::Strings
|
128
|
-
end
|
129
|
-
|
130
|
-
### Examples
|
131
|
-
|
132
|
-
The following sections demonstrate how each refinement enriches your objects with new capabilities.
|
133
|
-
|
134
|
-
#### Array
|
135
|
-
|
136
|
-
example = ["An", nil, "", "Example"]
|
137
|
-
example.compress # => ["An", "Example"]
|
138
|
-
example # => ["An", nil, "", "Example"]
|
139
|
-
|
140
|
-
example = ["An", nil, "", "Example"]
|
141
|
-
example.compress! # => ["An", "Example"]
|
142
|
-
example # => ["An", "Example"]
|
143
|
-
|
144
|
-
#### DateTime
|
145
|
-
|
146
|
-
DateTime.utc # => #<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>
|
147
|
-
|
148
|
-
#### Big Decimal
|
149
|
-
|
150
|
-
BigDecimal.new("5.0E-10").inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"
|
151
|
-
|
152
|
-
#### File
|
153
|
-
|
154
|
-
File.rewrite("/test.txt") { |content| content.gsub "[placeholder]", "example" }
|
155
|
-
|
156
|
-
#### Hash
|
157
|
-
|
158
|
-
example = {a: 1, b: 2, c: 3}
|
159
|
-
example.except :a, :b # => {c: 3}
|
160
|
-
example # => {a: 1, b: 2, c: 3}
|
161
|
-
|
162
|
-
example = {a: 1, b: 2, c: 3}
|
163
|
-
example.except! :a, :b # => {c: 3}
|
164
|
-
example # => {c: 3}
|
165
|
-
|
166
|
-
example = {"a" => 1, "b" => 2}
|
167
|
-
example.symbolize_keys # => {a: 1, b: 2}
|
168
|
-
example # => {"a" => 1, "b" => 2}
|
169
|
-
|
170
|
-
example = {"a" => 1, "b" => 2}
|
171
|
-
example.symbolize_keys! # => {a: 1, b: 2}
|
172
|
-
example # => {a: 1, b: 2}
|
173
|
-
|
174
|
-
example = {a: 1, b: 2, c: 3}
|
175
|
-
example.slice :a, :c # => {a: 1, c: 3}
|
176
|
-
example # => {a: 1, b: 2, c: 3}
|
177
|
-
|
178
|
-
example = {a: 1, b: 2, c: 3}
|
179
|
-
example.slice! :a, :c # => {a: 1, c: 3}
|
180
|
-
example # => {a: 1, c: 3}
|
181
|
-
|
182
|
-
example = {a: "A", b: {one: "One", two: "Two"}}
|
183
|
-
example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
|
184
|
-
example # => {a: "A", b: {one: "One", two: "Two"}}
|
185
|
-
|
186
|
-
example = {a: "A", b: {one: "One", two: "Two"}}
|
187
|
-
example.deep_merge! b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
|
188
|
-
example # => {a: "A", b: {one: 1, two: "Two"}}
|
189
|
-
|
190
|
-
example = {a: 1, b: 2}
|
191
|
-
example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
|
192
|
-
example # => {a: 1, b: 2}
|
193
|
-
|
194
|
-
example = {a: 1, b: 2}
|
195
|
-
example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
|
196
|
-
example # => {a: 1, b: 2, c: 3}
|
197
|
-
|
198
|
-
example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
|
199
|
-
example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"
|
200
|
-
|
201
|
-
#### Pathname
|
202
|
-
|
203
|
-
Pathname("test.txt").name # => Pathname("test")
|
204
|
-
|
205
|
-
Pathname("input.txt").copy Pathname("output.txt")
|
206
|
-
|
207
|
-
Pathname("/test.txt").rewrite { |content| content.sub "[placeholder]", "example" }
|
208
|
-
|
209
|
-
Pathname("test.txt").touch
|
210
|
-
Pathname("test.txt").touch accessed_at: Time.now - 1, modified_at: Time.now - 1
|
211
|
-
|
212
|
-
#### String
|
213
|
-
|
214
|
-
"example".first # => "e"
|
215
|
-
"example".first 4 # => "exam"
|
216
|
-
|
217
|
-
"instant".last # => "t"
|
218
|
-
"instant".last 3 # => "ant"
|
219
|
-
|
220
|
-
" \n\t\r".blank? # => true
|
221
|
-
"example".up # => "Example"
|
222
|
-
"EXAMPLE".down # => "eXAMPLE"
|
223
|
-
"this_is_an_example".camelcase # => "ThisIsAnExample"
|
224
|
-
"ThisIsAnExample".snakecase # => "this_is_an_example"
|
225
|
-
"ThisIsAnExample".titleize # => "This Is An Example"
|
226
|
-
|
227
|
-
## Tests
|
228
|
-
|
229
|
-
To test, run:
|
230
|
-
|
231
|
-
bundle exec rake
|
232
|
-
|
233
|
-
## Versioning
|
234
|
-
|
235
|
-
Read [Semantic Versioning](https://semver.org) for details. Briefly, it means:
|
236
|
-
|
237
|
-
- Major (X.y.z) - Incremented for any backwards incompatible public API changes.
|
238
|
-
- Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
|
239
|
-
- Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
|
240
|
-
|
241
|
-
## Code of Conduct
|
242
|
-
|
243
|
-
Please note that this project is released with a [CODE OF CONDUCT](CODE_OF_CONDUCT.md). By
|
244
|
-
participating in this project you agree to abide by its terms.
|
245
|
-
|
246
|
-
## Contributions
|
247
|
-
|
248
|
-
Read [CONTRIBUTING](CONTRIBUTING.md) for details.
|
249
|
-
|
250
|
-
## License
|
251
|
-
|
252
|
-
Copyright 2015 [Alchemists](https://www.alchemists.io).
|
253
|
-
Read [LICENSE](LICENSE.md) for details.
|
254
|
-
|
255
|
-
## History
|
256
|
-
|
257
|
-
Read [CHANGES](CHANGES.md) for details.
|
258
|
-
Built with [Gemsmith](https://github.com/bkuhlmann/gemsmith).
|
259
|
-
|
260
|
-
## Credits
|
261
|
-
|
262
|
-
Developed by [Brooke Kuhlmann](https://www.alchemists.io) at
|
263
|
-
[Alchemists](https://www.alchemists.io).
|