lite-ruby 1.0.14 → 1.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,413 +1,415 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Hash
3
+ if Lite::Ruby.configuration.monkey_patches.include?('hash')
4
+ class Hash
4
5
 
5
- class << self
6
+ class << self
6
7
 
7
- def zip(keys, values)
8
- keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] }
9
- end
8
+ def zip(keys, values)
9
+ keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] }
10
+ end
10
11
 
11
- end
12
+ end
12
13
 
13
- def aka(new_key, old_key)
14
- self[new_key] = self[old_key] if key?(old_key)
15
- self
16
- end
14
+ def aka(new_key, old_key)
15
+ self[new_key] = self[old_key] if key?(old_key)
16
+ self
17
+ end
17
18
 
18
- def assert_valid_keys!(*valid_keys)
19
- each_key do |key|
20
- next if valid_keys.include?(key)
19
+ def assert_valid_keys!(*valid_keys)
20
+ each_key do |key|
21
+ next if valid_keys.include?(key)
21
22
 
22
- raise ArgumentError,
23
- "Invalid key: #{key.inspect}." \
24
- "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
23
+ raise ArgumentError,
24
+ "Invalid key: #{key.inspect}." \
25
+ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
26
+ end
25
27
  end
26
- end
27
28
 
28
- def assert_all_valid_keys!(*valid_keys)
29
- return assert_valid_keys!(*valid_keys) unless empty?
29
+ def assert_all_valid_keys!(*valid_keys)
30
+ return assert_valid_keys!(*valid_keys) unless empty?
30
31
 
31
- raise ArgumentError, 'An empty hash is not allowed'
32
- end
32
+ raise ArgumentError, 'An empty hash is not allowed'
33
+ end
33
34
 
34
- def assert_valid_values!(*valid_values)
35
- each_value do |value|
36
- next if valid_values.include?(value)
35
+ def assert_valid_values!(*valid_values)
36
+ each_value do |value|
37
+ next if valid_values.include?(value)
37
38
 
38
- raise ArgumentError,
39
- "Invalid value: #{value.inspect}." \
40
- "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
39
+ raise ArgumentError,
40
+ "Invalid value: #{value.inspect}." \
41
+ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
42
+ end
41
43
  end
42
- end
43
44
 
44
- def assert_all_valid_values!(*valid_values)
45
- return assert_valid_values!(*valid_values) unless empty?
45
+ def assert_all_valid_values!(*valid_values)
46
+ return assert_valid_values!(*valid_values) unless empty?
46
47
 
47
- raise ArgumentError, 'An empty hash is not allowed'
48
- end
48
+ raise ArgumentError, 'An empty hash is not allowed'
49
+ end
49
50
 
50
- # rubocop:disable Style/GuardClause
51
- def bury(*args)
52
- if args.count < 2
53
- raise ArgumentError, '2 or more arguments required'
54
- elsif args.count == 2
55
- self[args[0]] = args[1]
56
- else
57
- arg = args.shift
58
- self[arg] = {} unless self[arg]
59
- self[arg].bury(*args) unless args.empty?
51
+ # rubocop:disable Style/GuardClause
52
+ def bury(*args)
53
+ if args.count < 2
54
+ raise ArgumentError, '2 or more arguments required'
55
+ elsif args.count == 2
56
+ self[args[0]] = args[1]
57
+ else
58
+ arg = args.shift
59
+ self[arg] = {} unless self[arg]
60
+ self[arg].bury(*args) unless args.empty?
61
+ end
62
+
63
+ self
60
64
  end
65
+ # rubocop:enable Style/GuardClause
61
66
 
62
- self
63
- end
64
- # rubocop:enable Style/GuardClause
67
+ # rubocop:disable Metrics/MethodLength
68
+ def collate(*others)
69
+ hash = {}
65
70
 
66
- # rubocop:disable Metrics/MethodLength
67
- def collate(*others)
68
- hash = {}
71
+ each_key { |key| hash[key] = [] }
69
72
 
70
- each_key { |key| hash[key] = [] }
73
+ others.each do |other|
74
+ other.each_key { |key| hash[key] = [] }
75
+ end
71
76
 
72
- others.each do |other|
73
- other.each_key { |key| hash[key] = [] }
74
- end
77
+ each { |key, val| hash[key] << val }
75
78
 
76
- each { |key, val| hash[key] << val }
79
+ others.each do |other|
80
+ other.each { |key, val| hash[key] << val }
81
+ end
77
82
 
78
- others.each do |other|
79
- other.each { |key, val| hash[key] << val }
83
+ hash.each_value(&:flatten!)
84
+ hash
80
85
  end
86
+ # rubocop:enable Metrics/MethodLength
81
87
 
82
- hash.each_value(&:flatten!)
83
- hash
84
- end
85
- # rubocop:enable Metrics/MethodLength
86
-
87
- def collate!(other_hash)
88
- replace(collate(other_hash))
89
- end
88
+ def collate!(other_hash)
89
+ replace(collate(other_hash))
90
+ end
90
91
 
91
- def collect_keys
92
- collect { |key, _| yield(key) }
93
- end
92
+ def collect_keys
93
+ collect { |key, _| yield(key) }
94
+ end
94
95
 
95
- def collect_values
96
- collect { |_, val| yield(val) }
97
- end
96
+ def collect_values
97
+ collect { |_, val| yield(val) }
98
+ end
98
99
 
99
- def dearray_values(idx = 0)
100
- each_with_object({}) do |(key, val), hash|
101
- hash[key] = case val
102
- when Array then val[idx] || val[-1]
103
- else val
104
- end
100
+ def dearray_values(idx = 0)
101
+ each_with_object({}) do |(key, val), hash|
102
+ hash[key] = case val
103
+ when Array then val[idx] || val[-1]
104
+ else val
105
+ end
106
+ end
105
107
  end
106
- end
107
108
 
108
- def dearray_values!(idx = 0)
109
- replace(dearray_values(idx))
110
- end
109
+ def dearray_values!(idx = 0)
110
+ replace(dearray_values(idx))
111
+ end
111
112
 
112
- def dearray_singular_values
113
- each_with_object({}) do |(key, val), hash|
114
- hash[key] = case val
115
- when Array then val.size < 2 ? val[0] : val
116
- else val
117
- end
113
+ def dearray_singular_values
114
+ each_with_object({}) do |(key, val), hash|
115
+ hash[key] = case val
116
+ when Array then val.size < 2 ? val[0] : val
117
+ else val
118
+ end
119
+ end
118
120
  end
119
- end
120
121
 
121
- def dearray_singular_values!
122
- replace(dearray_singular_values)
123
- end
122
+ def dearray_singular_values!
123
+ replace(dearray_singular_values)
124
+ end
124
125
 
125
- def deep_merge(other_hash, &block)
126
- dup.deep_merge!(other_hash, &block)
127
- end
126
+ def deep_merge(other_hash, &block)
127
+ dup.deep_merge!(other_hash, &block)
128
+ end
128
129
 
129
- def deep_merge!(other_hash, &block)
130
- merge!(other_hash) do |key, this_val, other_val|
131
- if this_val.is_a?(Hash) && other_val.is_a?(Hash)
132
- this_val.deep_merge(other_val, &block)
133
- elsif block_given?
134
- yield(key, this_val, other_val)
135
- else
136
- other_val
130
+ def deep_merge!(other_hash, &block)
131
+ merge!(other_hash) do |key, this_val, other_val|
132
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
133
+ this_val.deep_merge(other_val, &block)
134
+ elsif block_given?
135
+ yield(key, this_val, other_val)
136
+ else
137
+ other_val
138
+ end
137
139
  end
138
140
  end
139
- end
140
141
 
141
- def delete_unless
142
- delete_if { |key, val| !yield(key, val) }
143
- end
142
+ def delete_unless
143
+ delete_if { |key, val| !yield(key, val) }
144
+ end
144
145
 
145
- def delete_values(*values)
146
- each_key.with_object([]) do |key, array|
147
- next unless values.include?(self[key])
146
+ def delete_values(*values)
147
+ each_key.with_object([]) do |key, array|
148
+ next unless values.include?(self[key])
148
149
 
149
- array << key
150
- delete(key)
150
+ array << key
151
+ delete(key)
152
+ end
151
153
  end
152
- end
153
154
 
154
- def demote(key)
155
- dup.demote!(key)
156
- end
155
+ def demote(key)
156
+ dup.demote!(key)
157
+ end
157
158
 
158
- def demote!(key)
159
- return self unless key?(key)
159
+ def demote!(key)
160
+ return self unless key?(key)
160
161
 
161
- self[key] = delete(key)
162
- self
163
- end
162
+ self[key] = delete(key)
163
+ self
164
+ end
164
165
 
165
- def denillify(identity = 0)
166
- dup.denillify!(identity)
167
- end
166
+ def denillify(identity = 0)
167
+ dup.denillify!(identity)
168
+ end
168
169
 
169
- def denillify!(identity = 0)
170
- each { |key, val| self[key] = val || identity }
171
- end
170
+ def denillify!(identity = 0)
171
+ each { |key, val| self[key] = val || identity }
172
+ end
172
173
 
173
- def diff(hash)
174
- h1 = dup.delete_if { |k, v| hash[k] == v }
175
- h2 = hash.dup.delete_if { |k, _| key?(k) }
176
- h1.merge(h2)
177
- end
174
+ def diff(hash)
175
+ h1 = dup.delete_if { |k, v| hash[k] == v }
176
+ h2 = hash.dup.delete_if { |k, _| key?(k) }
177
+ h1.merge(h2)
178
+ end
178
179
 
179
- def except(*keys)
180
- dup.except!(*keys)
181
- end
180
+ def except(*keys)
181
+ dup.except!(*keys)
182
+ end
182
183
 
183
- def except!(*keys)
184
- keys.each_with_object(self) { |key, _| delete(key) }
185
- end
184
+ def except!(*keys)
185
+ keys.each_with_object(self) { |key, _| delete(key) }
186
+ end
186
187
 
187
- def extract!(*keys)
188
- keys.each_with_object(self) { |key, hash| hash[key] = delete(key) if key?(key) }
189
- end
188
+ def extract!(*keys)
189
+ keys.each_with_object(self) { |key, hash| hash[key] = delete(key) if key?(key) }
190
+ end
190
191
 
191
- def hmap(&block)
192
- dup.hmap!(&block)
193
- end
192
+ def hmap(&block)
193
+ dup.hmap!(&block)
194
+ end
194
195
 
195
- def hmap!
196
- inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) }
197
- end
196
+ def hmap!
197
+ inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) }
198
+ end
198
199
 
199
- def insert(name, value)
200
- return false if key?(name)
200
+ def insert(name, value)
201
+ return false if key?(name)
201
202
 
202
- store(name, value)
203
- true
204
- end
203
+ store(name, value)
204
+ true
205
+ end
205
206
 
206
- def invert
207
- each_pair.with_object({}) do |(key, val), hash|
208
- if val.is_a?(Array)
209
- val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) }
210
- else
211
- hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key)
207
+ def invert
208
+ each_pair.with_object({}) do |(key, val), hash|
209
+ if val.is_a?(Array)
210
+ val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) }
211
+ else
212
+ hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key)
213
+ end
212
214
  end
213
215
  end
214
- end
215
216
 
216
- def keys?(*check_keys)
217
- unknown_keys = check_keys - keys
218
- unknown_keys.empty?
219
- end
217
+ def keys?(*check_keys)
218
+ unknown_keys = check_keys - keys
219
+ unknown_keys.empty?
220
+ end
220
221
 
221
- alias has_keys? keys?
222
+ alias has_keys? keys?
222
223
 
223
- def nillify
224
- dup.nillify!
225
- end
224
+ def nillify
225
+ dup.nillify!
226
+ end
226
227
 
227
- def nillify!
228
- each do |key, val|
229
- self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?)
228
+ def nillify!
229
+ each do |key, val|
230
+ self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?)
231
+ end
230
232
  end
231
- end
232
233
 
233
- def only_fill(*keys, placeholder: nil)
234
- keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder }
235
- end
234
+ def only_fill(*keys, placeholder: nil)
235
+ keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder }
236
+ end
236
237
 
237
- def only_fill!(*keys, placeholder: nil)
238
- replace(only_fill(*keys, placeholder: placeholder))
239
- end
238
+ def only_fill!(*keys, placeholder: nil)
239
+ replace(only_fill(*keys, placeholder: placeholder))
240
+ end
240
241
 
241
- def only_keys?(*check_keys)
242
- unknown_keys = keys - check_keys
243
- unknown_keys.empty?
244
- end
242
+ def only_keys?(*check_keys)
243
+ unknown_keys = keys - check_keys
244
+ unknown_keys.empty?
245
+ end
245
246
 
246
- alias has_only_keys? only_keys?
247
+ alias has_only_keys? only_keys?
247
248
 
248
- def pair?(key, value)
249
- self[key] == value
250
- end
249
+ def pair?(key, value)
250
+ self[key] == value
251
+ end
251
252
 
252
- def promote(key)
253
- dup.promote!(key)
254
- end
253
+ def promote(key)
254
+ dup.promote!(key)
255
+ end
255
256
 
256
- def promote!(key)
257
- return self unless key?(key)
257
+ def promote!(key)
258
+ return self unless key?(key)
258
259
 
259
- { key => delete(key) }.merge(self)
260
- end
260
+ { key => delete(key) }.merge(self)
261
+ end
261
262
 
262
- def rename_keys(*keys)
263
- dup.rename_keys!(*keys)
264
- end
263
+ def rename_keys(*keys)
264
+ dup.rename_keys!(*keys)
265
+ end
265
266
 
266
- def rename_keys!(*keys)
267
- keys = Hash[*keys]
268
- keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] }
269
- end
267
+ def rename_keys!(*keys)
268
+ keys = Hash[*keys]
269
+ keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] }
270
+ end
270
271
 
271
- def reverse_merge(other_hash)
272
- other_hash.merge(self)
273
- end
272
+ def reverse_merge(other_hash)
273
+ other_hash.merge(self)
274
+ end
274
275
 
275
- def reverse_merge!(other_hash)
276
- other_hash.merge!(self)
277
- end
276
+ def reverse_merge!(other_hash)
277
+ other_hash.merge!(self)
278
+ end
278
279
 
279
- def sample
280
- key = sample_key
281
- [key, fetch(key)]
282
- end
280
+ def sample
281
+ key = sample_key
282
+ [key, fetch(key)]
283
+ end
283
284
 
284
- def sample!
285
- key, value = sample
286
- delete(key)
287
- [key, value]
288
- end
285
+ def sample!
286
+ key, value = sample
287
+ delete(key)
288
+ [key, value]
289
+ end
289
290
 
290
- def sample_key
291
- hash_keys = keys
292
- hash_keys.at(Random.rand(hash_keys.size - 1))
293
- end
291
+ def sample_key
292
+ hash_keys = keys
293
+ hash_keys.at(Random.rand(hash_keys.size - 1))
294
+ end
294
295
 
295
- def sample_key!
296
- key, = sample
297
- delete(key)
298
- key
299
- end
296
+ def sample_key!
297
+ key, = sample
298
+ delete(key)
299
+ key
300
+ end
300
301
 
301
- def sample_value
302
- fetch(sample_key)
303
- end
302
+ def sample_value
303
+ fetch(sample_key)
304
+ end
304
305
 
305
- def sample_value!
306
- key, value = sample
307
- delete(key)
308
- value
309
- end
306
+ def sample_value!
307
+ key, value = sample
308
+ delete(key)
309
+ value
310
+ end
310
311
 
311
- def shuffle
312
- Hash[to_a.sample(size)]
313
- end
312
+ def shuffle
313
+ Hash[to_a.sample(size)]
314
+ end
314
315
 
315
- def shuffle!
316
- replace(shuffle)
317
- end
316
+ def shuffle!
317
+ replace(shuffle)
318
+ end
318
319
 
319
- def slice!(*keys)
320
- omit = slice(*self.keys - keys)
321
- hash = slice(*keys)
322
- hash.default = default
323
- hash.default_proc = default_proc if default_proc
324
- replace(hash)
325
- omit
326
- end
320
+ def slice!(*keys)
321
+ omit = slice(*self.keys - keys)
322
+ hash = slice(*keys)
323
+ hash.default = default
324
+ hash.default_proc = default_proc if default_proc
325
+ replace(hash)
326
+ omit
327
+ end
327
328
 
328
- alias only slice
329
- alias only! slice!
329
+ alias only slice
330
+ alias only! slice!
330
331
 
331
- def stringify_keys
332
- each_with_object({}) { |(key, val), hash| hash[key.to_s] = val }
333
- end
332
+ def stringify_keys
333
+ each_with_object({}) { |(key, val), hash| hash[key.to_s] = val }
334
+ end
334
335
 
335
- def stringify_keys!
336
- replace(stringify_keys)
337
- end
336
+ def stringify_keys!
337
+ replace(stringify_keys)
338
+ end
338
339
 
339
- def strip
340
- select { |_, val| !val.blank? }
341
- end
340
+ def strip
341
+ select { |_, val| !val.blank? }
342
+ end
342
343
 
343
- def strip!
344
- reject! { |_, val| val.blank? }
345
- end
344
+ def strip!
345
+ reject! { |_, val| val.blank? }
346
+ end
346
347
 
347
- def symbolize_keys
348
- each_with_object({}) do |(key, val), hash|
349
- new_key = begin
350
- key.to_s.to_sym
351
- rescue StandardError
352
- key
353
- end
348
+ def symbolize_keys
349
+ each_with_object({}) do |(key, val), hash|
350
+ new_key = begin
351
+ key.to_s.to_sym
352
+ rescue StandardError
353
+ key
354
+ end
354
355
 
355
- hash[new_key] = val
356
+ hash[new_key] = val
357
+ end
356
358
  end
357
- end
358
359
 
359
- def symbolize_keys!
360
- replace(symbolize_keys)
361
- end
360
+ def symbolize_keys!
361
+ replace(symbolize_keys)
362
+ end
363
+
364
+ # rubocop:disable Metrics/MethodLength
365
+ def symbolize_and_underscore_keys
366
+ each_with_object({}) do |(key, val), hash|
367
+ new_key = begin
368
+ key.to_s
369
+ .gsub(/::/, '/')
370
+ .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
371
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
372
+ .tr(' -', '_')
373
+ .downcase
374
+ .to_sym
375
+ rescue StandardError
376
+ key
377
+ end
362
378
 
363
- # rubocop:disable Metrics/MethodLength
364
- def symbolize_and_underscore_keys
365
- each_with_object({}) do |(key, val), hash|
366
- new_key = begin
367
- key.to_s
368
- .gsub(/::/, '/')
369
- .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
370
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
371
- .tr(' -', '_')
372
- .downcase
373
- .to_sym
374
- rescue StandardError
375
- key
376
- end
377
-
378
- hash[new_key] = val
379
+ hash[new_key] = val
380
+ end
379
381
  end
380
- end
381
- # rubocop:enable Metrics/MethodLength
382
+ # rubocop:enable Metrics/MethodLength
382
383
 
383
- def symbolize_and_underscore_keys!
384
- replace(symbolize_and_underscore_keys)
385
- end
384
+ def symbolize_and_underscore_keys!
385
+ replace(symbolize_and_underscore_keys)
386
+ end
386
387
 
387
- def to_object
388
- JSON.parse(to_json, object_class: OpenStruct)
389
- end
388
+ def to_object
389
+ JSON.parse(to_json, object_class: OpenStruct)
390
+ end
390
391
 
391
- alias to_o to_object
392
+ alias to_o to_object
392
393
 
393
- def update_each
394
- replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) })
395
- end
394
+ def update_each
395
+ replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) })
396
+ end
396
397
 
397
- def update_keys
398
- return to_enum(:update_keys) unless block_given?
398
+ def update_keys
399
+ return to_enum(:update_keys) unless block_given?
399
400
 
400
- replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
401
- end
401
+ replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
402
+ end
402
403
 
403
- def update_values
404
- return to_enum(:update_values) unless block_given?
404
+ def update_values
405
+ return to_enum(:update_values) unless block_given?
405
406
 
406
- replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
407
- end
407
+ replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
408
+ end
408
409
 
409
- def vacant?(key)
410
- self[key].blank?
411
- end
410
+ def vacant?(key)
411
+ self[key].blank?
412
+ end
412
413
 
414
+ end
413
415
  end