refinements 10.1.1 → 11.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +98 -29
- data/lib/refinements/arrays.rb +28 -1
- data/lib/refinements/pathnames.rb +1 -3
- data/lib/refinements/strings.rb +2 -2
- data/refinements.gemspec +2 -2
- data.tar.gz.sig +0 -0
- metadata +4 -4
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efdcd2042bcfde5031fad3d0049e01fc4e1ad23a9d4841ee601fdcb02a96d3af
|
4
|
+
data.tar.gz: 55fac5447439c53705870a9409268e96aaf633e7a420f9d4678257f9b68f078d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81949b0fb95b4aca53d6fd88f51d8cb4f0c8738bbb3eac83816880ff6b479ac86228847dd6660d8a99a861c5e3aee6e6985a0628764af037b328db55422cc7fb
|
7
|
+
data.tar.gz: 6b40c6be0c5cc6304366fe60c8df54bca9e3ab366338f45308b2267f9a81f6adb123002740feb933af3313a0349a9a5035c39525c0b90d48f875842fefd0c3cf
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -31,18 +31,34 @@ Enhances the following objects:
|
|
31
31
|
|
32
32
|
== Setup
|
33
33
|
|
34
|
-
To install, run:
|
34
|
+
To install _with_ security, run:
|
35
|
+
|
36
|
+
[source,bash]
|
37
|
+
----
|
38
|
+
# 💡 Skip this line if you already have the public certificate installed.
|
39
|
+
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
|
40
|
+
gem install refinements --trust-policy HighSecurity
|
41
|
+
----
|
42
|
+
|
43
|
+
To install _without_ security, run:
|
35
44
|
|
36
45
|
[source,bash]
|
37
46
|
----
|
38
47
|
gem install refinements
|
39
48
|
----
|
40
49
|
|
41
|
-
|
50
|
+
You can also add the gem directly to your project:
|
51
|
+
|
52
|
+
[source,bash]
|
53
|
+
----
|
54
|
+
bundle add refinements
|
55
|
+
----
|
56
|
+
|
57
|
+
Once the gem is installed, you only need to require it:
|
42
58
|
|
43
59
|
[source,ruby]
|
44
60
|
----
|
45
|
-
|
61
|
+
require "refinements"
|
46
62
|
----
|
47
63
|
|
48
64
|
== Usage
|
@@ -262,6 +278,46 @@ needs to be a specific size with padded values.
|
|
262
278
|
[1, 2].pad 3, max: 3 # [1, 2, 3]
|
263
279
|
----
|
264
280
|
|
281
|
+
===== #pick
|
282
|
+
|
283
|
+
Answers value of first element that matches given key.
|
284
|
+
|
285
|
+
[source,ruby]
|
286
|
+
----
|
287
|
+
array = [{name: "a", label: "A"}, {name: "b", label: "B"}, {name: "c", label: "C"}]
|
288
|
+
|
289
|
+
array.pick :name # "a"
|
290
|
+
array.pick :name, :label # ["a", "A"]
|
291
|
+
array.pick # nil
|
292
|
+
[].pick(:other) # nil
|
293
|
+
----
|
294
|
+
|
295
|
+
===== #pluck
|
296
|
+
|
297
|
+
Answers values of all elements that match given keys.
|
298
|
+
|
299
|
+
[source,ruby]
|
300
|
+
----
|
301
|
+
array = [{name: "a", label: "A"}, {name: "b", label: "B"}, {name: "c", label: "C"}]
|
302
|
+
|
303
|
+
array.pluck :name # ["a", "b", "c"]
|
304
|
+
array.pluck :name, :label # [["a", "A"], ["b", "B"], ["c", "C"]]
|
305
|
+
array.pluck # []
|
306
|
+
[].pluck :other # []
|
307
|
+
----
|
308
|
+
|
309
|
+
===== #replace_at
|
310
|
+
|
311
|
+
Answers mutated array where an element -- at a specific index -- is replaced by single or multiple elements.
|
312
|
+
|
313
|
+
[source,ruby]
|
314
|
+
----
|
315
|
+
%i[a b c].replace_at(0, :x) # [:x, :b, :c]
|
316
|
+
%i[a b c].replace_at(1, :x) # [:a, :x, :c]
|
317
|
+
%i[a b c].replace_at(1, :x, :y) # [:a, :x, :y, :c]
|
318
|
+
%i[a b c].replace_at(-1, :x) # [:a, :b, :x]
|
319
|
+
----
|
320
|
+
|
265
321
|
===== #ring
|
266
322
|
|
267
323
|
Answers a circular array which can enumerate before, current, after elements.
|
@@ -303,17 +359,31 @@ Answers mutated array where all target elements are replaced by single or multip
|
|
303
359
|
|
304
360
|
===== #to_sentence
|
305
361
|
|
306
|
-
Answers a sentence using `"
|
362
|
+
Answers a sentence using `"and"` as the default conjunction and `", "` as the default delimiter.
|
307
363
|
Useful when building documentation, answering human readable error messages, etc.
|
308
364
|
|
309
365
|
[source,ruby]
|
310
366
|
----
|
311
|
-
[].to_sentence
|
312
|
-
["
|
313
|
-
["a", :b].to_sentence
|
314
|
-
[1, "a", :b, 2.0, /\w+/].
|
315
|
-
%w[one two three].to_sentence
|
316
|
-
%w[eins zwei drei].to_sentence
|
367
|
+
[].to_sentence # ""
|
368
|
+
["demo"].to_sentence # "demo"
|
369
|
+
["a", :b].to_sentence # "a and b"
|
370
|
+
[1, "a", :b, 2.0, /\w+/].to_sentence # "1, a, b, 2.0, and (?-mix:\\w+)"
|
371
|
+
%w[one two three].to_sentence # "one, two, and three"
|
372
|
+
%w[eins zwei drei].to_sentence "und", delimiter: " " # "eins zwei und drei"
|
373
|
+
----
|
374
|
+
|
375
|
+
===== #to_usage
|
376
|
+
|
377
|
+
Builds upon and enhances `#to_sentence` further by answering a sentence which all elements are inspected -- where each element of the array is called with `#inspect` -- before turned into a sentence using `"and"` as the default conjunction and `", "` as the default delimiter. This is useful when providing detailed error messages _and_ you need to detail the types of element used.
|
378
|
+
|
379
|
+
[source,ruby]
|
380
|
+
----
|
381
|
+
[].to_usage # ""
|
382
|
+
["demo"].to_usage # "\"demo\""
|
383
|
+
["a", :b].to_usage # "\"a\" and :b"
|
384
|
+
[1, "a", :b, 2.0, /\w+/].to_usage # "1, \"a\", :b, 2.0, and /\\w+/"
|
385
|
+
%w[one two three].to_usage # "\"one\", \"two\", and \"three\""
|
386
|
+
%w[eins zwei drei].to_usage "und", delimiter: " " # "\"eins\" \"zwei\" und \"drei\""
|
317
387
|
----
|
318
388
|
|
319
389
|
==== Big Decimal
|
@@ -470,12 +540,12 @@ expression `example.fetch(:desired_key) || "default_value"`.
|
|
470
540
|
|
471
541
|
[source,ruby]
|
472
542
|
----
|
473
|
-
{a: "
|
474
|
-
{a: "
|
543
|
+
{a: "demo"}.fetch_value :a, "default" # "demo"
|
544
|
+
{a: "demo"}.fetch_value :a # "demo"
|
475
545
|
{a: nil}.fetch_value :a, "default" # "default"
|
476
546
|
{}.fetch_value(:a) { "default" } # "default"
|
477
547
|
{}.fetch_value :a # KeyError
|
478
|
-
{a: "
|
548
|
+
{a: "demo"}.fetch_value # ArgumentError
|
479
549
|
----
|
480
550
|
|
481
551
|
===== #flatten_keys
|
@@ -485,7 +555,7 @@ though.
|
|
485
555
|
|
486
556
|
[source,ruby]
|
487
557
|
----
|
488
|
-
{a: {b: 1}}.flatten_keys prefix: :
|
558
|
+
{a: {b: 1}}.flatten_keys prefix: :demo # {demo_a_b: 1}
|
489
559
|
{a: {b: 1}}.flatten_keys delimiter: :| # {:"a|b" => 1}
|
490
560
|
|
491
561
|
example = {a: {b: 1}}
|
@@ -500,7 +570,7 @@ though.
|
|
500
570
|
|
501
571
|
[source,ruby]
|
502
572
|
----
|
503
|
-
{a: {b: 1}}.flatten_keys! prefix: :
|
573
|
+
{a: {b: 1}}.flatten_keys! prefix: :demo # {demo_a_b: 1}
|
504
574
|
{a: {b: 1}}.flatten_keys! delimiter: :| # {:"a|b" => 1}
|
505
575
|
|
506
576
|
example = {a: {b: 1}}
|
@@ -665,11 +735,11 @@ answered instead.
|
|
665
735
|
|
666
736
|
[source,ruby]
|
667
737
|
----
|
668
|
-
io = IO.new IO.sysopen(Pathname("
|
738
|
+
io = IO.new IO.sysopen(Pathname("demo.txt").to_s, "w+")
|
669
739
|
other = IO.new IO.sysopen(Pathname("other.txt").to_s, "w+")
|
670
740
|
|
671
741
|
io.redirect other # "#<IO:fd 20>"
|
672
|
-
io.redirect(other) { |stream| stream.write "
|
742
|
+
io.redirect(other) { |stream| stream.write "demo" } # "#<IO:fd 21>"
|
673
743
|
----
|
674
744
|
|
675
745
|
===== #reread
|
@@ -678,15 +748,15 @@ Answers full stream by rewinding to beginning of stream and reading all content.
|
|
678
748
|
|
679
749
|
[source,ruby]
|
680
750
|
----
|
681
|
-
io = IO.new IO.sysopen(Pathname("
|
682
|
-
io.write "This is a
|
751
|
+
io = IO.new IO.sysopen(Pathname("demo.txt").to_s, "w+")
|
752
|
+
io.write "This is a demo."
|
683
753
|
|
684
|
-
io.reread # "This is a
|
754
|
+
io.reread # "This is a demo."
|
685
755
|
io.reread 4 # "This"
|
686
756
|
|
687
757
|
buffer = "".dup
|
688
|
-
io.reread(buffer: buffer) # "This is a
|
689
|
-
buffer # "This is a
|
758
|
+
io.reread(buffer: buffer) # "This is a demo."
|
759
|
+
buffer # "This is a demo."
|
690
760
|
----
|
691
761
|
|
692
762
|
===== #squelch
|
@@ -813,8 +883,7 @@ Pathname.make_temp_dir { |path| path.join "sub_dir" } # Pathname:/var/fol
|
|
813
883
|
|
814
884
|
===== .require_tree
|
815
885
|
|
816
|
-
Requires all files in given root path and corresponding nested tree structure. All files are sorted
|
817
|
-
before being required to ensure consistent behavior. Example:
|
886
|
+
Requires all Ruby files in given root path and corresponding nested tree structure. All files are sorted before being required to ensure consistent behavior. Example:
|
818
887
|
|
819
888
|
[source,ruby]
|
820
889
|
----
|
@@ -1142,11 +1211,11 @@ Answers indentation (string) which is the result of the multiplier times padding
|
|
1142
1211
|
|
1143
1212
|
[source,ruby]
|
1144
1213
|
----
|
1145
|
-
"example".indent
|
1146
|
-
"example".indent 0
|
1147
|
-
"example".indent -1
|
1148
|
-
"example".indent 2
|
1149
|
-
"example".indent 3,
|
1214
|
+
"example".indent # " example"
|
1215
|
+
"example".indent 0 # "example"
|
1216
|
+
"example".indent -1 # "example"
|
1217
|
+
"example".indent 2 # " example"
|
1218
|
+
"example".indent 3, pad: " " # " example"
|
1150
1219
|
----
|
1151
1220
|
|
1152
1221
|
===== #last
|
data/lib/refinements/arrays.rb
CHANGED
@@ -33,6 +33,29 @@ module Refinements
|
|
33
33
|
|
34
34
|
def pad(value, max: size) = dup.fill(value, size..(max - 1))
|
35
35
|
|
36
|
+
def pick(*keys)
|
37
|
+
return if empty?
|
38
|
+
|
39
|
+
keys.many? ? keys.map { |key| first[key] } : first[keys.first]
|
40
|
+
end
|
41
|
+
|
42
|
+
def pluck(*keys)
|
43
|
+
return dup if empty?
|
44
|
+
return [] if keys.empty?
|
45
|
+
|
46
|
+
if keys.many?
|
47
|
+
map { |element| keys.map { |key| element[key] } }
|
48
|
+
else
|
49
|
+
key = keys.first
|
50
|
+
map { |element| element[key] }
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def replace_at(index, *elements)
|
55
|
+
delete_at index
|
56
|
+
insert(index, *elements)
|
57
|
+
end
|
58
|
+
|
36
59
|
def ring(&) = [last, *self, first].each_cons(3, &)
|
37
60
|
|
38
61
|
def supplant target, *replacements
|
@@ -49,13 +72,17 @@ module Refinements
|
|
49
72
|
self
|
50
73
|
end
|
51
74
|
|
52
|
-
def to_sentence
|
75
|
+
def to_sentence conjunction = "and", delimiter: ", "
|
53
76
|
case length
|
54
77
|
when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
|
55
78
|
when 2 then join " #{conjunction} "
|
56
79
|
else join
|
57
80
|
end
|
58
81
|
end
|
82
|
+
|
83
|
+
def to_usage conjunction = "and", delimiter: ", "
|
84
|
+
map(&:inspect).to_sentence conjunction, delimiter:
|
85
|
+
end
|
59
86
|
end
|
60
87
|
end
|
61
88
|
end
|
@@ -24,9 +24,7 @@ module Refinements
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
def require_tree
|
28
|
-
new(root).files(pattern).each { |path| require path.to_s }
|
29
|
-
end
|
27
|
+
def require_tree(root) = new(root).files("**/*.rb").each { |path| require path.to_s }
|
30
28
|
|
31
29
|
def root = new(File::SEPARATOR)
|
32
30
|
end
|
data/lib/refinements/strings.rb
CHANGED
@@ -26,8 +26,8 @@ module Refinements
|
|
26
26
|
self[..(maximum - 1)]
|
27
27
|
end
|
28
28
|
|
29
|
-
def indent multiplier = 1,
|
30
|
-
multiplier.negative? ? self : (
|
29
|
+
def indent multiplier = 1, pad: " "
|
30
|
+
multiplier.negative? ? self : (pad * multiplier) + self
|
31
31
|
end
|
32
32
|
|
33
33
|
def last minimum = 0
|
data/refinements.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = "refinements"
|
5
|
-
spec.version = "
|
5
|
+
spec.version = "11.0.1"
|
6
6
|
spec.authors = ["Brooke Kuhlmann"]
|
7
7
|
spec.email = ["brooke@alchemists.io"]
|
8
8
|
spec.homepage = "https://alchemists.io/projects/refinements"
|
9
|
-
spec.summary = "A collection of
|
9
|
+
spec.summary = "A collection of core Ruby object refinements."
|
10
10
|
spec.license = "Hippocratic-2.1"
|
11
11
|
|
12
12
|
spec.metadata = {
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: refinements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 11.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -35,7 +35,7 @@ cert_chain:
|
|
35
35
|
3n5C8/6Zh9DYTkpcwPSuIfAga6wf4nXc9m6JAw8AuMLaiWN/r/2s4zJsUHYERJEu
|
36
36
|
gZGm4JqtuSg8pYjPeIJxS960owq+SfuC+jxqmRA54BisFCv/0VOJi7tiJVY=
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date: 2023-
|
38
|
+
date: 2023-06-19 00:00:00.000000000 Z
|
39
39
|
dependencies: []
|
40
40
|
description:
|
41
41
|
email:
|
@@ -90,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
90
|
- !ruby/object:Gem::Version
|
91
91
|
version: '0'
|
92
92
|
requirements: []
|
93
|
-
rubygems_version: 3.4.
|
93
|
+
rubygems_version: 3.4.14
|
94
94
|
signing_key:
|
95
95
|
specification_version: 4
|
96
|
-
summary: A collection of
|
96
|
+
summary: A collection of core Ruby object refinements.
|
97
97
|
test_files: []
|
metadata.gz.sig
CHANGED
Binary file
|