refinements 8.1.1 → 8.3.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: c04df598103624b71b08d5b175c319d21c9fa5ff773976c5956f6e1aa0acdb70
4
- data.tar.gz: d6b9d57e2a23a74d1bed6e98146590e1bdc4f4a843ea9ed72747b6ccd3d847bc
3
+ metadata.gz: d389fcb07d681440a24f0b368c81a76948e15b71287a850e857f8e018fc973da
4
+ data.tar.gz: 3f7a815dbc49376aad77ab549e77bf9e607bec74ff136b537dfccc371649fe4a
5
5
  SHA512:
6
- metadata.gz: 996ac02a62679e6cd2bd083863ee587d9915a65bee40883bb21875a196d79126eacae345458f4e5f784203e9d26af17a8735c46a6c3e2eacf77adecacea07d08
7
- data.tar.gz: 5a4d11cb1010fbe61137b5e0382568bec9bdb4812d169f68d749e2e9809a033109102631a1568030eef53b6372e5bda4a23d93b3ceb7cef0f164f6b5e9b6a91d
6
+ metadata.gz: d7269907e701c6917bb5c096af4bbf1650d03b8ffb752b56c37baa895e8eff649359246b79e0a19b3b790d6dde60711a06aeb0a37b3da0c080a64bbcc54e96bf
7
+ data.tar.gz: 5cbd5fd8f553c47fa5f77e4145c48af1ac3467927fb09aae8db48639a92c6000c255b75d66d90824474c7dbffd10ef19ee5581e3d5da83f4bb0526d0d8d1a2e1
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -11,7 +11,9 @@ image::https://img.shields.io/badge/code_style-alchemists-brightgreen.svg[Alchem
11
11
  [link=https://circleci.com/gh/bkuhlmann/refinements]
12
12
  image::https://circleci.com/gh/bkuhlmann/refinements.svg?style=svg[Circle CI Status]
13
13
 
14
- A collection of refinements (enhancements) to primitive Ruby objects.
14
+ Refinements is a collection of enhancements to primitive Ruby objects without needing to resort
15
+ painful and hard to debug monkey patches. These refinements give you additional syntactic sugar to
16
+ build clean and concise implementations all while using less code.
15
17
 
16
18
  toc::[]
17
19
 
@@ -135,6 +137,23 @@ Removes given array or elements without mutating itself.
135
137
  [1, 2, 3, 4, 5].excluding 4, 5 # => [1, 2, 3]
136
138
  ----
137
139
 
140
+ ===== #filter_find
141
+
142
+ Answers the first truthy and filtered result from a collection.
143
+
144
+ [source,ruby]
145
+ ----
146
+ handlers = [
147
+ ->(object) { object if object == :b },
148
+ proc { false },
149
+ ->(object) { object if object == :a }
150
+ ]
151
+
152
+ handlers.filter_find # => Enumerator::Lazy
153
+ handlers.filter_find { |handler| handler.call :a } # => :a
154
+ handlers.filter_find { |handler| handler.call :x } # => nil
155
+ ----
156
+
138
157
  ===== #including
139
158
 
140
159
  Adds given array or elements without mutating itself.
@@ -156,6 +175,19 @@ Inserts additional elements or array between all members of given array.
156
175
  [1, 2, 3].intersperse %i[a b c] # => [1, :a, :b, :c, 2, :a, :b, :c, 3]
157
176
  ----
158
177
 
178
+ ===== #maximum
179
+
180
+ Answers the maximum extracted value from a collection of objects.
181
+
182
+ [source,ruby]
183
+ ----
184
+ Point = Struct.new :x, :y, keyword_init: true
185
+ points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
186
+
187
+ points.maximum(:x) # => 2
188
+ points.maximum(:y) # => 3
189
+ ----
190
+
159
191
  ===== #mean
160
192
 
161
193
  Answers mean/average all elements within an array.
@@ -168,6 +200,19 @@ Answers mean/average all elements within an array.
168
200
  [1.25, 1.5, 1.75].mean # => 1.5
169
201
  ----
170
202
 
203
+ ===== #minimum
204
+
205
+ Answers the minimum extracted value from a collection of objects.
206
+
207
+ [source,ruby]
208
+ ----
209
+ Point = Struct.new :x, :y, keyword_init: true
210
+ points = [Point[x: 1, y: 2], Point[x: 0, y: 1], Point[x: 2, y: 3]]
211
+
212
+ points.minimum(:x) # => 0
213
+ points.minimum(:y) # => 1
214
+ ----
215
+
171
216
  ===== #pad
172
217
 
173
218
  Answers new array padded with given value up to a maximum size. Useful in situations where an array
@@ -243,6 +288,30 @@ example = Hash.with_default []
243
288
  example[:b] # => []
244
289
  ----
245
290
 
291
+ ===== #compress
292
+
293
+ Removes empty objects without mutating itself.
294
+
295
+ [source,ruby]
296
+ ----
297
+ object = Object.new
298
+ example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
299
+ example.compress # => {a: 1, b: "blueberry", g: object}
300
+ example # => {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
301
+ ----
302
+
303
+ ===== #compress!
304
+
305
+ Removes empty objects while mutating itself.
306
+
307
+ [source,ruby]
308
+ ----
309
+ object = Object.new
310
+ example = {a: 1, b: "blueberry", c: nil, d: "", e: [], f: {}, g: object}
311
+ example.compress! # => {a: 1, b: "blueberry", g: object}
312
+ example # => {a: 1, b: "blueberry", g: object}
313
+ ----
314
+
246
315
  ===== #deep_merge
247
316
 
248
317
  Merges deeply nested hashes together without mutating itself.
@@ -492,7 +561,7 @@ Wraps `Dir.mktmpdir` with the following behavior (see
492
561
  link:https://rubyapi.org/o/Dir.mktmpdir#method-c-mktmpdir[Dir.mktmpdir] for details):
493
562
 
494
563
  * *Without Block* - Answers a newly created Pathname instance which is not automatically cleaned up.
495
- * *With Block* Yields a Pathname instance, answers result of given block, and automatidally cleans
564
+ * *With Block* Yields a Pathname instance, answers result of given block, and automatically cleans
496
565
  up temporary directory after block exits.
497
566
 
498
567
  The following examples use truncated temporary directories for illustration purposes only. In
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Array primitive.
4
5
  module Arrays
5
6
  refine Array do
6
7
  def compress = compact.reject(&:empty?)
@@ -9,11 +10,17 @@ module Refinements
9
10
 
10
11
  def excluding(*elements) = self - elements.flatten
11
12
 
13
+ def filter_find(&block) = block ? lazy.map(&block).find(&:itself) : lazy
14
+
12
15
  def including(*elements) = self + elements.flatten
13
16
 
14
17
  def intersperse(*elements) = product([elements]).tap(&:pop).flatten.push(last)
15
18
 
16
- def mean = size.zero? ? 0 : sum(0) / size
19
+ def maximum(key) = map(&key).max
20
+
21
+ def mean = size.zero? ? 0 : sum(0.0) / size
22
+
23
+ def minimum(key) = map(&key).min
17
24
 
18
25
  def pad(value, max: size) = dup.fill(value, size..(max - 1))
19
26
 
@@ -3,6 +3,7 @@
3
3
  require "bigdecimal"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the BigDecimal primitive.
6
7
  module BigDecimals
7
8
  refine BigDecimal do
8
9
  def inspect = format("#<BigDecimal:%{id} %{string}>", id: object_id, string: to_s("F"))
@@ -3,6 +3,7 @@
3
3
  require "date"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the DateTime primitive.
6
7
  module DateTimes
7
8
  refine DateTime.singleton_class do
8
9
  def utc = now.new_offset(0)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Hash primitive.
4
5
  module Hashes
5
6
  refine Hash.singleton_class do
6
7
  def infinite
@@ -11,6 +12,12 @@ module Refinements
11
12
  end
12
13
 
13
14
  refine Hash do
15
+ def compress = dup.compress!
16
+
17
+ def compress!
18
+ compact!.delete_if { |_key, value| value.respond_to?(:empty?) && value.empty? }
19
+ end
20
+
14
21
  def deep_merge other
15
22
  clazz = self.class
16
23
 
@@ -62,13 +69,13 @@ module Refinements
62
69
  end
63
70
  end
64
71
 
65
- def stringify_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
72
+ def stringify_keys = transform_keys(&:to_s)
66
73
 
67
- def stringify_keys! = replace(stringify_keys)
74
+ def stringify_keys! = transform_keys!(&:to_s)
68
75
 
69
- def symbolize_keys = reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
76
+ def symbolize_keys = transform_keys(&:to_sym)
70
77
 
71
- def symbolize_keys! = replace(symbolize_keys)
78
+ def symbolize_keys! = transform_keys!(&:to_sym)
72
79
 
73
80
  def use &block
74
81
  return [] unless block
@@ -5,7 +5,7 @@ module Refinements
5
5
  module Identity
6
6
  NAME = "refinements"
7
7
  LABEL = "Refinements"
8
- VERSION = "8.1.1"
9
- VERSION_LABEL = "#{LABEL} #{VERSION}"
8
+ VERSION = "8.3.0"
9
+ VERSION_LABEL = "#{LABEL} #{VERSION}".freeze
10
10
  end
11
11
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the IO primitive.
4
5
  module IOs
5
6
  refine IO.singleton_class do
6
7
  def void
@@ -3,9 +3,9 @@
3
3
  require "pathname"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the Pathname primitive.
6
7
  module Pathnames
7
8
  refine Kernel do
8
- # :reek:UncommunicativeMethodName
9
9
  def Pathname object
10
10
  return super(String(object)) unless object
11
11
 
@@ -3,6 +3,7 @@
3
3
  require "stringio"
4
4
 
5
5
  module Refinements
6
+ # Provides additional enhancements to the StringIO primitive.
6
7
  module StringIOs
7
8
  refine StringIO do
8
9
  def reread(length = nil, buffer: nil) = tap(&:rewind).read(length, buffer)
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the String primitive.
4
5
  module Strings
5
6
  DELIMITERS = %r([a-z][A-Z]|\s*-\s*|\s*/\s*|\s*:+\s*|\s*_\s*|\s+)
6
7
 
7
8
  refine String do
8
- def blank? = match?(/\A\s*\z/)
9
+ def blank? = empty? || match?(/\A[[:space:]]*\z/)
9
10
 
10
11
  def camelcase
11
12
  return up unless match? DELIMITERS
@@ -35,7 +36,7 @@ module Refinements
35
36
  def indent multiplier = 1, padding: " "
36
37
  return self if multiplier.negative?
37
38
 
38
- padding * multiplier + self
39
+ (padding * multiplier) + self
39
40
  end
40
41
 
41
42
  def last number = 0
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Refinements
4
+ # Provides additional enhancements to the Struct primitive.
4
5
  module Structs
5
6
  refine Struct.singleton_class do
6
7
  def keyworded? = inspect.include?("keyword_init: true")
@@ -21,9 +22,7 @@ module Refinements
21
22
  def revalue attributes = each_pair
22
23
  return self unless block_given?
23
24
 
24
- dup.tap do |copy|
25
- attributes.each { |key, value| copy[key] = yield self[key], value }
26
- end
25
+ dup.tap { |copy| attributes.each { |key, value| copy[key] = yield self[key], value } }
27
26
  end
28
27
 
29
28
  def revalue! attributes = each_pair
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: 8.1.1
4
+ version: 8.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brooke Kuhlmann
@@ -28,7 +28,7 @@ cert_chain:
28
28
  lkHilIrX69jq8wMPpBhlaw2mRmeSL50Wv5u6xVBvOHhXFSP1crXM95vfLhLyRYod
29
29
  W2A=
30
30
  -----END CERTIFICATE-----
31
- date: 2021-04-18 00:00:00.000000000 Z
31
+ date: 2021-09-26 00:00:00.000000000 Z
32
32
  dependencies: []
33
33
  description:
34
34
  email:
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
- rubygems_version: 3.2.16
78
+ rubygems_version: 3.2.28
79
79
  signing_key:
80
80
  specification_version: 4
81
81
  summary: A collection of refinements to core Ruby objects.
metadata.gz.sig CHANGED
Binary file