refinements 7.8.0 → 7.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +1 -3
- data.tar.gz.sig +0 -0
- data/LICENSE.adoc +1 -1
- data/README.adoc +651 -157
- data/lib/refinements.rb +1 -0
- data/lib/refinements/arrays.rb +10 -2
- data/lib/refinements/hashes.rb +59 -24
- data/lib/refinements/identity.rb +1 -1
- data/lib/refinements/ios.rb +35 -0
- data/lib/refinements/pathnames.rb +31 -4
- data/lib/refinements/strings.rb +30 -24
- metadata +19 -4
- metadata.gz.sig +1 -1
data/lib/refinements.rb
CHANGED
data/lib/refinements/arrays.rb
CHANGED
@@ -11,12 +11,20 @@ module Refinements
|
|
11
11
|
replace compress
|
12
12
|
end
|
13
13
|
|
14
|
+
def exclude *elements
|
15
|
+
self - elements.flatten
|
16
|
+
end
|
17
|
+
|
14
18
|
def include *elements
|
15
19
|
self + elements.flatten
|
16
20
|
end
|
17
21
|
|
18
|
-
def
|
19
|
-
|
22
|
+
def intersperse *elements
|
23
|
+
product([elements]).tap(&:pop).flatten.push last
|
24
|
+
end
|
25
|
+
|
26
|
+
def mean
|
27
|
+
size.zero? ? 0 : sum(0) / size
|
20
28
|
end
|
21
29
|
|
22
30
|
def ring &block
|
data/lib/refinements/hashes.rb
CHANGED
@@ -13,30 +13,10 @@ module Refinements
|
|
13
13
|
end
|
14
14
|
|
15
15
|
refine Hash do
|
16
|
-
def except *keys
|
17
|
-
reject { |key, _value| keys.include? key }
|
18
|
-
end
|
19
|
-
|
20
|
-
def except! *keys
|
21
|
-
replace except(*keys)
|
22
|
-
end
|
23
|
-
|
24
|
-
def symbolize_keys
|
25
|
-
reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
|
26
|
-
end
|
27
|
-
|
28
|
-
def symbolize_keys!
|
29
|
-
replace symbolize_keys
|
30
|
-
end
|
31
|
-
|
32
16
|
def deep_merge other
|
33
|
-
dup.deep_merge! other
|
34
|
-
end
|
35
|
-
|
36
|
-
def deep_merge! other
|
37
17
|
clazz = self.class
|
38
18
|
|
39
|
-
merge
|
19
|
+
merge other do |_key, this_value, other_value|
|
40
20
|
if this_value.is_a?(clazz) && other_value.is_a?(clazz)
|
41
21
|
this_value.deep_merge other_value
|
42
22
|
else
|
@@ -45,12 +25,51 @@ module Refinements
|
|
45
25
|
end
|
46
26
|
end
|
47
27
|
|
28
|
+
def deep_merge! other
|
29
|
+
replace deep_merge(other)
|
30
|
+
end
|
31
|
+
|
32
|
+
def deep_stringify_keys
|
33
|
+
recurse(&:stringify_keys)
|
34
|
+
end
|
35
|
+
|
36
|
+
def deep_stringify_keys!
|
37
|
+
replace deep_stringify_keys
|
38
|
+
end
|
39
|
+
|
48
40
|
def deep_symbolize_keys
|
49
41
|
recurse(&:symbolize_keys)
|
50
42
|
end
|
51
43
|
|
52
44
|
def deep_symbolize_keys!
|
53
|
-
|
45
|
+
replace deep_symbolize_keys
|
46
|
+
end
|
47
|
+
|
48
|
+
def except *keys
|
49
|
+
reject { |key, _value| keys.include? key }
|
50
|
+
end
|
51
|
+
|
52
|
+
def except! *keys
|
53
|
+
replace except(*keys)
|
54
|
+
end
|
55
|
+
|
56
|
+
# :reek:TooManyStatements
|
57
|
+
def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
|
58
|
+
fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
|
59
|
+
|
60
|
+
reduce({}) do |flat, (key, value)|
|
61
|
+
flat_key = prefix ? "#{prefix}#{delimiter}#{key}" : key
|
62
|
+
|
63
|
+
next flat.merge flat_key.public_send(cast) => value unless value.is_a? self.class
|
64
|
+
|
65
|
+
flat.merge(
|
66
|
+
recurse { value.flatten_keys prefix: flat_key, delimiter: delimiter, cast: cast }
|
67
|
+
)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def flatten_keys! prefix: nil, delimiter: "_", cast: :to_sym
|
72
|
+
replace flatten_keys(prefix: prefix, delimiter: delimiter, cast: cast)
|
54
73
|
end
|
55
74
|
|
56
75
|
def recurse &block
|
@@ -75,12 +94,28 @@ module Refinements
|
|
75
94
|
|
76
95
|
def reverse_merge other
|
77
96
|
warn "[DEPRECATION]: #reverse_merge is deprecated, use #merge instead."
|
78
|
-
other
|
97
|
+
merge(other) { |_key, old_value, _new_value| old_value }
|
79
98
|
end
|
80
99
|
|
81
100
|
def reverse_merge! other
|
82
101
|
warn "[DEPRECATION]: #reverse_merge! is deprecated, use #merge! instead."
|
83
|
-
|
102
|
+
replace reverse_merge(other)
|
103
|
+
end
|
104
|
+
|
105
|
+
def stringify_keys
|
106
|
+
reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
|
107
|
+
end
|
108
|
+
|
109
|
+
def stringify_keys!
|
110
|
+
replace stringify_keys
|
111
|
+
end
|
112
|
+
|
113
|
+
def symbolize_keys
|
114
|
+
reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
|
115
|
+
end
|
116
|
+
|
117
|
+
def symbolize_keys!
|
118
|
+
replace symbolize_keys
|
84
119
|
end
|
85
120
|
|
86
121
|
def use &block
|
data/lib/refinements/identity.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Refinements
|
4
|
+
module IOs
|
5
|
+
refine IO.singleton_class do
|
6
|
+
def void
|
7
|
+
new(sysopen("/dev/null", "w+")).then do |io|
|
8
|
+
return io unless block_given?
|
9
|
+
|
10
|
+
yield io
|
11
|
+
io.tap(&:close)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
refine IO do
|
17
|
+
def redirect other
|
18
|
+
return self unless block_given?
|
19
|
+
|
20
|
+
backup = dup
|
21
|
+
reopen other
|
22
|
+
yield self
|
23
|
+
reopen backup
|
24
|
+
end
|
25
|
+
|
26
|
+
def reread length = nil, buffer: nil
|
27
|
+
tap(&:rewind).read length, buffer
|
28
|
+
end
|
29
|
+
|
30
|
+
def squelch &block
|
31
|
+
self.class.void.then { |void| redirect(void, &block) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -13,8 +13,9 @@ module Refinements
|
|
13
13
|
end
|
14
14
|
|
15
15
|
refine Pathname do
|
16
|
-
def
|
17
|
-
|
16
|
+
def change_dir &block
|
17
|
+
Dir.chdir(self, &block)
|
18
|
+
self
|
18
19
|
end
|
19
20
|
|
20
21
|
def copy to
|
@@ -39,6 +40,28 @@ module Refinements
|
|
39
40
|
self.class.new to_s.gsub(pattern, replacement)
|
40
41
|
end
|
41
42
|
|
43
|
+
def make_ancestors
|
44
|
+
dirname.mkpath
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def make_dir
|
49
|
+
exist? ? self : mkdir and self
|
50
|
+
end
|
51
|
+
|
52
|
+
def make_path
|
53
|
+
mkpath
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def mkdir
|
58
|
+
exist? ? self : super and self
|
59
|
+
end
|
60
|
+
|
61
|
+
def name
|
62
|
+
basename extname
|
63
|
+
end
|
64
|
+
|
42
65
|
def relative_parent root_dir
|
43
66
|
relative_path_from(root_dir).parent
|
44
67
|
end
|
@@ -49,8 +72,12 @@ module Refinements
|
|
49
72
|
relative_parent root_dir
|
50
73
|
end
|
51
74
|
|
52
|
-
def
|
53
|
-
|
75
|
+
def remove_dir
|
76
|
+
exist? ? (rmdir and self) : self
|
77
|
+
end
|
78
|
+
|
79
|
+
def remove_tree
|
80
|
+
rmtree if exist?
|
54
81
|
self
|
55
82
|
end
|
56
83
|
|
data/lib/refinements/strings.rb
CHANGED
@@ -12,6 +12,24 @@ module Refinements
|
|
12
12
|
end
|
13
13
|
|
14
14
|
refine String do
|
15
|
+
def blank?
|
16
|
+
match?(/\A\s*\z/)
|
17
|
+
end
|
18
|
+
|
19
|
+
def camelcase
|
20
|
+
return up unless match? DELIMITERS
|
21
|
+
|
22
|
+
split(%r(\s*-\s*|\s*/\s*|\s*:+\s*)).then { |parts| combine parts, :up, "::" }
|
23
|
+
.then { |text| text.split(/\s*_\s*|\s+/) }
|
24
|
+
.then { |parts| combine parts, :up }
|
25
|
+
end
|
26
|
+
|
27
|
+
def down
|
28
|
+
return self if empty?
|
29
|
+
|
30
|
+
first.downcase + self[1, size]
|
31
|
+
end
|
32
|
+
|
15
33
|
def first number = 0
|
16
34
|
return self if empty?
|
17
35
|
|
@@ -23,6 +41,12 @@ module Refinements
|
|
23
41
|
self[..(max - 1)]
|
24
42
|
end
|
25
43
|
|
44
|
+
def indent multiplier = 1, padding: " "
|
45
|
+
return self if multiplier.negative?
|
46
|
+
|
47
|
+
padding * multiplier + self
|
48
|
+
end
|
49
|
+
|
26
50
|
def last number = 0
|
27
51
|
return self if empty?
|
28
52
|
|
@@ -34,30 +58,6 @@ module Refinements
|
|
34
58
|
self[(min + 1)..]
|
35
59
|
end
|
36
60
|
|
37
|
-
def blank?
|
38
|
-
match?(/\A\s*\z/)
|
39
|
-
end
|
40
|
-
|
41
|
-
def up
|
42
|
-
return self if empty?
|
43
|
-
|
44
|
-
first.upcase + self[1, size]
|
45
|
-
end
|
46
|
-
|
47
|
-
def down
|
48
|
-
return self if empty?
|
49
|
-
|
50
|
-
first.downcase + self[1, size]
|
51
|
-
end
|
52
|
-
|
53
|
-
def camelcase
|
54
|
-
return up unless match? DELIMITERS
|
55
|
-
|
56
|
-
split(%r(\s*-\s*|\s*/\s*|\s*:+\s*)).then { |parts| combine parts, :up, "::" }
|
57
|
-
.then { |text| text.split(/\s*_\s*|\s+/) }
|
58
|
-
.then { |parts| combine parts, :up }
|
59
|
-
end
|
60
|
-
|
61
61
|
def snakecase
|
62
62
|
return downcase unless match? DELIMITERS
|
63
63
|
|
@@ -78,6 +78,12 @@ module Refinements
|
|
78
78
|
%w[true yes on t y 1].include? downcase.strip
|
79
79
|
end
|
80
80
|
|
81
|
+
def up
|
82
|
+
return self if empty?
|
83
|
+
|
84
|
+
first.upcase + self[1, size]
|
85
|
+
end
|
86
|
+
|
81
87
|
private
|
82
88
|
|
83
89
|
# :reek:DuplicateMethodCall
|
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: 7.
|
4
|
+
version: 7.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brooke Kuhlmann
|
@@ -28,7 +28,7 @@ cert_chain:
|
|
28
28
|
2XV8FRa7/JimI07sPLC13eLY3xd/aYTi85Z782KIA4j0G8XEEWAX0ouBhlXPocZv
|
29
29
|
QWc=
|
30
30
|
-----END CERTIFICATE-----
|
31
|
-
date: 2020-
|
31
|
+
date: 2020-11-07 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler-audit
|
@@ -44,6 +44,20 @@ dependencies:
|
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0.6'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler-leak
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.2'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0.2'
|
47
61
|
- !ruby/object:Gem::Dependency
|
48
62
|
name: gemsmith
|
49
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -218,14 +232,14 @@ dependencies:
|
|
218
232
|
requirements:
|
219
233
|
- - "~>"
|
220
234
|
- !ruby/object:Gem::Version
|
221
|
-
version: '0.
|
235
|
+
version: '0.19'
|
222
236
|
type: :development
|
223
237
|
prerelease: false
|
224
238
|
version_requirements: !ruby/object:Gem::Requirement
|
225
239
|
requirements:
|
226
240
|
- - "~>"
|
227
241
|
- !ruby/object:Gem::Version
|
228
|
-
version: '0.
|
242
|
+
version: '0.19'
|
229
243
|
description:
|
230
244
|
email:
|
231
245
|
- brooke@alchemists.io
|
@@ -244,6 +258,7 @@ files:
|
|
244
258
|
- lib/refinements/files.rb
|
245
259
|
- lib/refinements/hashes.rb
|
246
260
|
- lib/refinements/identity.rb
|
261
|
+
- lib/refinements/ios.rb
|
247
262
|
- lib/refinements/pathnames.rb
|
248
263
|
- lib/refinements/string_ios.rb
|
249
264
|
- lib/refinements/strings.rb
|
metadata.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
��
|
1
|
+
��o����=j4#�e�\Aj%�@��j���i^��3���Cm���}gJ
|