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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0dbec0b903b2ce6a78545bf34285ab60d20fd1b511975c750829a883107de915
4
- data.tar.gz: 14c0fa259be4dd70fcc3766bcf745b9c267af3b1a58c0f32fc0f1fdfbb9d6458
3
+ metadata.gz: 1cd2e7a6476708f49a9261d1021f1cab7e5342e96e03279e31f3ca598a285d09
4
+ data.tar.gz: 97144fe2d70d45b8b6c8769d98987882f10df33b297ffe4e842703f826c8bf02
5
5
  SHA512:
6
- metadata.gz: 0bdd1ea6074fd07ae02659c283a1a035da4695d5375150264eea4e88ca5eae7b041a41ea6a14384305143695752fd6c1397477be387a28e75d39f80c907c8f87
7
- data.tar.gz: 0caa5d7b26ad27a5378daad28b9a0cc7c7bc6c6f0d5db5d3fc2f20cacc0a834ae902269b74432196df89b90510d918afa60a941ae19254cb6bf0eecc46fec8f8
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.
@@ -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.is_a? self.class
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
 
@@ -50,6 +50,8 @@ module Refinements
50
50
  .then { |parts| combine parts, :down, "_" }
51
51
  end
52
52
 
53
+ def squish = gsub(/[[:space:]]+/, " ").tap(&:strip!)
54
+
53
55
  def titleize
54
56
  return capitalize unless match? DELIMITERS
55
57
 
data/refinements.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "refinements"
5
- spec.version = "9.7.1"
5
+ spec.version = "9.8.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://www.alchemists.io/projects/refinements"
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.7.1
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-10-22 00:00:00.000000000 Z
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.24
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