refinements 9.7.1 → 10.0.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: 6886f2e81d621658654c24748db65834f2f0d6a1f37b9cd6f5f201dedc0d23e6
4
+ data.tar.gz: c047e00e34e10f9c4ed31561c5e64e4d247151dec65b1251a14f79d273e21d02
5
5
  SHA512:
6
- metadata.gz: 0bdd1ea6074fd07ae02659c283a1a035da4695d5375150264eea4e88ca5eae7b041a41ea6a14384305143695752fd6c1397477be387a28e75d39f80c907c8f87
7
- data.tar.gz: 0caa5d7b26ad27a5378daad28b9a0cc7c7bc6c6f0d5db5d3fc2f20cacc0a834ae902269b74432196df89b90510d918afa60a941ae19254cb6bf0eecc46fec8f8
6
+ metadata.gz: 771838c7e7fa1b044b861724ec99ab78a2f1393db0dc9f2906ebf9fd58d34aa2adee55dccf7c4cd045e9ac4b3b30a49fbc613b23628ca216ce47c271289038d4
7
+ data.tar.gz: 13c855b9fdc681ab75e008a61059bec785f8918f7fbfa44576100a5f014102ea806d6b0061eae89475106cb8cc55625026c536d5f6d55144d8cf06127604d7cb
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -128,28 +128,32 @@ example.combinatorial? [] # false
128
128
 
129
129
  ===== #compress
130
130
 
131
- Removes `nil` and empty objects without mutating itself.
131
+ Removes `nil` and empty objects without mutating itself. Answers itself if there is nothing to remove.
132
132
 
133
133
  [source,ruby]
134
134
  ----
135
135
  object = Object.new
136
136
  example = [1, "blueberry", nil, "", [], {}, object]
137
137
 
138
+ [].compress # []
139
+ [1, 2].compress # [1, 2]
138
140
  example.compress # [1, "blueberry", object]
139
141
  example # [1, "blueberry", nil, "", [], {}, object]
140
142
  ----
141
143
 
142
144
  ===== #compress!
143
145
 
144
- Removes `nil` and empty values while mutating itself.
146
+ Removes `nil` and empty values while mutating itself. Answers `nil` if there is nothing to remove.
145
147
 
146
148
  [source,ruby]
147
149
  ----
148
150
  object = Object.new
149
151
  example = [1, "blueberry", nil, "", [], {}, object]
150
152
 
151
- example.compress # [1, "blueberry", object]
152
- example # [1, "blueberry", object]
153
+ [].compress! # nil
154
+ [1, 2].compress! # nil
155
+ example.compress! # [1, "blueberry", object]
156
+ example # [1, "blueberry", object]
153
157
  ----
154
158
 
155
159
  ===== #excluding
@@ -219,7 +223,7 @@ Answers the maximum extracted value from a collection of objects.
219
223
 
220
224
  [source,ruby]
221
225
  ----
222
- Point = Struct.new :x, :y, keyword_init: true
226
+ Point = Struct.new :x, :y
223
227
  points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
224
228
 
225
229
  points.maximum(:x) # 2
@@ -244,7 +248,7 @@ Answers the minimum extracted value from a collection of objects.
244
248
 
245
249
  [source,ruby]
246
250
  ----
247
- Point = Struct.new :x, :y, keyword_init: true
251
+ Point = Struct.new :x, :y
248
252
  points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
249
253
 
250
254
  points.minimum(:x) # 0
@@ -367,28 +371,32 @@ example[:b] # []
367
371
 
368
372
  ===== #compress
369
373
 
370
- Removes `nil` and empty objects without mutating itself.
374
+ Removes `nil` and empty objects without mutating itself. Answers itself if there is nothing to remove.
371
375
 
372
376
  [source,ruby]
373
377
  ----
374
378
  object = Object.new
375
379
  example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
376
380
 
377
- example.compress # {a: 1, b: "blueberry", g: object}
378
- example # {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
381
+ {}.compress # {}
382
+ {a: 1, b: 2}.compress # {a: 1, b: 2}
383
+ example.compress # {a: 1, b: "blueberry", g: object}
384
+ example # {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
379
385
  ----
380
386
 
381
387
  ===== #compress!
382
388
 
383
- Removes `nil` and empty objects while mutating itself.
389
+ Removes `nil` and empty objects while mutating itself. Answers `nil` if there is nothing to remove.
384
390
 
385
391
  [source,ruby]
386
392
  ----
387
393
  object = Object.new
388
394
  example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
389
395
 
390
- example.compress! # {a: 1, b: "blueberry", g: object}
391
- example # {a: 1, b: "blueberry", g: object}
396
+ {}.compress! # nil
397
+ {a: 1, b: 2}.compress! # nil
398
+ example.compress! # {a: 1, b: "blueberry", g: object}
399
+ example # {a: 1, b: "blueberry", g: object}
392
400
  ----
393
401
 
394
402
  ===== #deep_merge
@@ -485,9 +493,6 @@ though.
485
493
  {a: {b: 1}}.flatten_keys prefix: :test # {test_a_b: 1}
486
494
  {a: {b: 1}}.flatten_keys delimiter: :| # {:"a|b" => 1}
487
495
 
488
- {a: {b: 1}}.flatten_keys cast: :to_s # {"a_b" => 1}
489
- {"a" => {"b" => 1}}.flatten_keys cast: :to_sym # {a_b: 1}
490
-
491
496
  example = {a: {b: 1}}
492
497
  example.flatten_keys # {a_b: 1}
493
498
  example # {a: {b: 1}}
@@ -500,6 +505,9 @@ though.
500
505
 
501
506
  [source,ruby]
502
507
  ----
508
+ {a: {b: 1}}.flatten_keys! prefix: :test # {test_a_b: 1}
509
+ {a: {b: 1}}.flatten_keys! delimiter: :| # {:"a|b" => 1}
510
+
503
511
  example = {a: {b: 1}}
504
512
  example.flatten_keys! # {a_b: 1}
505
513
  example # {a_b: 1}
@@ -574,6 +582,66 @@ example.symbolize_keys! # {a: 1, b: 2}
574
582
  example # {a: 1, b: 2}
575
583
  ----
576
584
 
585
+ ===== #transform_with
586
+
587
+ Transforms key/value pairs based on specific operations where each operation (i.e. function that responds to the `call` message). Does not mutate itself.
588
+
589
+ You can transform multiple values at once:
590
+
591
+ [source,ruby]
592
+ ----
593
+ example = {name: "Jayne Doe", email: "<jd@example.com>"}
594
+
595
+ example.transform_with name: -> value { value.delete_suffix " Doe" },
596
+ email: -> value { value.tr "<>", "" }
597
+ # {name: "Jayne", email: "jd@example.com"}
598
+ ----
599
+
600
+ Invalid keys are ignored:
601
+
602
+ [source,ruby]
603
+ ----
604
+ example.transform_with bogus: -> value { value.tr "<>", "" }
605
+ # {email: "<jd@example.com>"}
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
+ The original object will be mutated:
639
+
640
+ [source,ruby]
641
+ ----
642
+ example # {name: "Jayne", email: "jd@example.com"}
643
+ ----
644
+
577
645
  ===== #use
578
646
 
579
647
  Passes each hash value as a block argument for further processing.
@@ -1135,6 +1203,16 @@ Answers a snake cased string.
1135
1203
  "ThisIsAnExample".snakecase # "this_is_an_example"
1136
1204
  ----
1137
1205
 
1206
+ ===== #squish
1207
+
1208
+ Removes leading, in body, and trailing whitespace, including any tabs or newlines, without mutating itself. Processes ASCII and unicode whitespace as well.
1209
+
1210
+ [source,ruby]
1211
+ ----
1212
+ "one two three".squish # "one two three"
1213
+ " one two \n \t three ".squish # "one two three"
1214
+ ----
1215
+
1138
1216
  ===== #titleize
1139
1217
 
1140
1218
  Answers a title string with proper capitalization of each word.
@@ -1187,36 +1265,6 @@ buffer # "This is a test."
1187
1265
 
1188
1266
  ==== Struct
1189
1267
 
1190
- ===== .keyworded?
1191
-
1192
- ⚠️ Will be removed in the next major version. Use `.keyword_init?` instead.
1193
-
1194
- Answers whether a struct was constructed with keyword or positional arguments.
1195
-
1196
- [source,ruby]
1197
- ----
1198
- Struct.new(:a, keyword_init: true).keyworded? # true
1199
- Struct.new(:a).keyworded? # false
1200
- ----
1201
-
1202
- ===== .with_keywords
1203
-
1204
- Answers a struct instance with given keyword arguments regardless of
1205
- whether the struct was constructed with positional or keyword arguments.
1206
-
1207
- [source,ruby]
1208
- ----
1209
- Example = Struct.new :a, :b, :c
1210
- Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3>
1211
- Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil>
1212
- Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1>
1213
-
1214
- Example = Struct.new :a, :b, :c, keyword_init: true
1215
- Example.with_keywords a: 1, b: 2, c: 3 # #<struct a=1, b=2, c=3>
1216
- Example.with_keywords a: 1 # #<struct a=1, b=nil, c=nil>
1217
- Example.with_keywords c: 1 # #<struct a=nil, b=nil, c=1>
1218
- ----
1219
-
1220
1268
  ===== .with_positions
1221
1269
 
1222
1270
  Answers a struct instance with given positional arguments regardless of
@@ -10,11 +10,11 @@ module Refinements
10
10
 
11
11
  def combinatorial?(other) = !other.empty? && size == union(other).size
12
12
 
13
- def compress = dup.compress!
13
+ def compress = compact.delete_if { |element| element.respond_to?(:empty?) && element.empty? }
14
14
 
15
15
  def compress!
16
- compact!
17
16
  delete_if { |element| element.respond_to?(:empty?) && element.empty? }
17
+ compact!
18
18
  end
19
19
 
20
20
  def excluding(*elements) = self - elements.flatten
@@ -16,13 +16,11 @@ module Refinements
16
16
  refine Hash do
17
17
  import_methods Shared::Enumerables::Many
18
18
 
19
- def compress = dup.compress!
19
+ def compress = compact.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
20
20
 
21
21
  def compress!
22
- return self if empty?
23
-
24
- compact!
25
22
  delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
23
+ compact!
26
24
  end
27
25
 
28
26
  def deep_merge other
@@ -47,37 +45,25 @@ module Refinements
47
45
 
48
46
  def deep_symbolize_keys! = replace(deep_symbolize_keys)
49
47
 
50
- def fetch_value(key, *default_value, &)
51
- fetch(key, *default_value, &) || default_value.first
52
- end
53
-
54
- # :reek:TooManyStatements
55
- def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
56
- fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
48
+ def fetch_value(key, *default_value, &) = fetch(key, *default_value, &) || default_value.first
57
49
 
58
- reduce({}) do |flat, (key, value)|
59
- flat_key = prefix ? "#{prefix}#{delimiter}#{key}" : key
50
+ def flatten_keys prefix: nil, delimiter: "_"
51
+ reduce({}) do |accumulator, (key, value)|
52
+ flat_key = prefix ? "#{prefix}#{delimiter}#{key}".to_sym : key
60
53
 
61
- next flat.merge flat_key.public_send(cast) => value unless value.is_a? self.class
54
+ next accumulator.merge flat_key => value unless value in Hash
62
55
 
63
- flat.merge(
64
- recurse { value.flatten_keys prefix: flat_key, delimiter:, cast: }
65
- )
56
+ accumulator.merge(recurse { value.flatten_keys prefix: flat_key, delimiter: })
66
57
  end
67
58
  end
68
59
 
69
- def flatten_keys! prefix: nil, delimiter: "_", cast: :to_sym
70
- replace flatten_keys(prefix:, delimiter:, cast:)
71
- end
60
+ def flatten_keys!(prefix: nil, delimiter: "_") = replace flatten_keys(prefix:, delimiter:)
72
61
 
73
62
  def recurse &block
74
63
  return self unless block
75
64
 
76
65
  transform = yield self
77
-
78
- transform.each do |key, value|
79
- transform[key] = value.recurse(&block) if value.is_a? self.class
80
- end
66
+ transform.each { |key, value| transform[key] = value.recurse(&block) if value in Hash }
81
67
  end
82
68
 
83
69
  def stringify_keys = transform_keys(&:to_s)
@@ -88,6 +74,13 @@ module Refinements
88
74
 
89
75
  def symbolize_keys! = transform_keys!(&:to_sym)
90
76
 
77
+ def transform_with(operations) = dup.transform_with! operations
78
+
79
+ def transform_with! operations
80
+ operations.each { |key, function| self[key] = function.call self[key] if key? key }
81
+ self
82
+ end
83
+
91
84
  def use &block
92
85
  return [] unless block
93
86
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "stringio"
4
3
  require "refinements/shared/ios/reread"
4
+ require "stringio"
5
5
 
6
6
  module Refinements
7
7
  # Provides additional enhancements to the StringIO primitive.
@@ -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
 
@@ -4,13 +4,6 @@ module Refinements
4
4
  # Provides additional enhancements to the Struct primitive.
5
5
  module Structs
6
6
  refine Struct.singleton_class do
7
- def keyworded?
8
- warn "[DEPRECATION]: .keyworded? is deprecated, use .keyword_init? instead."
9
- inspect.include? "keyword_init: true"
10
- end
11
-
12
- def with_keywords(**arguments) = keyword_init? ? new(**arguments) : new.merge!(**arguments)
13
-
14
7
  def with_positions(*values) = keyword_init? ? new(**members.zip(values).to_h) : new(*values)
15
8
  end
16
9
 
data/lib/refinements.rb CHANGED
@@ -5,10 +5,10 @@ require "refinements/big_decimals"
5
5
  require "refinements/date_times"
6
6
  require "refinements/hashes"
7
7
  require "refinements/ios"
8
+ require "refinements/log_devices"
9
+ require "refinements/loggers"
8
10
  require "refinements/pathnames"
9
- require "refinements/strings"
10
11
  require "refinements/string_ios"
12
+ require "refinements/strings"
11
13
  require "refinements/structs"
12
14
  require "refinements/symbols"
13
- require "refinements/log_devices"
14
- require "refinements/loggers"
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 = "10.0.0"
6
6
  spec.authors = ["Brooke Kuhlmann"]
7
7
  spec.email = ["brooke@alchemists.io"]
8
8
  spec.homepage = "https://www.alchemists.io/projects/refinements"
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
  spec.signing_key = Gem.default_key_path
23
23
  spec.cert_chain = [Gem.default_cert_path]
24
24
 
25
- spec.required_ruby_version = "~> 3.1"
25
+ spec.required_ruby_version = "~> 3.2"
26
26
 
27
27
  spec.files = Dir["*.gemspec", "lib/**/*"]
28
28
  spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
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: 10.0.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-12-25 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -76,14 +76,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
76
  requirements:
77
77
  - - "~>"
78
78
  - !ruby/object:Gem::Version
79
- version: '3.1'
79
+ version: '3.2'
80
80
  required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - ">="
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.3.24
86
+ rubygems_version: 3.3.26
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