refinements 7.0.0 → 7.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff378c2c0076828fcd3e3ad623de04d09cf9c0f55bd12a17912504fad71bdf28
4
- data.tar.gz: 9280e888aad9dcebe9872734501cc6d5a8ce46af9baa7e3fb9bf3baca0e49518
3
+ metadata.gz: 0ccd018fc5d5ae2bfa0b6fc1b9f090bc3638ac4053ea5790d7826e2c291bc378
4
+ data.tar.gz: 43cd2a8603f9e8667eab9c18aa24ccf5b1f7860054d88b7bc39e22df1bfad573
5
5
  SHA512:
6
- metadata.gz: 0c2ba552ed6a3779b3eb220f6d39b348baf83e2bab906aca907c483209f52f98554d9baeed1c5cbeeba939be205779ed20e1c625671672d4e143451ea36210d0
7
- data.tar.gz: e7a91844e7df2654a1b20257e036d634d96bd867291bf5687e0ead5e07cdf307cd2e1347a917304119520813baddaca8f740589674545f1b3ac24acecc1e910d
6
+ metadata.gz: b5dd7fb29e0de7fe1e51209d427889331d8179c5e92868167e818e25dd792509d4221cdf07448a86bea13820cb13ccf0c680cb4b459c7c57ebb0dc0017255aba
7
+ data.tar.gz: 94a069d63f4c565fd0c189d489887d23e7420bb63b8a9ec3cb5defc3626a87158037eb7cefdafdaa1b43e5b66764fda59dcf9007279cdcc3d8d53eadbf078bb7
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,4 +1,4 @@
1
- # Apache License
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
- ## 1. Definitions
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
- ## 2. Grant of Copyright License
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
- ## 3. Grant of Patent License
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
- ## 4. Redistribution
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
- 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
83
+ . You must give any other recipients of the Work or Derivative Works a copy of this License; and
84
84
 
85
- 2. You must cause any modified files to carry prominent notices stating that You changed the
86
- files; and
85
+ . You must cause any modified files to carry prominent notices stating that You changed the files;
86
+ and
87
87
 
88
- 3. 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
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
- 4. If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative
93
- Works that You distribute must include a readable copy of the attribution notices contained
94
- within such NOTICE file, excluding those notices that do not pertain to any part of the
95
- Derivative Works, in at least one of the following places: within a NOTICE text file
96
- distributed as part of the Derivative Works; within the Source form or documentation, if
97
- provided along with the Derivative Works; or, within a display generated by the Derivative
98
- Works, if and wherever such third-party notices normally appear. The contents of the NOTICE
99
- file are for informational purposes only and do not modify the License. You may add Your own
100
- attribution notices within Derivative Works that You distribute, alongside or as an addendum to
101
- the NOTICE text from the Work, provided that such additional attribution notices cannot be
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
- ## 5. Submission of Contributions
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
- ## 6. Trademarks
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
- ## 7. Disclaimer of Warranty
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
- ## 8. Limitation of Liability
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
- ## 9. Accepting Warranty or Additional Liability
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 2015 [Alchemists](https://www.alchemists.io).
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 [copy](http://www.apache.org/licenses/LICENSE-2.0) of
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
@@ -0,0 +1,362 @@
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
+ ** `#except` - Answers new hash with given keys removed without modifying itself.
31
+ ** `#except!` - Answers new hash with given keys removed while modifying itself.
32
+ ** `#symbolize_keys` - Converts keys to symbols without modifying itself.
33
+ ** `#symbolize_keys!` - Converts keys to symbols while modifying itself.
34
+ ** `#deep_merge` - Merges deeply nested hashes together without modifying itself.
35
+ ** `#deep_merge!` - Merges deeply nested hashes together while modifying itself.
36
+ ** `#deep_symbolize_keys` - Symbolizes keys of nested hash without modifying itself. Does not handle
37
+ nested arrays, though.
38
+ ** `#deep_symbolize_keys!` - Symbolizes keys of nested hash while modifying itself. Does not handle
39
+ nested arrays, though.
40
+ ** `#recurse` - Applies block to nested hash. Does not handle nested arrays, though.
41
+ ** `#rekey` - Transforms keys per mapping (size of mapping can vary) without modifying itself.
42
+ ** `#rekey!` - Transforms keys per mapping (size of mapping can vary) while modifying itself.
43
+ ** `#reverse_merge` - Merges calling hash into passed in hash without modifying itself.
44
+ ** `#reverse_merge!` - Merges calling hash into passed in hash while modifying itself.
45
+ ** `#use` - Passes each hash value as a block argument for further processing.
46
+ * *Pathnames*:
47
+ ** `Pathname` - Conversion function (refined from `Kernel`) which can cast `nil` into a pathname.
48
+ ** `#name` - Answers file name without extension.
49
+ ** `#copy` - Copies file from current location to new location.
50
+ ** `#directories` - Answers all or filtered directories for current path.
51
+ ** `#extensions` - Answers file extensions as an array.
52
+ ** `#files` - Answers all or filtered files for current path.
53
+ ** `#relative_parent_from` - Answers relative path from parent directory. This is a complement to
54
+ `#relative_path_from`.
55
+ ** `#make_ancestors` - Ensures all ancestor directories are created for a path.
56
+ ** `#rewrite` - When given a block, it provides the contents of the recently read file for
57
+ manipulation and immediate writing back to the same file.
58
+ ** `#touch` - Updates access and modification times for path. Defaults to current time.
59
+ * *Strings*:
60
+ ** `#first` - Answers first character of a string or first set of characters if given a number.
61
+ ** `#last` - Answers last character of a string or last set of characters if given a number.
62
+ ** `#blank?` - Answers `true`/`false` based on whether string is blank or not
63
+ (i.e. `<space>`, `\n`, `\t`, `\r`).
64
+ ** `#up` - Answers string with only first letter upcased.
65
+ ** `#down` - Answers string with only first letter downcased.
66
+ ** `#camelcase` - Answers a camelcased string.
67
+ ** `#snakecase` - Answers a snakecased string.
68
+ ** `#titleize` - Answers titleized string.
69
+ ** `#to_bool` - Answers string as a boolean.
70
+
71
+ == Requirements
72
+
73
+ . https://www.ruby-lang.org[Ruby 2.7.x].
74
+ . A solid understanding of link:https://www.alchemists.io/articles/ruby_refinements[Ruby refinements
75
+ and lexical scope].
76
+
77
+ == Setup
78
+
79
+ === Production
80
+
81
+ To install, run:
82
+
83
+ [source,bash]
84
+ ----
85
+ gem install refinements
86
+ ----
87
+
88
+ Add the following to your Gemfile file:
89
+
90
+ [source,ruby]
91
+ ----
92
+ gem "refinements"
93
+ ----
94
+
95
+ === Development
96
+
97
+ To contribute, run:
98
+
99
+ [source,bash]
100
+ ----
101
+ git clone https://github.com/bkuhlmann/refinements.git
102
+ cd refinements
103
+ bin/setup
104
+ ----
105
+
106
+ You can also use the IRB console for direct access to all objects:
107
+
108
+ [source,bash]
109
+ ----
110
+ bin/console
111
+ ----
112
+
113
+ == Usage
114
+
115
+ === Requires
116
+
117
+ If all refinements are not desired, add the following to your `+Gemfile+` instead:
118
+
119
+ [source,ruby]
120
+ ----
121
+ gem "refinements", require: false
122
+ ----
123
+
124
+ …then require the specific refinement, as needed. Example:
125
+
126
+ [source,ruby]
127
+ ----
128
+ require "refinements/arrays"
129
+ require "refinements/big_decimals"
130
+ require "refinements/date_times"
131
+ require "refinements/files"
132
+ require "refinements/hashes"
133
+ require "refinements/pathnames"
134
+ require "refinements/strings"
135
+ ----
136
+
137
+ === Using
138
+
139
+ Much like including/extending a module, you’ll need modify your object(s) to use the refinement(s):
140
+
141
+ [source,ruby]
142
+ ----
143
+ class Example
144
+ using Refinements::Arrays
145
+ using Refinements::BigDecimals
146
+ using Refinements::DateTimes
147
+ using Refinements::Files
148
+ using Refinements::Hashes
149
+ using Refinements::Pathnames
150
+ using Refinements::Strings
151
+ end
152
+ ----
153
+
154
+ === Examples
155
+
156
+ The following sections demonstrate how each refinement enriches your objects with new capabilities.
157
+
158
+ ==== Array
159
+
160
+ [source,ruby]
161
+ ----
162
+ example = ["An", nil, "", "Example"]
163
+ example.compress # => ["An", "Example"]
164
+ example # => ["An", nil, "", "Example"]
165
+
166
+ example = ["An", nil, "", "Example"]
167
+ example.compress! # => ["An", "Example"]
168
+ example # => ["An", "Example"]
169
+
170
+ example = [1, 2, 3]
171
+ example.ring # => #<Enumerator: ...>
172
+ example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
173
+ # [3 1 2]
174
+ # [1 2 3]
175
+ # [2 3 1]
176
+ ----
177
+
178
+ ==== Big Decimal
179
+
180
+ [source,ruby]
181
+ ----
182
+ BigDecimal.new("5.0E-10").inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"
183
+ ----
184
+
185
+ ==== DateTime
186
+
187
+ [source,ruby]
188
+ ----
189
+ DateTime.utc # => #<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>
190
+ ----
191
+
192
+ ==== File
193
+
194
+ [source,ruby]
195
+ ----
196
+ File.rewrite("/test.txt") { |content| content.gsub "[placeholder]", "example" }
197
+ ----
198
+
199
+ ==== Hash
200
+
201
+ [source,ruby]
202
+ ----
203
+ example = {a: 1, b: 2, c: 3}
204
+ example.except :a, :b # => {c: 3}
205
+ example # => {a: 1, b: 2, c: 3}
206
+
207
+ example = {a: 1, b: 2, c: 3}
208
+ example.except! :a, :b # => {c: 3}
209
+ example # => {c: 3}
210
+
211
+ example = {"a" => 1, "b" => 2}
212
+ example.symbolize_keys # => {a: 1, b: 2}
213
+ example # => {"a" => 1, "b" => 2}
214
+
215
+ example = {"a" => 1, "b" => 2}
216
+ example.symbolize_keys! # => {a: 1, b: 2}
217
+ example # => {a: 1, b: 2}
218
+
219
+ example = {a: 1, b: 2, c: 3}
220
+ example.slice :a, :c # => {a: 1, c: 3}
221
+ example # => {a: 1, b: 2, c: 3}
222
+
223
+ example = {a: 1, b: 2, c: 3}
224
+ example.slice! :a, :c # => {a: 1, c: 3}
225
+ example # => {a: 1, c: 3}
226
+
227
+ example = {a: "A", b: {one: "One", two: "Two"}}
228
+ example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
229
+ example # => {a: "A", b: {one: "One", two: "Two"}}
230
+
231
+ example = {a: "A", b: {one: "One", two: "Two"}}
232
+ example.deep_merge! b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
233
+ example # => {a: "A", b: {one: 1, two: "Two"}}
234
+
235
+ example = {"a" => {"b" => 2}}
236
+ example.deep_symbolize_keys # => {a: {b: 1}}
237
+ example # => {"a" => {"b" => 2}}
238
+
239
+ example = {"a" => {"b" => 2}}
240
+ example.deep_symbolize_keys! # => {a: {b: 1}}
241
+ example # => {a: {b: 1}}
242
+
243
+ example = {"a" => {"b" => 1}}
244
+ example.recurse(&:symbolize_keys) # => {a: {b: 1}}
245
+ example.recurse(&:invert) # => {{"b" => 1} => "a"}
246
+
247
+ example = {a: 1, b: 2, c: 3}
248
+ example.rekey a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
249
+ example # => {a: 1, b: 2, c: 3}
250
+
251
+ example = {a: 1, b: 2, c: 3}
252
+ example.rekey! a: :amber, b: :blue # => {amber: 1, blue: 2, c: 3}
253
+ example # => {amber: 1, blue: 2, c: 3}
254
+
255
+ example = {a: 1, b: 2}
256
+ example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
257
+ example # => {a: 1, b: 2}
258
+
259
+ example = {a: 1, b: 2}
260
+ example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
261
+ example # => {a: 1, b: 2, c: 3}
262
+
263
+ example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
264
+ example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"
265
+ ----
266
+
267
+ ==== Pathname
268
+
269
+ [source,ruby]
270
+ ----
271
+ Pathname(nil) # => Pathname("")
272
+
273
+ Pathname("example.txt").name # => Pathname("example")
274
+
275
+ Pathname("input.txt").copy Pathname("output.txt")
276
+
277
+ Pathname("/example").directories # => [Pathname("a"), Pathname("b")]
278
+ Pathname("/example").directories "a*" # => [Pathname("a")]
279
+
280
+ Pathname("example.txt.erb").extensions # => [".txt", ".erb"]
281
+
282
+ Pathname("/example").files # => [Pathname("a.txt"), Pathname("a.png")]
283
+ Pathname("/example").files "*.png" # => [Pathname("a.png")]
284
+
285
+ Pathname("/one/two/three").relative_parent_from("/one") # => Pathname "two"
286
+
287
+ Pathname("/one/two").make_ancestors
288
+ Pathname("/one").exist? # => true
289
+ Pathname("/one/two").exist? # => false
290
+
291
+ Pathname("/test.txt").rewrite { |content| content.sub "[placeholder]", "example" }
292
+
293
+ Pathname("example.txt").touch
294
+ Pathname("example.txt").touch at: Time.now - 1
295
+ ----
296
+
297
+ ==== String
298
+
299
+ [source,ruby]
300
+ ----
301
+ "example".first # => "e"
302
+ "example".first 4 # => "exam"
303
+
304
+ "instant".last # => "t"
305
+ "instant".last 3 # => "ant"
306
+
307
+ " \n\t\r".blank? # => true
308
+
309
+ "example".up # => "Example"
310
+
311
+ "EXAMPLE".down # => "eXAMPLE"
312
+
313
+ "this_is_an_example".camelcase # => "ThisIsAnExample"
314
+
315
+ "ThisIsAnExample".snakecase # => "this_is_an_example"
316
+
317
+ "ThisIsAnExample".titleize # => "This Is An Example"
318
+
319
+ "true".to_bool # => true
320
+ "yes".to_bool # => true
321
+ "1".to_bool # => true
322
+ "".to_bool # => false
323
+ "example".to_bool # => false
324
+ ----
325
+
326
+ == Tests
327
+
328
+ To test, run:
329
+
330
+ [source,bash]
331
+ ----
332
+ bundle exec rake
333
+ ----
334
+
335
+ == Versioning
336
+
337
+ Read link:https://semver.org[Semantic Versioning] for details. Briefly, it means:
338
+
339
+ * Major (X.y.z) - Incremented for any backwards incompatible public API changes.
340
+ * Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
341
+ * Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
342
+
343
+ == Code of Conduct
344
+
345
+ Please note that this project is released with a link:CODE_OF_CONDUCT.adoc[CODE OF CONDUCT]. By
346
+ participating in this project you agree to abide by its terms.
347
+
348
+ == Contributions
349
+
350
+ Read link:CONTRIBUTING.adoc[CONTRIBUTING] for details.
351
+
352
+ == License
353
+
354
+ Read link:LICENSE.adoc[LICENSE] for details.
355
+
356
+ == History
357
+
358
+ Read link:CHANGES.adoc[CHANGES] for details.
359
+
360
+ == Credits
361
+
362
+ Engineered by link:https://www.alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
@@ -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"
@@ -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
@@ -3,7 +3,6 @@
3
3
  require "bigdecimal"
4
4
 
5
5
  module Refinements
6
- # Refinements for big decimals.
7
6
  module BigDecimals
8
7
  refine BigDecimal do
9
8
  def inspect
@@ -4,6 +4,7 @@ module Refinements
4
4
  module Files
5
5
  refine File.singleton_class do
6
6
  def rewrite path
7
+ warn "[DEPRECATION]: File.rewrite is deprecated, use Pathname#rewrite instead."
7
8
  read(path).then { |content| write path, yield(content) }
8
9
  end
9
10
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
- # Refinements for hashes.
5
4
  module Hashes
6
5
  refine Hash do
7
6
  def except *keys
@@ -13,22 +12,22 @@ module Refinements
13
12
  end
14
13
 
15
14
  def symbolize_keys
16
- dup.symbolize_keys!
15
+ reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
17
16
  end
18
17
 
19
18
  def symbolize_keys!
20
- keys.each { |key| self[key.to_sym] = delete key }
21
- self
19
+ replace symbolize_keys
22
20
  end
23
21
 
24
22
  def deep_merge other
25
23
  dup.deep_merge! other
26
24
  end
27
25
 
28
- # :reek:FeatureEnvy
29
26
  def deep_merge! other
27
+ clazz = self.class
28
+
30
29
  merge! other do |_key, this_value, other_value|
31
- if this_value.is_a?(Hash) && other_value.is_a?(Hash)
30
+ if this_value.is_a?(clazz) && other_value.is_a?(clazz)
32
31
  this_value.deep_merge other_value
33
32
  else
34
33
  other_value
@@ -36,11 +35,41 @@ module Refinements
36
35
  end
37
36
  end
38
37
 
38
+ def deep_symbolize_keys
39
+ recurse(&:symbolize_keys)
40
+ end
41
+
42
+ def deep_symbolize_keys!
43
+ recurse(&:symbolize_keys!)
44
+ end
45
+
46
+ def recurse &block
47
+ return self unless block_given?
48
+
49
+ transform = yield self
50
+
51
+ transform.each do |key, value|
52
+ transform[key] = value.recurse(&block) if value.is_a? self.class
53
+ end
54
+ end
55
+
56
+ def rekey mapping = {}
57
+ return self if mapping.empty?
58
+
59
+ transform_keys { |key| mapping[key] || key }
60
+ end
61
+
62
+ def rekey! mapping = {}
63
+ replace rekey(mapping)
64
+ end
65
+
39
66
  def reverse_merge other
67
+ warn "[DEPRECATION]: #reverse_merge is deprecated, use #merge instead."
40
68
  other.merge self
41
69
  end
42
70
 
43
71
  def reverse_merge! other
72
+ warn "[DEPRECATION]: #reverse_merge! is deprecated, use #merge! instead."
44
73
  merge!(other) { |_key, old_value, _new_value| old_value }
45
74
  end
46
75
 
@@ -3,20 +3,9 @@
3
3
  module Refinements
4
4
  # Gem identity information.
5
5
  module Identity
6
- def self.name
7
- "refinements"
8
- end
9
-
10
- def self.label
11
- "Refinements"
12
- end
13
-
14
- def self.version
15
- "7.0.0"
16
- end
17
-
18
- def self.version_label
19
- "#{label} #{version}"
20
- end
6
+ NAME = "refinements"
7
+ LABEL = "Refinements"
8
+ VERSION = "7.5.0"
9
+ VERSION_LABEL = "#{LABEL} #{VERSION}"
21
10
  end
22
11
  end
@@ -4,9 +4,54 @@ 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
16
+ def name
17
+ basename extname
18
+ end
19
+
20
+ def copy to
21
+ destination = to.directory? ? to.join(basename) : to
22
+ read.then { |content| destination.write content }
23
+ self
24
+ end
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
+
8
47
  def rewrite
9
- read.then { |content| write yield(content) }
48
+ read.then { |content| write yield(content) if block_given? }
49
+ self
50
+ end
51
+
52
+ def touch at: Time.now
53
+ exist? ? utime(at, at) : write("")
54
+ self
10
55
  end
11
56
  end
12
57
  end
@@ -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*\-\s*|\s*\/\s*|\s*\:+\s*|\s*\_\s*|\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[0..(max - 1)]
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[max] if min.zero?
28
+ return self[size - 1] if min.zero?
32
29
  return "" if min.negative?
33
30
 
34
- self[(min + 1)..max]
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
- if match? self.class.delimiters
55
- split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)).then { |parts| combine parts, :up, "::" }
56
- .then { |text| text.split(/\s*\_\s*|\s+/) }
57
- .then { |parts| combine parts, :up }
58
- else
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
- if match? self.class.delimiters
65
- split(%r(\s*\-\s*|\s*\/\s*|\s*\:+\s*)).then { |parts| combine parts, :down, "/" }
66
- .then { |txt| txt.split(/(?=[A-Z])|\s*\_\s*|\s+/) }
67
- .then { |parts| combine parts, :down, "_" }
68
- else
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
- if match? self.class.delimiters
75
- split(/(?=[A-Z])|\s*\_\s*|\s*\-\s*|\s+/).then { |parts| combine parts, :up, " " }
76
- .then { |text| text.split %r(\s*\/\s*|\s*\:+\s*) }
77
- .then { |parts| combine parts, :up, "/" }
78
- else
79
- capitalize
80
- end
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.0.0
4
+ version: 7.5.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/jCCAeagAwIBAgIBAjANBgkqhkiG9w0BAQsFADAlMSMwIQYDVQQDDBpicm9v
14
- a2UvREM9YWxjaGVtaXN0cy9EQz1pbzAeFw0xOTAyMTcxNjAxMTFaFw0yMDAyMTcx
15
- NjAxMTFaMCUxIzAhBgNVBAMMGmJyb29rZS9EQz1hbGNoZW1pc3RzL0RDPWlvMIIB
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/gY4ggwDQYJKoZIhvcNAQELBQADggEBANgfX7kPZkJvsbdWMvbe
24
- DUUYxRTqTUzCDpLd2p3sQvgLMKjTNSKt6CFq/kh+98b6RCgO6DqiVfxvOEq02dvf
25
- ebMN/ylQqan2FgiAs1rpP0/D4C0MlBjoDktsT+2IyLol1KmkDB20ExJ4Sf1GRNdA
26
- hpWOtcgqHtmrc7xzgB6BuyoYufrKKUlCO3ewTM4C375D2qO7hIdKuMOO3CdPMtSk
27
- OrWpbzCL+nbbXqeoqiHgZaUvcyyQrGj5sQRxMTzuMXzzQxtHnuTuKrcz16HMTX/O
28
- dKvURM+1PwDCzC5tvRwjhUJIizau6+MtkFCvJHmaAj1aZL3odcPejHj5Hxt/0CUW
29
- y84=
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-01-01 00:00:00.000000000 Z
31
+ date: 2020-06-07 00:00:00.000000000 Z
32
32
  dependencies:
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler-audit
@@ -44,6 +44,34 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0.6'
47
+ - !ruby/object:Gem::Dependency
48
+ name: gemsmith
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '14.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '14.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: git-cop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '4.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '4.0'
47
75
  - !ruby/object:Gem::Dependency
48
76
  name: guard-rspec
49
77
  requirement: !ruby/object:Gem::Requirement
@@ -64,28 +92,28 @@ dependencies:
64
92
  requirements:
65
93
  - - "~>"
66
94
  - !ruby/object:Gem::Version
67
- version: '0.12'
95
+ version: '0.13'
68
96
  type: :development
69
97
  prerelease: false
70
98
  version_requirements: !ruby/object:Gem::Requirement
71
99
  requirements:
72
100
  - - "~>"
73
101
  - !ruby/object:Gem::Version
74
- version: '0.12'
102
+ version: '0.13'
75
103
  - !ruby/object:Gem::Dependency
76
104
  name: pry-byebug
77
105
  requirement: !ruby/object:Gem::Requirement
78
106
  requirements:
79
107
  - - "~>"
80
108
  - !ruby/object:Gem::Version
81
- version: '3.7'
109
+ version: '3.9'
82
110
  type: :development
83
111
  prerelease: false
84
112
  version_requirements: !ruby/object:Gem::Requirement
85
113
  requirements:
86
114
  - - "~>"
87
115
  - !ruby/object:Gem::Version
88
- version: '3.7'
116
+ version: '3.9'
89
117
  - !ruby/object:Gem::Dependency
90
118
  name: rake
91
119
  requirement: !ruby/object:Gem::Requirement
@@ -106,14 +134,14 @@ dependencies:
106
134
  requirements:
107
135
  - - "~>"
108
136
  - !ruby/object:Gem::Version
109
- version: '5.4'
137
+ version: '6.0'
110
138
  type: :development
111
139
  prerelease: false
112
140
  version_requirements: !ruby/object:Gem::Requirement
113
141
  requirements:
114
142
  - - "~>"
115
143
  - !ruby/object:Gem::Version
116
- version: '5.4'
144
+ version: '6.0'
117
145
  - !ruby/object:Gem::Dependency
118
146
  name: rspec
119
147
  requirement: !ruby/object:Gem::Requirement
@@ -134,14 +162,14 @@ dependencies:
134
162
  requirements:
135
163
  - - "~>"
136
164
  - !ruby/object:Gem::Version
137
- version: '0.78'
165
+ version: '0.83'
138
166
  type: :development
139
167
  prerelease: false
140
168
  version_requirements: !ruby/object:Gem::Requirement
141
169
  requirements:
142
170
  - - "~>"
143
171
  - !ruby/object:Gem::Version
144
- version: '0.78'
172
+ version: '0.83'
145
173
  - !ruby/object:Gem::Dependency
146
174
  name: rubocop-performance
147
175
  requirement: !ruby/object:Gem::Requirement
@@ -176,39 +204,39 @@ dependencies:
176
204
  requirements:
177
205
  - - "~>"
178
206
  - !ruby/object:Gem::Version
179
- version: '1.37'
207
+ version: '1.39'
180
208
  type: :development
181
209
  prerelease: false
182
210
  version_requirements: !ruby/object:Gem::Requirement
183
211
  requirements:
184
212
  - - "~>"
185
213
  - !ruby/object:Gem::Version
186
- version: '1.37'
214
+ version: '1.39'
187
215
  - !ruby/object:Gem::Dependency
188
216
  name: simplecov
189
217
  requirement: !ruby/object:Gem::Requirement
190
218
  requirements:
191
219
  - - "~>"
192
220
  - !ruby/object:Gem::Version
193
- version: '0.17'
221
+ version: '0.18'
194
222
  type: :development
195
223
  prerelease: false
196
224
  version_requirements: !ruby/object:Gem::Requirement
197
225
  requirements:
198
226
  - - "~>"
199
227
  - !ruby/object:Gem::Version
200
- version: '0.17'
201
- description:
228
+ version: '0.18'
229
+ description:
202
230
  email:
203
231
  - brooke@alchemists.io
204
232
  executables: []
205
233
  extensions: []
206
234
  extra_rdoc_files:
207
- - README.md
208
- - LICENSE.md
235
+ - README.adoc
236
+ - LICENSE.adoc
209
237
  files:
210
- - LICENSE.md
211
- - README.md
238
+ - LICENSE.adoc
239
+ - README.adoc
212
240
  - lib/refinements.rb
213
241
  - lib/refinements/arrays.rb
214
242
  - lib/refinements/big_decimals.rb
@@ -218,14 +246,15 @@ files:
218
246
  - lib/refinements/identity.rb
219
247
  - lib/refinements/pathnames.rb
220
248
  - lib/refinements/strings.rb
221
- homepage: https://github.com/bkuhlmann/refinements
249
+ homepage: https://www.alchemists.io/projects/refinements
222
250
  licenses:
223
251
  - Apache-2.0
224
252
  metadata:
225
- source_code_uri: https://github.com/bkuhlmann/refinements
226
- changelog_uri: https://github.com/bkuhlmann/refinements/blob/master/CHANGES.md
227
253
  bug_tracker_uri: https://github.com/bkuhlmann/refinements/issues
228
- post_install_message:
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:
229
258
  rdoc_options: []
230
259
  require_paths:
231
260
  - lib
@@ -240,8 +269,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
240
269
  - !ruby/object:Gem::Version
241
270
  version: '0'
242
271
  requirements: []
243
- rubygems_version: 3.1.2
244
- signing_key:
272
+ rubygems_version: 3.1.4
273
+ signing_key:
245
274
  specification_version: 4
246
275
  summary: A collection of refinements to core Ruby objects.
247
276
  test_files: []
metadata.gz.sig CHANGED
Binary file
data/README.md DELETED
@@ -1,252 +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
- - `.rewrite` - When given a file path and a block, it provides the contents of the recently read
53
- file for manipulation and immediate writing back to the same file.
54
- - Provides Hash refinements:
55
- - `#except` - Answers new hash with given keys removed without modifying calling hash.
56
- - `#except!` - Answers new hash with given keys removed while modifying calling hash.
57
- - `#symbolize_keys` - Converts keys to symbols without modifying itself.
58
- - `#symbolize_keys!` - Converts keys to symbols while modifying itself.
59
- - `#deep_merge` - Merges deeply nested hashes together without modifying itself.
60
- - `#deep_merge!` - Merges deeply nested hashes together while modifying itself.
61
- - `#reverse_merge` - Merges calling hash into passed in hash without modifying calling hash.
62
- - `#reverse_merge!` - Merges calling hash into passed in hash while modifying calling hash.
63
- - Provides Pathname refinements:
64
- - `#rewrite` - When given a block, it provides the contents of the recently read file for
65
- manipulation and immediate writing back to the same file.
66
- - Provides String refinements:
67
- - `#first` - Answers first character of a string or first set of characters if given a number.
68
- - `#last` - Answers last character of a string or last set of characters if given a number.
69
- - `#blank?` - Answers `true`/`false` based on whether string is blank or not (i.e. `<space>`,
70
- `\n`, `\t`, `\r`).
71
- - `#up` - Answers string with only first letter upcased.
72
- - `#down` - Answers string with only first letter downcased.
73
- - `#camelcase` - Answers a camelcased string. Example: "ThisIsCamelcase".
74
- - `#snakecase` - Answers a snakecased string. Example: "this_is_snakecase".
75
- - `#titleize` - Answers titleized string. Example: "This Is Titleized".
76
- - `#use` - Provides hash value computation, via a block, by using
77
- only the keys as arguments to the block.
78
-
79
- ## Requirements
80
-
81
- 1. [Ruby 2.6.x](https://www.ruby-lang.org).
82
- 1. A solid understanding of [Ruby refinements and lexical scope](https://www.youtube.com/watch?v=qXC9Gk4dCEw).
83
-
84
- ## Setup
85
-
86
- Type the following to install:
87
-
88
- gem install refinements
89
-
90
- Add the following to your Gemfile file:
91
-
92
- gem "refinements"
93
-
94
- ## Usage
95
-
96
- ### Requires
97
-
98
- If all refinements are not desired, add the following to your `Gemfile` instead:
99
-
100
- gem "refinements", require: false
101
-
102
- ...then require the specific refinement, as needed. Example:
103
-
104
- require "refinements/arrays"
105
- require "refinements/date_times"
106
- require "refinements/big_decimals"
107
- require "refinements/files"
108
- require "refinements/hashes"
109
- require "refinements/pathnames"
110
- require "refinements/strings"
111
-
112
- ### Using
113
-
114
- Much like including/extending a module, you'll need modify your object(s) to use the refinement(s):
115
-
116
- class Example
117
- using Refinements::Arrays
118
- using Refinements::DateTimes
119
- using Refinements::BigDecimals
120
- using Refinements::Files
121
- using Refinements::Hashes
122
- using Refinements::Pathnames
123
- using Refinements::Strings
124
- end
125
-
126
- ### Examples
127
-
128
- The following sections demonstrate how each refinement enriches your objects with new capabilities.
129
-
130
- #### Array
131
-
132
- example = ["An", nil, "", "Example"]
133
- example.compress # => ["An", "Example"]
134
- example # => ["An", nil, "", "Example"]
135
-
136
- example = ["An", nil, "", "Example"]
137
- example.compress! # => ["An", "Example"]
138
- example # => ["An", "Example"]
139
-
140
- #### DateTime
141
-
142
- DateTime.utc # => #<DateTime: 2019-12-31T18:17:00+00:00 ((2458849j,65820s,181867000n),+0s,2299161j)>
143
-
144
- #### Big Decimal
145
-
146
- BigDecimal.new("5.0E-10").inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"
147
-
148
- #### File
149
-
150
- File.rewrite("/test.txt") { |content| content.gsub "[placeholder]", "example" }
151
-
152
- #### Hash
153
-
154
- example = {a: 1, b: 2, c: 3}
155
- example.except :a, :b # => {c: 3}
156
- example # => {a: 1, b: 2, c: 3}
157
-
158
- example = {a: 1, b: 2, c: 3}
159
- example.except! :a, :b # => {c: 3}
160
- example # => {c: 3}
161
-
162
- example = {"a" => 1, "b" => 2}
163
- example.symbolize_keys # => {a: 1, b: 2}
164
- example # => {"a" => 1, "b" => 2}
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, c: 3}
171
- example.slice :a, :c # => {a: 1, c: 3}
172
- example # => {a: 1, b: 2, c: 3}
173
-
174
- example = {a: 1, b: 2, c: 3}
175
- example.slice! :a, :c # => {a: 1, c: 3}
176
- example # => {a: 1, c: 3}
177
-
178
- example = {a: "A", b: {one: "One", two: "Two"}}
179
- example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
180
- example # => {a: "A", b: {one: "One", two: "Two"}}
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: 1, two: "Two"}}
185
-
186
- example = {a: 1, b: 2}
187
- example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
188
- example # => {a: 1, b: 2}
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, c: 3}
193
-
194
- example = {unit: "221B", street: "Baker Street", city: "London", country: "UK"}
195
- example.use { |unit, street| "#{unit} #{street}" } # => "221B Baker Street"
196
-
197
- #### Pathname
198
-
199
- Pathname("/test.txt").rewrite { |content| content.sub "[placeholder]", "example" }
200
-
201
- #### String
202
-
203
- "example".first # => "e"
204
- "example".first 4 # => "exam"
205
-
206
- "instant".last # => "t"
207
- "instant".last 3 # => "ant"
208
-
209
- " \n\t\r".blank? # => true
210
- "example".up # => "Example"
211
- "EXAMPLE".down # => "eXAMPLE"
212
- "this_is_an_example".camelcase # => "ThisIsAnExample"
213
- "ThisIsAnExample".snakecase # => "this_is_an_example"
214
- "ThisIsAnExample".titleize # => "This Is An Example"
215
-
216
- ## Tests
217
-
218
- To test, run:
219
-
220
- bundle exec rake
221
-
222
- ## Versioning
223
-
224
- Read [Semantic Versioning](https://semver.org) for details. Briefly, it means:
225
-
226
- - Major (X.y.z) - Incremented for any backwards incompatible public API changes.
227
- - Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
228
- - Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
229
-
230
- ## Code of Conduct
231
-
232
- Please note that this project is released with a [CODE OF CONDUCT](CODE_OF_CONDUCT.md). By
233
- participating in this project you agree to abide by its terms.
234
-
235
- ## Contributions
236
-
237
- Read [CONTRIBUTING](CONTRIBUTING.md) for details.
238
-
239
- ## License
240
-
241
- Copyright 2015 [Alchemists](https://www.alchemists.io).
242
- Read [LICENSE](LICENSE.md) for details.
243
-
244
- ## History
245
-
246
- Read [CHANGES](CHANGES.md) for details.
247
- Built with [Gemsmith](https://github.com/bkuhlmann/gemsmith).
248
-
249
- ## Credits
250
-
251
- Developed by [Brooke Kuhlmann](https://www.alchemists.io) at
252
- [Alchemists](https://www.alchemists.io).