lite-ruby 1.3.3 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,34 @@
1
+ weekday: '%d'
2
+ weekday_blank: '%_d'
3
+ weekday_padded: '%d'
4
+ weekday_unpadded: '%-d'
5
+ weekday_name: '%A'
6
+ weekday_name_abbr: '%a'
7
+ month: '%m'
8
+ month_blank: '%_m'
9
+ month_padded: '%m'
10
+ month_unpadded: '%-m'
11
+ month_name: '%B'
12
+ month_name_abbr: '%b'
13
+ week_iso: '%V'
14
+ week_monday: '%W'
15
+ week_sunday: '%U'
16
+ year: '%Y'
17
+ yr: '%y'
18
+ year_abbr: '%y'
19
+ day: '%B %-d'
20
+ day_abbr: '%b %-d'
21
+ day_iso: '%m-%d'
22
+ month_year: '%m %Y'
23
+ month_blank_year: '%_m %Y'
24
+ month_padded_year: '%m %Y'
25
+ month_unpadded_year: '%-m %Y'
26
+ month_name_year: '%B %Y'
27
+ month_name_abbr_year: '%b %Y'
28
+ week_year_iso: '%V-%G'
29
+ year_day: '%Y-%m-%d'
30
+ year_month: '%Y-%m'
31
+ year_week: '%G-%V'
32
+ date: '%B %-d %Y'
33
+ date_abbr: '%b %-d %Y'
34
+ date_iso: '%Y-%m-%d'
@@ -0,0 +1,42 @@
1
+ w: 'u'
2
+ weekday: 'u'
3
+ ww: 'w'
4
+ weekday_offset: 'w'
5
+ www: 'A'
6
+ weekday_name: 'A'
7
+ wwww: 'a'
8
+ weekday_name_abbr: 'a'
9
+ d: 'd'
10
+ day: 'd'
11
+ day_padded: 'd'
12
+ dd: '-d'
13
+ Day: '-d'
14
+ day_unpadded: '-d'
15
+ ddd: '_d'
16
+ DAY: '_d'
17
+ day_blank: '_d'
18
+ dddd: 'j'
19
+ day_of_the_year: 'j'
20
+ m: 'm'
21
+ month: 'm'
22
+ month_padded: 'm'
23
+ mm: '-m'
24
+ Month: '-m'
25
+ month_unpadded: '-m'
26
+ mmm: '_m'
27
+ MONTH: '_m'
28
+ month_blank: '_m'
29
+ mmmm: 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+ 'Ź': 'Z'
185
+ 'ź': 'z'
186
+ 'Ż': 'Z'
187
+ 'ż': 'z'
188
+ 'Ž': 'Z'
189
+ 'ž': 'z'
@@ -0,0 +1,42 @@
1
+ hour: '%H'
2
+ hour_blank: '%k'
3
+ hour_padded: '%H'
4
+ hour12: '%I'
5
+ hour12_padded: '%I'
6
+ hour12_blank: '%l'
7
+ minute: '%M'
8
+ second: '%S'
9
+ ampm: '%P'
10
+ meridian: '%p'
11
+ time_zone: '%z'
12
+ time_zone_offset: '%:z'
13
+ time_zone_offset_full: '%::z'
14
+ time_zone_name: '%Z'
15
+ time: '%H:%M'
16
+ time_blank: '%k:%M'
17
+ time_padded: '%H:%M'
18
+ time_tz: '%H:%M %z'
19
+ time_tzn: '%H:%M %Z'
20
+ time12: '%I:%M %P'
21
+ time12_padded: '%I:%M %P'
22
+ time12_blank: '%l:%M %P'
23
+ time12_tz: '%I:%M %P %z'
24
+ time12_tzn: '%I:%M %P %Z'
25
+ daytime: '%B %-d %H:%M'
26
+ daytime_abbr: '%b %-d %H:%M'
27
+ daytime_iso: '%m-%d %H:%M'
28
+ daytime12: '%B %-d %I:%M %P'
29
+ daytime12_abbr: '%b %-d %I:%M %P'
30
+ daytime12_iso: '%m-%d %I:%M %P'
31
+ datetime: '%B %-d %Y %H:%M'
32
+ datetime_abbr: '%b %-d %Y %H:%M'
33
+ datetime_iso: '%Y-%m-%d %H:%M'
34
+ datetime12: '%B %-d %Y %I:%M %P'
35
+ datetime12_abbr: '%b %-d %Y %I:%M %P'
36
+ datetime12_iso: '%Y-%m-%d %I:%M %P'
37
+ datetime_tzn: '%B %-d %Y %H:%M %Z'
38
+ datetime_abbr_tzn: '%b %-d %Y %H:%M %Z'
39
+ datetime_iso_tzn: '%Y-%m-%d %H:%M %z'
40
+ datetime12_tzn: '%B %-d %Y %I:%M %P %Z'
41
+ datetime12_abbr_tzn: '%b %-d %Y %I:%M %P %Z'
42
+ datetime12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
@@ -0,0 +1,28 @@
1
+ h: 'H'
2
+ hour: 'H'
3
+ hour_padded: 'H'
4
+ hh: 'k'
5
+ HOUR: 'k'
6
+ hour_blank: 'k'
7
+ hhh: 'I'
8
+ hour12: 'I'
9
+ hour12_padded: 'I'
10
+ hhhh: 'l'
11
+ HOUR12: 'l'
12
+ hour12_blank: 'l'
13
+ n: 'M'
14
+ minute: 'M'
15
+ s: 'S'
16
+ second: 'S'
17
+ ampm: 'P'
18
+ meridian: 'P'
19
+ AMPM: 'p'
20
+ MERIDIAN: 'p'
21
+ z: 'z'
22
+ time_zone: 'z'
23
+ zz: ':z'
24
+ time_zone_offset: ':z'
25
+ zzz: '::z'
26
+ time_zone_offset_full: '::z'
27
+ zzzz: 'Z'
28
+ time_zone_name: 'Z'
@@ -1,408 +1,420 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('hash')
4
- class Hash
3
+ class Hash
5
4
 
6
- class << self
7
-
8
- def zip(keys, values)
9
- keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] }
10
- end
5
+ class << self
11
6
 
7
+ def zip(keys, values)
8
+ keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] }
12
9
  end
13
10
 
14
- def aka(new_key, old_key)
15
- self[new_key] = self[old_key] if key?(old_key)
16
- self
17
- end
18
-
19
- def assert_min_keys!(*valid_keys)
20
- return self if empty?
21
-
22
- valid_keys.each do |key|
23
- next if key?(key)
24
-
25
- raise ArgumentError,
26
- "Missing key: #{key.inspect}. " \
27
- "Minimum keys are: #{valid_keys.map(&:inspect).join(', ')}"
28
- end
11
+ end
29
12
 
30
- self
31
- end
13
+ def aka(new_key, old_key)
14
+ self[new_key] = self[old_key] if key?(old_key)
15
+ self
16
+ end
32
17
 
33
- def assert_all_min_keys!(*valid_keys)
34
- return assert_min_keys!(*valid_keys) unless empty?
18
+ def assert_min_keys!(*valid_keys)
19
+ return self if empty?
35
20
 
36
- raise ArgumentError, 'An empty hash is not allowed'
37
- end
21
+ valid_keys.each do |key|
22
+ next if key?(key)
38
23
 
39
- def assert_pair_presence!(*valid_keys)
40
- each do |key, value|
41
- if !valid_keys.include?(key)
42
- raise ArgumentError,
43
- "Invalid key: #{key.inspect}. " \
44
- "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
45
- elsif value.respond_to?(:blank?) ? value.blank? : !value
46
- raise ArgumentError, "A #{value.inspect} value for #{key.inspect} is not allowed"
47
- end
48
- end
24
+ raise ArgumentError,
25
+ "Missing key: #{key.inspect}. " \
26
+ "Minimum keys are: #{valid_keys.map(&:inspect).join(', ')}"
49
27
  end
50
28
 
51
- def assert_all_pair_presence!(*valid_keys)
52
- return assert_pair_presence!(*valid_keys) unless empty?
29
+ self
30
+ end
53
31
 
54
- raise ArgumentError, 'An empty hash is not allowed'
55
- end
32
+ def assert_all_min_keys!(*valid_keys)
33
+ return assert_min_keys!(*valid_keys) unless empty?
56
34
 
57
- def assert_valid_keys!(*valid_keys)
58
- each_key do |key|
59
- next if valid_keys.include?(key)
35
+ raise ArgumentError, 'An empty hash is not allowed'
36
+ end
60
37
 
38
+ def assert_pair_presence!(*valid_keys)
39
+ each do |key, value|
40
+ if !valid_keys.include?(key)
61
41
  raise ArgumentError,
62
42
  "Invalid key: #{key.inspect}. " \
63
43
  "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
44
+ elsif value.respond_to?(:blank?) ? value.blank? : !value
45
+ raise ArgumentError, "A #{value.inspect} value for #{key.inspect} is not allowed"
64
46
  end
65
47
  end
48
+ end
66
49
 
67
- def assert_all_valid_keys!(*valid_keys)
68
- return assert_valid_keys!(*valid_keys) unless empty?
50
+ def assert_all_pair_presence!(*valid_keys)
51
+ return assert_pair_presence!(*valid_keys) unless empty?
69
52
 
70
- raise ArgumentError, 'An empty hash is not allowed'
71
- end
53
+ raise ArgumentError, 'An empty hash is not allowed'
54
+ end
72
55
 
73
- def assert_valid_values!(*valid_values)
74
- each_value do |value|
75
- next if valid_values.include?(value)
56
+ def assert_valid_keys!(*valid_keys)
57
+ each_key do |key|
58
+ next if valid_keys.include?(key)
76
59
 
77
- raise ArgumentError,
78
- "Invalid value: #{value.inspect}. " \
79
- "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
80
- end
60
+ raise ArgumentError,
61
+ "Invalid key: #{key.inspect}. " \
62
+ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
81
63
  end
64
+ end
82
65
 
83
- def assert_all_valid_values!(*valid_values)
84
- return assert_valid_values!(*valid_values) unless empty?
66
+ def assert_all_valid_keys!(*valid_keys)
67
+ return assert_valid_keys!(*valid_keys) unless empty?
85
68
 
86
- raise ArgumentError, 'An empty hash is not allowed'
87
- end
69
+ raise ArgumentError, 'An empty hash is not allowed'
70
+ end
88
71
 
89
- # rubocop:disable Style/GuardClause
90
- def bury(*args)
91
- if args.count < 2
92
- raise ArgumentError, '2 or more arguments required'
93
- elsif args.count == 2
94
- self[args[0]] = args[1]
95
- else
96
- arg = args.shift
97
- self[arg] = {} unless self[arg]
98
- self[arg].bury(*args) unless args.empty?
99
- end
72
+ def assert_valid_values!(*valid_values)
73
+ each_value do |value|
74
+ next if valid_values.include?(value)
100
75
 
101
- self
76
+ raise ArgumentError,
77
+ "Invalid value: #{value.inspect}. " \
78
+ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
102
79
  end
103
- # rubocop:enable Style/GuardClause
80
+ end
104
81
 
105
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
106
- def collate(*others)
107
- hash = {}
82
+ def assert_all_valid_values!(*valid_values)
83
+ return assert_valid_values!(*valid_values) unless empty?
108
84
 
109
- each_key { |key| hash[key] = [] }
85
+ raise ArgumentError, 'An empty hash is not allowed'
86
+ end
110
87
 
111
- others.each do |other|
112
- other.each_key { |key| hash[key] = [] }
113
- end
88
+ # rubocop:disable Style/GuardClause
89
+ def bury(*args)
90
+ if args.count < 2
91
+ raise ArgumentError, '2 or more arguments required'
92
+ elsif args.count == 2
93
+ self[args[0]] = args[1]
94
+ else
95
+ arg = args.shift
96
+ self[arg] = {} unless self[arg]
97
+ self[arg].bury(*args) unless args.empty?
98
+ end
114
99
 
115
- each { |key, val| hash[key] << val }
100
+ self
101
+ end
102
+ # rubocop:enable Style/GuardClause
116
103
 
117
- others.each do |other|
118
- other.each { |key, val| hash[key] << val }
119
- end
104
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
105
+ def collate(*others)
106
+ hash = {}
120
107
 
121
- hash.each_value(&:flatten!)
122
- hash
123
- end
124
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
108
+ each_key { |key| hash[key] = [] }
125
109
 
126
- def collate!(other_hash)
127
- replace(collate(other_hash))
110
+ others.each do |other|
111
+ other.each_key { |key| hash[key] = [] }
128
112
  end
129
113
 
130
- def collect_keys(&block)
131
- keys.map(&block)
132
- end
114
+ each { |key, val| hash[key] << val }
133
115
 
134
- def collect_values(&block)
135
- values.map(&block)
116
+ others.each do |other|
117
+ other.each { |key, val| hash[key] << val }
136
118
  end
137
119
 
138
- def dearray_values(idx = 0)
139
- each_with_object({}) do |(key, val), hash|
140
- hash[key] = case val
141
- when Array then val[idx] || val[-1]
142
- else val
143
- end
144
- end
145
- end
120
+ hash.each_value(&:flatten!)
121
+ hash
122
+ end
123
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
146
124
 
147
- def dearray_values!(idx = 0)
148
- replace(dearray_values(idx))
149
- end
125
+ def collate!(other_hash)
126
+ replace(collate(other_hash))
127
+ end
150
128
 
151
- def dearray_singular_values
152
- each_with_object({}) do |(key, val), hash|
153
- hash[key] = case val
154
- when Array then val.size < 2 ? val[0] : val
155
- else val
156
- end
157
- end
158
- end
129
+ def collect_keys(&block)
130
+ keys.map(&block)
131
+ end
159
132
 
160
- def dearray_singular_values!
161
- replace(dearray_singular_values)
162
- end
133
+ def collect_values(&block)
134
+ values.map(&block)
135
+ end
163
136
 
164
- def delete_unless
165
- delete_if { |key, val| !yield(key, val) }
137
+ def dearray_values(idx = 0)
138
+ each_with_object({}) do |(key, val), hash|
139
+ hash[key] = case val
140
+ when Array then val[idx] || val[-1]
141
+ else val
142
+ end
166
143
  end
144
+ end
167
145
 
168
- def delete_values(*values)
169
- each_key.with_object([]) do |key, array|
170
- next unless values.include?(self[key])
146
+ def dearray_values!(idx = 0)
147
+ replace(dearray_values(idx))
148
+ end
171
149
 
172
- array << key
173
- delete(key)
174
- end
150
+ def dearray_singular_values
151
+ each_with_object({}) do |(key, val), hash|
152
+ hash[key] = case val
153
+ when Array then val.size < 2 ? val[0] : val
154
+ else val
155
+ end
175
156
  end
157
+ end
176
158
 
177
- def demote(key)
178
- dup.demote!(key)
179
- end
159
+ def dearray_singular_values!
160
+ replace(dearray_singular_values)
161
+ end
180
162
 
181
- def demote!(key)
182
- return self unless key?(key)
163
+ def deep_key?(*keys)
164
+ last_hash = self
165
+ found_key = false
183
166
 
184
- self[key] = delete(key)
185
- self
186
- end
167
+ keys.each do |key|
168
+ break found_key = false unless last_hash.key?(key)
187
169
 
188
- def denillify(identity = 0)
189
- dup.denillify!(identity)
170
+ last_hash = last_hash[key]
171
+ found_key = true
190
172
  end
191
173
 
192
- def denillify!(identity = 0)
193
- each { |key, val| self[key] = val || identity }
194
- end
174
+ found_key
175
+ end
195
176
 
196
- def diff(hash)
197
- h1 = dup.delete_if { |k, v| hash[k] == v }
198
- h2 = hash.dup.delete_if { |k, _| key?(k) }
199
- h1.merge(h2)
200
- end
177
+ def delete_unless
178
+ delete_if { |key, val| !yield(key, val) }
179
+ end
201
180
 
202
- def hmap(&block)
203
- dup.hmap!(&block)
204
- end
181
+ def delete_values(*values)
182
+ each_key.with_object([]) do |key, array|
183
+ next unless values.include?(self[key])
205
184
 
206
- def hmap!
207
- inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) }
185
+ array << key
186
+ delete(key)
208
187
  end
188
+ end
209
189
 
210
- def insert(name, value)
211
- return false if key?(name)
190
+ def demote(key)
191
+ dup.demote!(key)
192
+ end
212
193
 
213
- store(name, value)
214
- true
215
- end
194
+ def demote!(key)
195
+ return self unless key?(key)
216
196
 
217
- def invert
218
- each_pair.with_object({}) do |(key, val), hash|
219
- if val.is_a?(Array)
220
- val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) }
221
- else
222
- hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key)
223
- end
224
- end
225
- end
197
+ self[key] = delete(key)
198
+ self
199
+ end
226
200
 
227
- def keys?(*check_keys)
228
- unknown_keys = check_keys - keys
229
- unknown_keys.empty?
230
- end
201
+ def denillify(identity = 0)
202
+ dup.denillify!(identity)
203
+ end
231
204
 
232
- def nillify
233
- dup.nillify!
234
- end
205
+ def denillify!(identity = 0)
206
+ each { |key, val| self[key] = val || identity }
207
+ end
235
208
 
236
- def nillify!
237
- each do |key, val|
238
- next if val.nil?
209
+ def diff(hash)
210
+ h1 = dup.delete_if { |k, v| hash[k] == v }
211
+ h2 = hash.dup.delete_if { |k, _| key?(k) }
212
+ h1.merge(h2)
213
+ end
214
+
215
+ def hmap(&block)
216
+ dup.hmap!(&block)
217
+ end
218
+
219
+ def hmap!
220
+ inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) }
221
+ end
239
222
 
240
- self[key] = nil if respond_to?(:blank?) ? val.blank? : !val
223
+ def insert(name, value)
224
+ return false if key?(name)
225
+
226
+ store(name, value)
227
+ true
228
+ end
229
+
230
+ def invert
231
+ each_pair.with_object({}) do |(key, val), hash|
232
+ if val.is_a?(Array)
233
+ val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) }
234
+ else
235
+ hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key)
241
236
  end
242
237
  end
238
+ end
243
239
 
244
- def only_fill(*keys, placeholder: nil)
245
- keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder }
246
- end
240
+ def keys?(*check_keys)
241
+ unknown_keys = check_keys - keys
242
+ unknown_keys.empty?
243
+ end
247
244
 
248
- def only_fill!(*keys, placeholder: nil)
249
- replace(only_fill(*keys, placeholder: placeholder))
250
- end
245
+ def nillify
246
+ dup.nillify!
247
+ end
251
248
 
252
- def only_keys?(*check_keys)
253
- unknown_keys = keys - check_keys
254
- unknown_keys.empty?
255
- end
249
+ def nillify!
250
+ each do |key, val|
251
+ next if val.nil?
256
252
 
257
- def pair?(key, value)
258
- self[key] == value
253
+ self[key] = nil if respond_to?(:blank?) ? val.blank? : !val
259
254
  end
255
+ end
260
256
 
261
- def promote(key)
262
- dup.promote!(key)
263
- end
257
+ def only_fill(*keys, placeholder: nil)
258
+ keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder }
259
+ end
264
260
 
265
- def promote!(key)
266
- return self unless key?(key)
261
+ def only_fill!(*keys, placeholder: nil)
262
+ replace(only_fill(*keys, placeholder: placeholder))
263
+ end
267
264
 
268
- { key => delete(key) }.merge(self)
269
- end
265
+ def only_keys?(*check_keys)
266
+ unknown_keys = keys - check_keys
267
+ unknown_keys.empty?
268
+ end
270
269
 
271
- def rename_keys(*keys)
272
- dup.rename_keys!(*keys)
273
- end
270
+ def pair?(key, value)
271
+ self[key] == value
272
+ end
274
273
 
275
- def rename_keys!(*keys)
276
- keys = Hash[*keys]
277
- keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] }
278
- end
274
+ def promote(key)
275
+ dup.promote!(key)
276
+ end
279
277
 
280
- def sample
281
- key = sample_key
282
- [key, fetch(key)]
283
- end
278
+ def promote!(key)
279
+ return self unless key?(key)
284
280
 
285
- def sample!
286
- key, value = sample
287
- delete(key)
288
- [key, value]
289
- end
281
+ { key => delete(key) }.merge(self)
282
+ end
290
283
 
291
- def sample_key
292
- hash_keys = keys
293
- hash_keys.at(Random.rand(hash_keys.size - 1))
294
- end
284
+ def rename_keys(*keys)
285
+ dup.rename_keys!(*keys)
286
+ end
295
287
 
296
- def sample_key!
297
- key, _val = sample
298
- delete(key)
299
- key
300
- end
288
+ def rename_keys!(*keys)
289
+ keys = Hash[*keys]
290
+ keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] }
291
+ end
301
292
 
302
- def sample_value
303
- fetch(sample_key)
304
- end
293
+ def sample
294
+ key = sample_key
295
+ [key, fetch(key)]
296
+ end
305
297
 
306
- def sample_value!
307
- key, value = sample
308
- delete(key)
309
- value
310
- end
298
+ def sample!
299
+ key, value = sample
300
+ delete(key)
301
+ [key, value]
302
+ end
311
303
 
312
- def shuffle
313
- to_a.sample(size).to_h
314
- end
304
+ def sample_key
305
+ hash_keys = keys
306
+ hash_keys.at(Random.rand(hash_keys.size - 1))
307
+ end
315
308
 
316
- def shuffle!
317
- replace(shuffle)
318
- end
309
+ def sample_key!
310
+ key, _val = sample
311
+ delete(key)
312
+ key
313
+ end
319
314
 
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
315
+ def sample_value
316
+ fetch(sample_key)
317
+ end
328
318
 
329
- def strip
330
- reject { |_, val| respond_to?(:blank?) ? val.blank? : !val }
331
- end
319
+ def sample_value!
320
+ key, value = sample
321
+ delete(key)
322
+ value
323
+ end
332
324
 
333
- def strip!
334
- reject! { |_, val| respond_to?(:blank?) ? val.blank? : !val }
335
- end
325
+ def shuffle
326
+ to_a.sample(size).to_h
327
+ end
336
328
 
337
- # rubocop:disable Metrics/MethodLength
338
- def symbolize_and_underscore_keys
339
- each_with_object({}) do |(key, val), hash|
340
- new_key = begin
341
- str = key.dup.to_s
342
- str.gsub!(/::/, '/') || str
343
- str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str
344
- str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str
345
- str.tr!(' -', '_') || str
346
- str.downcase!
347
- str.to_sym
348
- rescue StandardError
349
- key
350
- end
351
-
352
- hash[new_key] = val
353
- end
354
- end
355
- # rubocop:enable Metrics/MethodLength
329
+ def shuffle!
330
+ replace(shuffle)
331
+ end
356
332
 
357
- def symbolize_and_underscore_keys!
358
- replace(symbolize_and_underscore_keys)
359
- end
333
+ def slice!(*keys)
334
+ omit = slice(*self.keys - keys)
335
+ hash = slice(*keys)
336
+ hash.default = default
337
+ hash.default_proc = default_proc if default_proc
338
+ replace(hash)
339
+ omit
340
+ end
360
341
 
361
- def to_object
362
- JSON.parse(to_json, object_class: OpenStruct)
363
- end
342
+ def strip
343
+ reject { |_, val| respond_to?(:blank?) ? val.blank? : !val }
344
+ end
364
345
 
365
- def to_open_struct(lazy: true)
366
- struct = OpenStruct.new(self)
367
- struct.methods(lazy)
368
- struct
369
- end
346
+ def strip!
347
+ reject! { |_, val| respond_to?(:blank?) ? val.blank? : !val }
348
+ end
370
349
 
371
- def to_struct
372
- struct = Struct.new(*keys)
373
- struct.new(*values)
374
- end
350
+ # rubocop:disable Metrics/MethodLength
351
+ def symbolize_and_underscore_keys
352
+ each_with_object({}) do |(key, val), hash|
353
+ new_key = begin
354
+ str = key.dup.to_s
355
+ str.gsub!(/::/, '/') || str
356
+ str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str
357
+ str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str
358
+ str.tr!(' -', '_') || str
359
+ str.downcase!
360
+ str.to_sym
361
+ rescue StandardError
362
+ key
363
+ end
375
364
 
376
- def update_each
377
- replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) })
365
+ hash[new_key] = val
378
366
  end
367
+ end
368
+ # rubocop:enable Metrics/MethodLength
379
369
 
380
- def update_keys
381
- return to_enum(:update_keys) unless defined?(yield)
370
+ def symbolize_and_underscore_keys!
371
+ replace(symbolize_and_underscore_keys)
372
+ end
382
373
 
383
- replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
384
- end
374
+ def to_object
375
+ JSON.parse(to_json, object_class: OpenStruct)
376
+ end
385
377
 
386
- def update_values
387
- return to_enum(:update_values) unless defined?(yield)
378
+ def to_open_struct(lazy: true)
379
+ struct = OpenStruct.new(self)
380
+ struct.methods(lazy)
381
+ struct
382
+ end
388
383
 
389
- replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
390
- end
384
+ def to_struct
385
+ struct = Struct.new(*keys)
386
+ struct.new(*values)
387
+ end
391
388
 
392
- def vacant?(key)
393
- value = self[key]
394
- respond_to?(:blank?) ? value.blank? : !value
395
- end
389
+ def update_each
390
+ replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) })
391
+ end
392
+
393
+ def update_keys
394
+ return to_enum(:update_keys) unless defined?(yield)
395
+
396
+ replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
397
+ end
396
398
 
397
- alias has_keys? keys?
398
- alias has_only_keys? only_keys?
399
- alias map_keys collect_keys
400
- alias map_values collect_values
401
- alias only slice
402
- alias only! slice!
403
- alias to_o to_object
399
+ def update_values
400
+ return to_enum(:update_values) unless defined?(yield)
404
401
 
402
+ replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
405
403
  end
406
404
 
407
- require 'lite/ruby/safe/hash' unless defined?(ActiveSupport)
405
+ def vacant?(key)
406
+ value = self[key]
407
+ respond_to?(:blank?) ? value.blank? : !value
408
+ end
409
+
410
+ alias has_keys? keys?
411
+ alias has_only_keys? only_keys?
412
+ alias map_keys collect_keys
413
+ alias map_values collect_values
414
+ alias only slice
415
+ alias only! slice!
416
+ alias to_o to_object
417
+
408
418
  end
419
+
420
+ require 'lite/ruby/safe/hash' unless defined?(ActiveSupport)