refinements 9.7.1 → 9.8.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/README.adoc +86 -0
- data/lib/refinements/hashes.rb +14 -8
- data/lib/refinements/strings.rb +2 -0
- data/refinements.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +3 -3
- 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: 1cd2e7a6476708f49a9261d1021f1cab7e5342e96e03279e31f3ca598a285d09
|
4
|
+
data.tar.gz: 97144fe2d70d45b8b6c8769d98987882f10df33b297ffe4e842703f826c8bf02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e3af9aa7b5ed89ce79cde70b6b9985495034b02b5b3ae39c832140652c03d768d04f90a94a3fd206b4a64a539e26445cdccbfa859abdece1d0e00de43cbbdd3c
|
7
|
+
data.tar.gz: 844ca609869449d11639e1f69c2c968768ee4f5df6034c9230315851ea90ef0d31fa6fc4e44bdcad23824ed403972bfde75e8ff5eb212a12228d80ef7c2e31cd
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/README.adoc
CHANGED
@@ -574,6 +574,82 @@ example.symbolize_keys! # {a: 1, b: 2}
|
|
574
574
|
example # {a: 1, b: 2}
|
575
575
|
----
|
576
576
|
|
577
|
+
===== #transform_with
|
578
|
+
|
579
|
+
Transforms key/value pairs based on specific operations where each operation (i.e. function that responds to the `call` message). Does not mutate itself.
|
580
|
+
|
581
|
+
You can transform multiple values at once:
|
582
|
+
|
583
|
+
[source,ruby]
|
584
|
+
----
|
585
|
+
example = {name: "Jayne Doe", email: "<jd@example.com>"}
|
586
|
+
|
587
|
+
example.transform_with name: -> value { value.delete_suffix " Doe" },
|
588
|
+
email: -> value { value.tr "<>", "" }
|
589
|
+
# {name: "Jayne", email: "jd@example.com"}
|
590
|
+
----
|
591
|
+
|
592
|
+
Invalid keys are ignored:
|
593
|
+
|
594
|
+
[source,ruby]
|
595
|
+
----
|
596
|
+
example.transform_with bogus: -> value { value.tr "<>", "" }
|
597
|
+
# {email: "<jd@example.com>"}
|
598
|
+
----
|
599
|
+
|
600
|
+
Nil values are skipped:
|
601
|
+
|
602
|
+
[source,ruby]
|
603
|
+
----
|
604
|
+
{email: nil}.transform_with email: -> value { value.tr "<>", "" }
|
605
|
+
# {email: nil}
|
606
|
+
----
|
607
|
+
|
608
|
+
The original object will not be mutated:
|
609
|
+
|
610
|
+
[source,ruby]
|
611
|
+
----
|
612
|
+
example # {name: "Jayne Doe", email: "<jd@example.com>"}
|
613
|
+
----
|
614
|
+
|
615
|
+
===== #transform_with!
|
616
|
+
|
617
|
+
Transforms key/value pairs based on specific operations where each operation (i.e. function that responds to the `call` message). Mutates itself.
|
618
|
+
|
619
|
+
You can transform multiple values at once:
|
620
|
+
|
621
|
+
[source,ruby]
|
622
|
+
----
|
623
|
+
example = {name: "Jayne Doe", email: "<jd@example.com>"}
|
624
|
+
|
625
|
+
example.transform_with! name: -> value { value.delete_suffix " Doe" },
|
626
|
+
email: -> value { value.tr "<>", "" }
|
627
|
+
# {name: "Jayne", email: "jd@example.com"}
|
628
|
+
----
|
629
|
+
|
630
|
+
Invalid keys are ignored:
|
631
|
+
|
632
|
+
[source,ruby]
|
633
|
+
----
|
634
|
+
example.transform_with! bogus: -> value { value.tr "<>", "" }
|
635
|
+
# {email: "<jd@example.com>"}
|
636
|
+
----
|
637
|
+
|
638
|
+
Nil values are skipped:
|
639
|
+
|
640
|
+
[source,ruby]
|
641
|
+
----
|
642
|
+
{email: nil}.transform_with! email: -> value { value.tr "<>", "" }
|
643
|
+
# {email: nil}
|
644
|
+
----
|
645
|
+
|
646
|
+
The original object will be mutated:
|
647
|
+
|
648
|
+
[source,ruby]
|
649
|
+
----
|
650
|
+
example # {name: "Jayne", email: "jd@example.com"}
|
651
|
+
----
|
652
|
+
|
577
653
|
===== #use
|
578
654
|
|
579
655
|
Passes each hash value as a block argument for further processing.
|
@@ -1135,6 +1211,16 @@ Answers a snake cased string.
|
|
1135
1211
|
"ThisIsAnExample".snakecase # "this_is_an_example"
|
1136
1212
|
----
|
1137
1213
|
|
1214
|
+
===== #squish
|
1215
|
+
|
1216
|
+
Removes leading, in body, and trailing whitespace, including any tabs or newlines, without mutating itself. Processes ASCII and unicode whitespace as well.
|
1217
|
+
|
1218
|
+
[source,ruby]
|
1219
|
+
----
|
1220
|
+
"one two three".squish # "one two three"
|
1221
|
+
" one two \n \t three ".squish # "one two three"
|
1222
|
+
----
|
1223
|
+
|
1138
1224
|
===== #titleize
|
1139
1225
|
|
1140
1226
|
Answers a title string with proper capitalization of each word.
|
data/lib/refinements/hashes.rb
CHANGED
@@ -47,9 +47,7 @@ module Refinements
|
|
47
47
|
|
48
48
|
def deep_symbolize_keys! = replace(deep_symbolize_keys)
|
49
49
|
|
50
|
-
def fetch_value(key, *default_value, &)
|
51
|
-
fetch(key, *default_value, &) || default_value.first
|
52
|
-
end
|
50
|
+
def fetch_value(key, *default_value, &) = fetch(key, *default_value, &) || default_value.first
|
53
51
|
|
54
52
|
# :reek:TooManyStatements
|
55
53
|
def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
|
@@ -58,7 +56,7 @@ module Refinements
|
|
58
56
|
reduce({}) do |flat, (key, value)|
|
59
57
|
flat_key = prefix ? "#{prefix}#{delimiter}#{key}" : key
|
60
58
|
|
61
|
-
next flat.merge flat_key.public_send(cast) => value unless value
|
59
|
+
next flat.merge flat_key.public_send(cast) => value unless value in Hash
|
62
60
|
|
63
61
|
flat.merge(
|
64
62
|
recurse { value.flatten_keys prefix: flat_key, delimiter:, cast: }
|
@@ -74,10 +72,7 @@ module Refinements
|
|
74
72
|
return self unless block
|
75
73
|
|
76
74
|
transform = yield self
|
77
|
-
|
78
|
-
transform.each do |key, value|
|
79
|
-
transform[key] = value.recurse(&block) if value.is_a? self.class
|
80
|
-
end
|
75
|
+
transform.each { |key, value| transform[key] = value.recurse(&block) if value in Hash }
|
81
76
|
end
|
82
77
|
|
83
78
|
def stringify_keys = transform_keys(&:to_s)
|
@@ -88,6 +83,17 @@ module Refinements
|
|
88
83
|
|
89
84
|
def symbolize_keys! = transform_keys!(&:to_sym)
|
90
85
|
|
86
|
+
def transform_with(operations) = dup.transform_with! operations
|
87
|
+
|
88
|
+
def transform_with! operations
|
89
|
+
operations.each do |key, function|
|
90
|
+
value = self[key]
|
91
|
+
self[key] = function.call value if value
|
92
|
+
end
|
93
|
+
|
94
|
+
self
|
95
|
+
end
|
96
|
+
|
91
97
|
def use &block
|
92
98
|
return [] unless block
|
93
99
|
|
data/lib/refinements/strings.rb
CHANGED
data/refinements.gemspec
CHANGED
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: 9.
|
4
|
+
version: 9.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -28,7 +28,7 @@ cert_chain:
|
|
28
28
|
CxDe2+VuChj4I1nvIHdu+E6XoEVlanUPKmSg6nddhkKn2gC45Kyzh6FZqnzH/CRp
|
29
29
|
RFE=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2022-
|
31
|
+
date: 2022-11-15 00:00:00.000000000 Z
|
32
32
|
dependencies: []
|
33
33
|
description:
|
34
34
|
email:
|
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
83
|
- !ruby/object:Gem::Version
|
84
84
|
version: '0'
|
85
85
|
requirements: []
|
86
|
-
rubygems_version: 3.3.
|
86
|
+
rubygems_version: 3.3.25
|
87
87
|
signing_key:
|
88
88
|
specification_version: 4
|
89
89
|
summary: A collection of refinements to core Ruby objects.
|
metadata.gz.sig
CHANGED
Binary file
|