refinements 7.10.0 → 7.15.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 +0 -0
- data.tar.gz.sig +0 -0
- data/LICENSE.adoc +1 -1
- data/README.adoc +374 -159
- data/lib/refinements.rb +2 -0
- data/lib/refinements/arrays.rb +20 -2
- data/lib/refinements/hashes.rb +45 -45
- data/lib/refinements/identity.rb +1 -1
- data/lib/refinements/ios.rb +35 -0
- data/lib/refinements/pathnames.rb +35 -5
- data/lib/refinements/strings.rb +29 -29
- data/lib/refinements/structs.rb +16 -0
- metadata +32 -16
- metadata.gz.sig +0 -0
data/lib/refinements.rb
CHANGED
@@ -6,6 +6,8 @@ require "refinements/big_decimals"
|
|
6
6
|
require "refinements/date_times"
|
7
7
|
require "refinements/files"
|
8
8
|
require "refinements/hashes"
|
9
|
+
require "refinements/ios"
|
9
10
|
require "refinements/pathnames"
|
10
11
|
require "refinements/strings"
|
11
12
|
require "refinements/string_ios"
|
13
|
+
require "refinements/structs"
|
data/lib/refinements/arrays.rb
CHANGED
@@ -11,12 +11,30 @@ module Refinements
|
|
11
11
|
replace compress
|
12
12
|
end
|
13
13
|
|
14
|
+
def exclude *elements
|
15
|
+
warn "[DEPRECATION]: #exclude is deprecated, use #excluding instead."
|
16
|
+
excluding(*elements)
|
17
|
+
end
|
18
|
+
|
19
|
+
def excluding *elements
|
20
|
+
self - elements.flatten
|
21
|
+
end
|
22
|
+
|
14
23
|
def include *elements
|
24
|
+
warn "[DEPRECATION]: #include is deprecated, use #including instead."
|
25
|
+
including(*elements)
|
26
|
+
end
|
27
|
+
|
28
|
+
def including *elements
|
15
29
|
self + elements.flatten
|
16
30
|
end
|
17
31
|
|
18
|
-
def
|
19
|
-
|
32
|
+
def intersperse *elements
|
33
|
+
product([elements]).tap(&:pop).flatten.push last
|
34
|
+
end
|
35
|
+
|
36
|
+
def mean
|
37
|
+
size.zero? ? 0 : sum(0) / size
|
20
38
|
end
|
21
39
|
|
22
40
|
def ring &block
|
data/lib/refinements/hashes.rb
CHANGED
@@ -13,49 +13,6 @@ 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
|
-
# :reek:TooManyStatements
|
25
|
-
def flatten_keys prefix: nil, delimiter: "_", cast: :to_sym
|
26
|
-
fail StandardError, "Unknown cast: #{cast}." unless %i[to_sym to_s].include? cast
|
27
|
-
|
28
|
-
reduce({}) do |flat, (key, value)|
|
29
|
-
flat_key = prefix ? "#{prefix}#{delimiter}#{key}" : key
|
30
|
-
|
31
|
-
next flat.merge flat_key.public_send(cast) => value unless value.is_a? self.class
|
32
|
-
|
33
|
-
flat.merge(
|
34
|
-
recurse { value.flatten_keys prefix: flat_key, delimiter: delimiter, cast: cast }
|
35
|
-
)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
def flatten_keys! prefix: nil, delimiter: "_", cast: :to_sym
|
40
|
-
replace flatten_keys(prefix: prefix, delimiter: delimiter, cast: cast)
|
41
|
-
end
|
42
|
-
|
43
|
-
def stringify_keys
|
44
|
-
reduce({}) { |hash, (key, value)| hash.merge key.to_s => value }
|
45
|
-
end
|
46
|
-
|
47
|
-
def stringify_keys!
|
48
|
-
replace stringify_keys
|
49
|
-
end
|
50
|
-
|
51
|
-
def symbolize_keys
|
52
|
-
reduce({}) { |hash, (key, value)| hash.merge key.to_sym => value }
|
53
|
-
end
|
54
|
-
|
55
|
-
def symbolize_keys!
|
56
|
-
replace symbolize_keys
|
57
|
-
end
|
58
|
-
|
59
16
|
def deep_merge other
|
60
17
|
clazz = self.class
|
61
18
|
|
@@ -88,8 +45,35 @@ module Refinements
|
|
88
45
|
replace deep_symbolize_keys
|
89
46
|
end
|
90
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)
|
73
|
+
end
|
74
|
+
|
91
75
|
def recurse &block
|
92
|
-
return self unless
|
76
|
+
return self unless block
|
93
77
|
|
94
78
|
transform = yield self
|
95
79
|
|
@@ -118,8 +102,24 @@ module Refinements
|
|
118
102
|
replace reverse_merge(other)
|
119
103
|
end
|
120
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
|
119
|
+
end
|
120
|
+
|
121
121
|
def use &block
|
122
|
-
return [] unless
|
122
|
+
return [] unless block
|
123
123
|
|
124
124
|
block.parameters
|
125
125
|
.map { |(_type, key)| self[key] }
|
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,8 @@ module Refinements
|
|
13
13
|
end
|
14
14
|
|
15
15
|
refine Pathname do
|
16
|
-
def
|
17
|
-
|
16
|
+
def change_dir &block
|
17
|
+
block ? Dir.chdir(self, &block) : (Dir.chdir self and self)
|
18
18
|
end
|
19
19
|
|
20
20
|
def copy to
|
@@ -39,6 +39,28 @@ module Refinements
|
|
39
39
|
self.class.new to_s.gsub(pattern, replacement)
|
40
40
|
end
|
41
41
|
|
42
|
+
def make_ancestors
|
43
|
+
dirname.mkpath
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
def make_dir
|
48
|
+
exist? ? self : mkdir and self
|
49
|
+
end
|
50
|
+
|
51
|
+
def make_path
|
52
|
+
mkpath
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def mkdir
|
57
|
+
exist? ? self : super and self
|
58
|
+
end
|
59
|
+
|
60
|
+
def name
|
61
|
+
basename extname
|
62
|
+
end
|
63
|
+
|
42
64
|
def relative_parent root_dir
|
43
65
|
relative_path_from(root_dir).parent
|
44
66
|
end
|
@@ -49,20 +71,28 @@ module Refinements
|
|
49
71
|
relative_parent root_dir
|
50
72
|
end
|
51
73
|
|
52
|
-
def
|
53
|
-
|
74
|
+
def remove_dir
|
75
|
+
exist? ? (rmdir and self) : self
|
76
|
+
end
|
77
|
+
|
78
|
+
def remove_tree
|
79
|
+
rmtree if exist?
|
54
80
|
self
|
55
81
|
end
|
56
82
|
|
57
83
|
def rewrite
|
58
84
|
read.then { |content| write yield(content) if block_given? }
|
59
|
-
self
|
60
85
|
end
|
61
86
|
|
62
87
|
def touch at: Time.now
|
63
88
|
exist? ? utime(at, at) : write("")
|
64
89
|
self
|
65
90
|
end
|
91
|
+
|
92
|
+
def write content, offset: nil, **options
|
93
|
+
super content, offset, options
|
94
|
+
self
|
95
|
+
end
|
66
96
|
end
|
67
97
|
end
|
68
98
|
end
|
data/lib/refinements/strings.rb
CHANGED
@@ -12,42 +12,33 @@ module Refinements
|
|
12
12
|
end
|
13
13
|
|
14
14
|
refine String do
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
max = Integer number
|
15
|
+
def blank?
|
16
|
+
match?(/\A\s*\z/)
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
return
|
19
|
+
def camelcase
|
20
|
+
return up unless match? DELIMITERS
|
22
21
|
|
23
|
-
|
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 }
|
24
25
|
end
|
25
26
|
|
26
|
-
def
|
27
|
+
def down
|
27
28
|
return self if empty?
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
return self[size - 1] if min.zero?
|
32
|
-
return "" if min.negative?
|
33
|
-
|
34
|
-
self[(min + 1)..]
|
35
|
-
end
|
36
|
-
|
37
|
-
def blank?
|
38
|
-
match?(/\A\s*\z/)
|
30
|
+
first.downcase + self[1, size]
|
39
31
|
end
|
40
32
|
|
41
|
-
def
|
33
|
+
def first number = 0
|
42
34
|
return self if empty?
|
43
35
|
|
44
|
-
|
45
|
-
end
|
36
|
+
max = Integer number
|
46
37
|
|
47
|
-
|
48
|
-
return
|
38
|
+
return self[0] if max.zero?
|
39
|
+
return "" if max.negative?
|
49
40
|
|
50
|
-
|
41
|
+
self[..(max - 1)]
|
51
42
|
end
|
52
43
|
|
53
44
|
def indent multiplier = 1, padding: " "
|
@@ -56,12 +47,15 @@ module Refinements
|
|
56
47
|
padding * multiplier + self
|
57
48
|
end
|
58
49
|
|
59
|
-
def
|
60
|
-
return
|
50
|
+
def last number = 0
|
51
|
+
return self if empty?
|
61
52
|
|
62
|
-
|
63
|
-
|
64
|
-
|
53
|
+
min = Integer number
|
54
|
+
|
55
|
+
return self[size - 1] if min.zero?
|
56
|
+
return "" if min.negative?
|
57
|
+
|
58
|
+
self[(min + 1)..]
|
65
59
|
end
|
66
60
|
|
67
61
|
def snakecase
|
@@ -84,6 +78,12 @@ module Refinements
|
|
84
78
|
%w[true yes on t y 1].include? downcase.strip
|
85
79
|
end
|
86
80
|
|
81
|
+
def up
|
82
|
+
return self if empty?
|
83
|
+
|
84
|
+
first.upcase + self[1, size]
|
85
|
+
end
|
86
|
+
|
87
87
|
private
|
88
88
|
|
89
89
|
# :reek:DuplicateMethodCall
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Refinements
|
4
|
+
module Structs
|
5
|
+
refine Struct do
|
6
|
+
def merge **attributes
|
7
|
+
dup.merge! attributes
|
8
|
+
end
|
9
|
+
|
10
|
+
def merge! **attributes
|
11
|
+
to_h.merge(attributes).each { |key, value| self[key] = value }
|
12
|
+
self
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
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.15.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-21 00:00:00.000000000 Z
|
32
32
|
dependencies:
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: bundler-audit
|
@@ -36,42 +36,56 @@ dependencies:
|
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '0.
|
39
|
+
version: '0.7'
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version: '0.
|
46
|
+
version: '0.7'
|
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
|
50
64
|
requirements:
|
51
65
|
- - "~>"
|
52
66
|
- !ruby/object:Gem::Version
|
53
|
-
version: '14.
|
67
|
+
version: '14.8'
|
54
68
|
type: :development
|
55
69
|
prerelease: false
|
56
70
|
version_requirements: !ruby/object:Gem::Requirement
|
57
71
|
requirements:
|
58
72
|
- - "~>"
|
59
73
|
- !ruby/object:Gem::Version
|
60
|
-
version: '14.
|
74
|
+
version: '14.8'
|
61
75
|
- !ruby/object:Gem::Dependency
|
62
76
|
name: git-lint
|
63
77
|
requirement: !ruby/object:Gem::Requirement
|
64
78
|
requirements:
|
65
79
|
- - "~>"
|
66
80
|
- !ruby/object:Gem::Version
|
67
|
-
version: '1.
|
81
|
+
version: '1.3'
|
68
82
|
type: :development
|
69
83
|
prerelease: false
|
70
84
|
version_requirements: !ruby/object:Gem::Requirement
|
71
85
|
requirements:
|
72
86
|
- - "~>"
|
73
87
|
- !ruby/object:Gem::Version
|
74
|
-
version: '1.
|
88
|
+
version: '1.3'
|
75
89
|
- !ruby/object:Gem::Dependency
|
76
90
|
name: guard-rspec
|
77
91
|
requirement: !ruby/object:Gem::Requirement
|
@@ -148,42 +162,42 @@ dependencies:
|
|
148
162
|
requirements:
|
149
163
|
- - "~>"
|
150
164
|
- !ruby/object:Gem::Version
|
151
|
-
version: '3.
|
165
|
+
version: '3.10'
|
152
166
|
type: :development
|
153
167
|
prerelease: false
|
154
168
|
version_requirements: !ruby/object:Gem::Requirement
|
155
169
|
requirements:
|
156
170
|
- - "~>"
|
157
171
|
- !ruby/object:Gem::Version
|
158
|
-
version: '3.
|
172
|
+
version: '3.10'
|
159
173
|
- !ruby/object:Gem::Dependency
|
160
174
|
name: rubocop
|
161
175
|
requirement: !ruby/object:Gem::Requirement
|
162
176
|
requirements:
|
163
177
|
- - "~>"
|
164
178
|
- !ruby/object:Gem::Version
|
165
|
-
version: '
|
179
|
+
version: '1.3'
|
166
180
|
type: :development
|
167
181
|
prerelease: false
|
168
182
|
version_requirements: !ruby/object:Gem::Requirement
|
169
183
|
requirements:
|
170
184
|
- - "~>"
|
171
185
|
- !ruby/object:Gem::Version
|
172
|
-
version: '
|
186
|
+
version: '1.3'
|
173
187
|
- !ruby/object:Gem::Dependency
|
174
188
|
name: rubocop-performance
|
175
189
|
requirement: !ruby/object:Gem::Requirement
|
176
190
|
requirements:
|
177
191
|
- - "~>"
|
178
192
|
- !ruby/object:Gem::Version
|
179
|
-
version: '1.
|
193
|
+
version: '1.8'
|
180
194
|
type: :development
|
181
195
|
prerelease: false
|
182
196
|
version_requirements: !ruby/object:Gem::Requirement
|
183
197
|
requirements:
|
184
198
|
- - "~>"
|
185
199
|
- !ruby/object:Gem::Version
|
186
|
-
version: '1.
|
200
|
+
version: '1.8'
|
187
201
|
- !ruby/object:Gem::Dependency
|
188
202
|
name: rubocop-rake
|
189
203
|
requirement: !ruby/object:Gem::Requirement
|
@@ -204,14 +218,14 @@ dependencies:
|
|
204
218
|
requirements:
|
205
219
|
- - "~>"
|
206
220
|
- !ruby/object:Gem::Version
|
207
|
-
version: '
|
221
|
+
version: '2.0'
|
208
222
|
type: :development
|
209
223
|
prerelease: false
|
210
224
|
version_requirements: !ruby/object:Gem::Requirement
|
211
225
|
requirements:
|
212
226
|
- - "~>"
|
213
227
|
- !ruby/object:Gem::Version
|
214
|
-
version: '
|
228
|
+
version: '2.0'
|
215
229
|
- !ruby/object:Gem::Dependency
|
216
230
|
name: simplecov
|
217
231
|
requirement: !ruby/object:Gem::Requirement
|
@@ -244,9 +258,11 @@ 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
|
265
|
+
- lib/refinements/structs.rb
|
250
266
|
homepage: https://www.alchemists.io/projects/refinements
|
251
267
|
licenses:
|
252
268
|
- Apache-2.0
|