refinements 8.1.1 → 8.3.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 +4 -4
- checksums.yaml.gz.sig +0 -0
- data/README.adoc +71 -2
- data/lib/refinements/arrays.rb +8 -1
- data/lib/refinements/big_decimals.rb +1 -0
- data/lib/refinements/date_times.rb +1 -0
- data/lib/refinements/hashes.rb +11 -4
- data/lib/refinements/identity.rb +2 -2
- data/lib/refinements/ios.rb +1 -0
- data/lib/refinements/pathnames.rb +1 -1
- data/lib/refinements/string_ios.rb +1 -0
- data/lib/refinements/strings.rb +3 -2
- data/lib/refinements/structs.rb +2 -3
- 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: d389fcb07d681440a24f0b368c81a76948e15b71287a850e857f8e018fc973da
|
4
|
+
data.tar.gz: 3f7a815dbc49376aad77ab549e77bf9e607bec74ff136b537dfccc371649fe4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
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
|
data/lib/refinements/arrays.rb
CHANGED
@@ -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
|
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
|
|
data/lib/refinements/hashes.rb
CHANGED
@@ -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 =
|
72
|
+
def stringify_keys = transform_keys(&:to_s)
|
66
73
|
|
67
|
-
def stringify_keys! =
|
74
|
+
def stringify_keys! = transform_keys!(&:to_s)
|
68
75
|
|
69
|
-
def symbolize_keys =
|
76
|
+
def symbolize_keys = transform_keys(&:to_sym)
|
70
77
|
|
71
|
-
def symbolize_keys! =
|
78
|
+
def symbolize_keys! = transform_keys!(&:to_sym)
|
72
79
|
|
73
80
|
def use &block
|
74
81
|
return [] unless block
|
data/lib/refinements/identity.rb
CHANGED
data/lib/refinements/ios.rb
CHANGED
data/lib/refinements/strings.rb
CHANGED
@@ -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
|
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
|
data/lib/refinements/structs.rb
CHANGED
@@ -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
|
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.
|
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-
|
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.
|
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
|