refinements 9.4.0 → 9.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 48645ddfcd7e2ab3a7930401d2161dd259de6bf89ac2f5dbdf8f32015a9c9ed2
4
- data.tar.gz: 8788e3eef62a5856f9136b38b6b76fe9d4c5a8db56b39394f8021bb6ea1db36d
3
+ metadata.gz: bfcbba638db46745a2a7bed58489bea5a2942fb8d414d6803696ee78d12b8ae2
4
+ data.tar.gz: bf5d97f24387a1b46c90b2ebce6e20a66a8ef7f817b60f7029c58ef7d8897236
5
5
  SHA512:
6
- metadata.gz: 51c14c8e25b187ae86630f6c8c017cae3f53f16a4a1a72a9cbdfd3d5e2e83fe1e1bb07b2eacfb4865e35c5bd32092021f4758fdc454be900f42477a8f1a5f190
7
- data.tar.gz: 8b4a378f5bd2e1eec9a3ca9c1bf72c0a5e3773dbe3ece7bebf5ac4bf3be815bd35b25ddc918de5a51116e6d43c98d4b0e54b423beaa2e1532a48645f477337b5
6
+ metadata.gz: 72fc04e99a5e2cc9f6d7adbc2b7c9d461f6d3246249eb1615379d2f64f871f5066c27bdedc95bda42cd44e247e4c793514f6885e11a497ce70980763bb00cb32
7
+ data.tar.gz: 16be117393cd04565ca945c1812e25d63169f7cb5400f4fa11bfe1aac27cbe3061deeeac6fb0e160c2538cf1f274cd1998e7e296f7c9e6e3cde39a0c5a822307
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -21,6 +21,8 @@ Enhances the following objects:
21
21
  * DateTime
22
22
  * Hash
23
23
  * IO
24
+ * LogDevice
25
+ * Logger
24
26
  * Pathname
25
27
  * String
26
28
  * StringIO
@@ -73,6 +75,8 @@ require "refinements/strings"
73
75
  require "refinements/string_ios"
74
76
  require "refinements/structs"
75
77
  require "refinements/symbols"
78
+ require "refinements/log_devices"
79
+ require "refinements/loggers"
76
80
  ----
77
81
 
78
82
  === Using
@@ -93,6 +97,8 @@ class Example
93
97
  using Refinements::StringIOs
94
98
  using Refinements::Structs
95
99
  using Refinements::Symbols
100
+ using Refinements::LogDevices
101
+ using Refinements::Loggers
96
102
  end
97
103
  ----
98
104
 
@@ -102,6 +108,24 @@ The following sections demonstrate how each refinement enriches your objects wit
102
108
 
103
109
  ==== Array
104
110
 
111
+ ===== #combinatorial?
112
+
113
+ Answers if an array is equal to another array when the elements are equal but in any order and/or subset.
114
+
115
+ [source,ruby]
116
+ ----
117
+ example = %w[a b c]
118
+
119
+ example.combinatorial? %w[a b c] # true
120
+ example.combinatorial? %w[c a b] # true
121
+ example.combinatorial? %w[c] # true
122
+ example.combinatorial? %w[c b] # true
123
+ example.combinatorial? %w[x] # false
124
+ example.combinatorial? %w[z b c] # false
125
+ example.combinatorial? %w[a b c d] # false
126
+ example.combinatorial? [] # false
127
+ ----
128
+
105
129
  ===== #compress
106
130
 
107
131
  Removes `nil` and empty objects without mutating itself.
@@ -254,6 +278,45 @@ example.ring { |(before, current, after)| puts "#{before} #{current} #{after}" }
254
278
  # [2 3 1]
255
279
  ----
256
280
 
281
+ ===== #supplant
282
+
283
+ Answers mutated array where first target element found is replaced by single or multiple elements.
284
+
285
+ [source,ruby]
286
+ ----
287
+ %i[a b a].supplant :a, :z # [:z, :b, :a]
288
+ %i[a b a].supplant :a, :z, :y # [:z, :y, :b, :a]
289
+ %i[a b a].supplant :a, %i[z y] # [[:z, :y], :b, :a]
290
+ ----
291
+
292
+ ===== #supplant_if
293
+
294
+ Answers mutated array where all target elements are replaced by single or multiple elements.
295
+
296
+ ⚠️ Be aware that this can be an expensive operation on large arrays.
297
+
298
+ [source,ruby]
299
+ ----
300
+ %i[a b a].supplant_if :a, :z # [:z, :b, :z]
301
+ %i[a b a].supplant_if :a, :z, :y # [:z, :y, :b, :z, :y]
302
+ %i[a b a].supplant_if :a, %i[z y] # [[:z, :y], :b, [:z, :y]]
303
+ ----
304
+
305
+ ===== #to_sentence
306
+
307
+ Answers a sentence using `", "` as the default delimiter and `"and"` as the default conjunction.
308
+ Useful when building documentation, answering human readable error messages, etc.
309
+
310
+ [source,ruby]
311
+ ----
312
+ [].to_sentence # ""
313
+ ["test"].to_sentence # "test"
314
+ ["a", :b].to_sentence # "a and b"
315
+ [1, "a", :b, 2.0, /\w+/].map(&:inspect).to_sentence # 1, "a", :b, 2.0, and /\w+/
316
+ %w[one two three].to_sentence # "one, two, and three"
317
+ %w[eins zwei drei].to_sentence delimiter: " ", conjunction: "und" # "eins zwei und drei"
318
+ ----
319
+
257
320
  ==== Big Decimal
258
321
 
259
322
  ===== #inspect
@@ -581,6 +644,54 @@ io.squelch { io.write "Test" } # "#<IO:fd 20>"
581
644
  io.reread # ""
582
645
  ----
583
646
 
647
+ ==== LogDevice
648
+
649
+ ===== #reread
650
+
651
+ Answers previously written content by rewinding to beginning of device.
652
+
653
+ [source,ruby]
654
+ ----
655
+ # With File.
656
+ device = Logger::LogDevice.new "test.log"
657
+ device.write "Test."
658
+ device.reread # "Test."
659
+
660
+ # With StringIO.
661
+ device = Logger::LogDevice.new StringIO.new
662
+ device.write "Test."
663
+ device.reread # "Test."
664
+
665
+ # With STDOUT.
666
+ device = Logger::LogDevice.new $stdout
667
+ device.write "Test."
668
+ device.reread # ""
669
+ ----
670
+
671
+ ==== Logger
672
+
673
+ ===== #reread
674
+
675
+ Answers previously written content by rewinding to beginning of log.
676
+
677
+ [source,ruby]
678
+ ----
679
+ # With File.
680
+ logger = Logger.new "test.log"
681
+ logger.write "Test."
682
+ logger.reread # "Test."
683
+
684
+ # With StringIO.
685
+ logger = Logger.new StringIO.new
686
+ logger.write "Test."
687
+ logger.reread # "Test."
688
+
689
+ # With STDOUT.
690
+ logger = Logger.new $stdout
691
+ logger.write "Test."
692
+ logger.reread # ""
693
+ ----
694
+
584
695
  ==== Pathname
585
696
 
586
697
  ===== Pathname
@@ -8,6 +8,8 @@ module Refinements
8
8
  refine Array do
9
9
  import_methods Shared::Enumerables::Many
10
10
 
11
+ def combinatorial?(other) = !other.empty? && size == union(other).size
12
+
11
13
  def compress = dup.compress!
12
14
 
13
15
  def compress!
@@ -32,6 +34,28 @@ module Refinements
32
34
  def pad(value, max: size) = dup.fill(value, size..(max - 1))
33
35
 
34
36
  def ring(&) = [last, *self, first].each_cons(3, &)
37
+
38
+ def supplant target, *replacements
39
+ index(target).then do |position|
40
+ delete_at position
41
+ insert position, *replacements
42
+ end
43
+
44
+ self
45
+ end
46
+
47
+ def supplant_if target, *replacements
48
+ each { |item| supplant target, *replacements if item == target }
49
+ self
50
+ end
51
+
52
+ def to_sentence delimiter: ", ", conjunction: "and"
53
+ case length
54
+ when (3..) then "#{self[..-2].join delimiter}#{delimiter}#{conjunction} #{last}"
55
+ when 2 then join " #{conjunction} "
56
+ else join
57
+ end
58
+ end
35
59
  end
36
60
  end
37
61
  end
@@ -21,7 +21,8 @@ module Refinements
21
21
  def compress!
22
22
  return self if empty?
23
23
 
24
- compact!.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
24
+ compact!
25
+ delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
25
26
  end
26
27
 
27
28
  def deep_merge other
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "refinements/shared/ios/reread"
4
+
3
5
  module Refinements
4
6
  # Provides additional enhancements to the IO primitive.
5
7
  module IOs
@@ -15,6 +17,8 @@ module Refinements
15
17
  end
16
18
 
17
19
  refine IO do
20
+ import_methods Shared::IOs::Reread
21
+
18
22
  def redirect other
19
23
  return self unless block_given?
20
24
 
@@ -24,8 +28,6 @@ module Refinements
24
28
  reopen backup
25
29
  end
26
30
 
27
- def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
28
-
29
31
  def squelch(&) = self.class.void.then { |void| redirect(void, &) }
30
32
  end
31
33
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+ require "refinements/string_ios"
5
+
6
+ module Refinements
7
+ # Provides additional enhancements to a log device.
8
+ module LogDevices
9
+ using StringIOs
10
+
11
+ refine Logger::LogDevice do
12
+ def reread
13
+ case dev
14
+ when File then dev.class.new(dev).read
15
+ when StringIO then dev.reread
16
+ else ""
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "refinements/log_devices"
4
+
5
+ module Refinements
6
+ # Provides additional enhancements to a logger.
7
+ module Loggers
8
+ using LogDevices
9
+
10
+ refine Logger do
11
+ def reread = @logdev.reread
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Refinements
4
+ module Shared
5
+ module IOs
6
+ # Provides shared functionality for I/O object rewinding.
7
+ module Reread
8
+ def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "stringio"
4
+ require "refinements/shared/ios/reread"
4
5
 
5
6
  module Refinements
6
7
  # Provides additional enhancements to the StringIO primitive.
7
8
  module StringIOs
8
9
  refine StringIO do
9
- def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
10
+ import_methods Shared::IOs::Reread
10
11
  end
11
12
  end
12
13
  end
data/lib/refinements.rb CHANGED
@@ -10,3 +10,5 @@ require "refinements/strings"
10
10
  require "refinements/string_ios"
11
11
  require "refinements/structs"
12
12
  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.4.0"
5
+ spec.version = "9.7.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.4.0
4
+ version: 9.7.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-05-07 00:00:00.000000000 Z
31
+ date: 2022-09-01 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -47,8 +47,11 @@ files:
47
47
  - lib/refinements/date_times.rb
48
48
  - lib/refinements/hashes.rb
49
49
  - lib/refinements/ios.rb
50
+ - lib/refinements/log_devices.rb
51
+ - lib/refinements/loggers.rb
50
52
  - lib/refinements/pathnames.rb
51
53
  - lib/refinements/shared/enumerables/many.rb
54
+ - lib/refinements/shared/ios/reread.rb
52
55
  - lib/refinements/string_ios.rb
53
56
  - lib/refinements/strings.rb
54
57
  - lib/refinements/structs.rb
@@ -80,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
83
  - !ruby/object:Gem::Version
81
84
  version: '0'
82
85
  requirements: []
83
- rubygems_version: 3.3.13
86
+ rubygems_version: 3.3.21
84
87
  signing_key:
85
88
  specification_version: 4
86
89
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
@@ -1,4 +1,3 @@
1
- �B|�7�3����j�t5��|-w�Mp��������INxA�W)�R�J�@z���@����KN
2
- 9tDC�����1|I��}{m�ƨb�B8
3
- ozsl��$/�����!p�#I���q��hFqw��q��l��H����5f�LE�P��yN��=�Rw廔���A��B6����ȿ14��������#��
4
- ��p���s=%�B)ע)��2,��uG�̛��,~k �L��>�����}?�[�8�
1
+ ;�����^�Ǐ"���˾�S���l��}:������q\�������k�����+���鰩�
2
+ 7��I�4������0�{�=���y�̳>&��O�!w
3
+ b����e�5pѨ�[PXw��76>�^n�V��٫��ejFE��&��ek���2��.� ��Vr2�� vM�?~�F1����h-�!�P����Y�/�nqt*������?i(]?9<������,������<��u\?���{%�P�O