lite-ruby 1.0.27 → 1.1.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
- data/.rubocop.yml +8 -1
- data/.travis.yml +1 -0
- data/CHANGELOG.md +33 -2
- data/Gemfile.lock +47 -45
- data/README.md +1 -0
- data/docs/ARRAY.md +2 -10
- data/docs/HASH.md +4 -4
- data/docs/NUMERIC.md +0 -10
- data/docs/STRING.md +11 -20
- data/docs/TIME.md +17 -17
- data/lib/lite/ruby.rb +4 -1
- data/lib/lite/ruby/array.rb +15 -102
- data/lib/lite/ruby/boolean.rb +4 -4
- data/lib/lite/ruby/enumerable.rb +15 -49
- data/lib/lite/ruby/hash.rb +34 -108
- data/lib/lite/ruby/helpers/time_helper.rb +21 -21
- data/lib/lite/ruby/numeric.rb +13 -38
- data/lib/lite/ruby/object.rb +4 -38
- data/lib/lite/ruby/open_struct.rb +2 -3
- data/lib/lite/ruby/range.rb +2 -4
- data/lib/lite/ruby/safe/array.rb +93 -0
- data/lib/lite/ruby/safe/enumerable.rb +42 -0
- data/lib/lite/ruby/safe/hash.rb +83 -0
- data/lib/lite/ruby/safe/object.rb +41 -0
- data/lib/lite/ruby/safe/range.rb +9 -0
- data/lib/lite/ruby/safe/string.rb +187 -0
- data/lib/lite/ruby/string.rb +65 -218
- data/lib/lite/ruby/version.rb +1 -1
- metadata +8 -2
data/lib/lite/ruby.rb
CHANGED
@@ -4,11 +4,14 @@
|
|
4
4
|
require "lite/ruby/#{filename}"
|
5
5
|
end
|
6
6
|
|
7
|
+
%w[date time].each do |filename|
|
8
|
+
require "lite/ruby/helpers/#{filename}_helper"
|
9
|
+
end
|
10
|
+
|
7
11
|
%w[
|
8
12
|
array boolean date enumerable hash integer kernel numeric object open_struct range string
|
9
13
|
struct time
|
10
14
|
].each do |filename|
|
11
|
-
require "lite/ruby/helpers/#{filename}_helper" if %w[date time].include?(filename)
|
12
15
|
require "lite/ruby/#{filename}"
|
13
16
|
end
|
14
17
|
|
data/lib/lite/ruby/array.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('array')
|
4
|
+
require 'lite/ruby/safe/array' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
class Array
|
5
7
|
|
6
8
|
def assert_min_values!(*valid_values)
|
@@ -109,10 +111,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
109
111
|
# rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
|
110
112
|
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
111
113
|
|
112
|
-
def deep_dup
|
113
|
-
map(&:deep_dup)
|
114
|
-
end
|
115
|
-
|
116
114
|
def delete_first
|
117
115
|
self[1..-1]
|
118
116
|
end
|
@@ -160,11 +158,8 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
160
158
|
end
|
161
159
|
|
162
160
|
def except!(*values)
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
def from(position)
|
167
|
-
self[position, size] || []
|
161
|
+
reject! { |val| values.include?(val) }
|
162
|
+
self
|
168
163
|
end
|
169
164
|
|
170
165
|
def fulfill(value, amount)
|
@@ -183,54 +178,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
183
178
|
collection << self[-rem, rem]
|
184
179
|
end
|
185
180
|
|
186
|
-
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
187
|
-
def in_groups(number, fill_with = nil)
|
188
|
-
collection_size = size
|
189
|
-
division = collection_size.div(number)
|
190
|
-
modulo = collection_size % number
|
191
|
-
|
192
|
-
collection = []
|
193
|
-
start = 0
|
194
|
-
number.times do |int|
|
195
|
-
mod_gt_zero = modulo.positive?
|
196
|
-
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
197
|
-
collection << last_group = slice(start, grouping)
|
198
|
-
last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
|
199
|
-
start += grouping
|
200
|
-
end
|
201
|
-
|
202
|
-
return collection unless block_given?
|
203
|
-
|
204
|
-
collection.each { |val| yield(val) }
|
205
|
-
end
|
206
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
207
|
-
|
208
|
-
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
|
209
|
-
def in_groups_of(number, fill_with = nil)
|
210
|
-
if number.to_i <= 0
|
211
|
-
raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
|
212
|
-
elsif fill_with == false
|
213
|
-
collection = self
|
214
|
-
else
|
215
|
-
padding = (number - size % number) % number
|
216
|
-
collection = dup.concat(Array.new(padding, fill_with))
|
217
|
-
end
|
218
|
-
|
219
|
-
sliced_collection = collection.each_slice(number)
|
220
|
-
return sliced_collection.to_a unless block_given?
|
221
|
-
|
222
|
-
sliced_collection { |val| yield(val) }
|
223
|
-
end
|
224
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
|
225
|
-
|
226
181
|
def indexes(value)
|
227
182
|
array = []
|
228
183
|
each_with_index { |val, i| array << i if value == val }
|
229
184
|
array
|
230
185
|
end
|
231
186
|
|
232
|
-
alias indices indexes
|
233
|
-
|
234
187
|
def merge(*values)
|
235
188
|
dup.merge!(*values)
|
236
189
|
end
|
@@ -252,7 +205,8 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
252
205
|
end
|
253
206
|
|
254
207
|
def only!(*values)
|
255
|
-
|
208
|
+
select! { |val| values.include?(val) }
|
209
|
+
self
|
256
210
|
end
|
257
211
|
|
258
212
|
def position(value)
|
@@ -284,7 +238,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
284
238
|
end
|
285
239
|
|
286
240
|
def promote!(value)
|
287
|
-
|
241
|
+
sort_by! { |val| val == value ? -1 : 0 }
|
288
242
|
end
|
289
243
|
|
290
244
|
def rand_sample(max = nil)
|
@@ -292,10 +246,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
292
246
|
sample(amount)
|
293
247
|
end
|
294
248
|
|
295
|
-
def reject_values(*args)
|
296
|
-
reject { |val| args.include?(val) }
|
297
|
-
end
|
298
|
-
|
299
249
|
def rposition(value)
|
300
250
|
idx = rindex(value)
|
301
251
|
return idx if idx.nil?
|
@@ -307,36 +257,13 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
307
257
|
delete_at(Random.rand(size - 1))
|
308
258
|
end
|
309
259
|
|
310
|
-
# rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
311
|
-
def split(number = nil)
|
312
|
-
array = [[]]
|
313
|
-
|
314
|
-
if block_given?
|
315
|
-
each { |val| yield(val) ? (array << []) : (array.last << val) }
|
316
|
-
else
|
317
|
-
dup_arr = dup
|
318
|
-
|
319
|
-
until dup_arr.empty?
|
320
|
-
if (idx = dup_arr.index(number))
|
321
|
-
array.last << dup_arr.shift(idx)
|
322
|
-
dup_arr.shift
|
323
|
-
array << []
|
324
|
-
else
|
325
|
-
array.last << arr.shift(dup_arr.size)
|
326
|
-
end
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
array
|
331
|
-
end
|
332
|
-
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
333
|
-
|
334
260
|
def strip
|
335
261
|
reject(&:blank?)
|
336
262
|
end
|
337
263
|
|
338
264
|
def strip!
|
339
|
-
|
265
|
+
reject!(&:blank?)
|
266
|
+
self
|
340
267
|
end
|
341
268
|
|
342
269
|
def swap(from, to)
|
@@ -344,26 +271,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('array')
|
|
344
271
|
self
|
345
272
|
end
|
346
273
|
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
354
|
-
def to_sentence(options = {})
|
355
|
-
words_connector = options[:words_connector] || ', '
|
356
|
-
two_words_connector = options[:two_words_connector] || ' and '
|
357
|
-
last_word_connector = options[:last_word_connector] || ', and '
|
358
|
-
|
359
|
-
case size
|
360
|
-
when 0 then ''
|
361
|
-
when 1 then self[0].to_s.dup
|
362
|
-
when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
|
363
|
-
else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
|
364
|
-
end
|
365
|
-
end
|
366
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
274
|
+
alias extract! except!
|
275
|
+
alias indices indexes
|
276
|
+
alias reject_values except
|
277
|
+
alias reject_values! except!
|
278
|
+
alias select_values only
|
279
|
+
alias select_values! only!
|
367
280
|
|
368
281
|
end
|
369
282
|
end
|
data/lib/lite/ruby/boolean.rb
CHANGED
@@ -7,12 +7,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('boolean')
|
|
7
7
|
self
|
8
8
|
end
|
9
9
|
|
10
|
-
alias to_b to_bool
|
11
|
-
|
12
10
|
def to_i
|
13
11
|
0
|
14
12
|
end
|
15
13
|
|
14
|
+
alias to_b to_bool
|
15
|
+
|
16
16
|
end
|
17
17
|
|
18
18
|
class TrueClass
|
@@ -21,11 +21,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('boolean')
|
|
21
21
|
self
|
22
22
|
end
|
23
23
|
|
24
|
-
alias to_b to_bool
|
25
|
-
|
26
24
|
def to_i
|
27
25
|
1
|
28
26
|
end
|
29
27
|
|
28
|
+
alias to_b to_bool
|
29
|
+
|
30
30
|
end
|
31
31
|
end
|
data/lib/lite/ruby/enumerable.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
4
|
+
require 'lite/ruby/safe/enumerable' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
module Enumerable
|
5
7
|
|
6
8
|
def cluster
|
@@ -20,7 +22,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def deduce(identity = 0, &block)
|
23
|
-
if
|
25
|
+
if defined?(yield)
|
24
26
|
map(&block).deduce(identity)
|
25
27
|
else
|
26
28
|
inject { |acc, val| acc - val } || identity
|
@@ -46,7 +48,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
46
48
|
def exactly?(num)
|
47
49
|
found_count = 0
|
48
50
|
|
49
|
-
if
|
51
|
+
if defined?(yield)
|
50
52
|
each { |*opt| found_count += 1 if yield(*opt) }
|
51
53
|
else
|
52
54
|
each { |opt| found_count += 1 if opt }
|
@@ -61,23 +63,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
61
63
|
end
|
62
64
|
# rubocop:enable Style/CaseEquality
|
63
65
|
|
64
|
-
def exclude?(object)
|
65
|
-
!include?(object)
|
66
|
-
end
|
67
|
-
|
68
|
-
def excluding(*elements)
|
69
|
-
elements.flatten!(1)
|
70
|
-
reject { |element| elements.include?(element) }
|
71
|
-
end
|
72
|
-
|
73
|
-
alias without excluding
|
74
|
-
|
75
66
|
def expand
|
76
67
|
map { |val| val.is_a?(Enumerable) ? val.expand : val }
|
77
68
|
end
|
78
69
|
|
79
70
|
def exponential(identity = 0, &block)
|
80
|
-
if
|
71
|
+
if defined?(yield)
|
81
72
|
map(&block).exponential(identity)
|
82
73
|
else
|
83
74
|
inject { |acc, val| acc**val } || identity
|
@@ -88,20 +79,12 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
88
79
|
each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
|
89
80
|
end
|
90
81
|
|
91
|
-
alias occurrences frequency
|
92
|
-
|
93
82
|
# rubocop:disable Style/CaseEquality
|
94
83
|
def incase?(object)
|
95
84
|
any? { |val| object === val }
|
96
85
|
end
|
97
86
|
# rubocop:enable Style/CaseEquality
|
98
87
|
|
99
|
-
def including(*elements)
|
100
|
-
to_a.including(*elements)
|
101
|
-
end
|
102
|
-
|
103
|
-
alias with including
|
104
|
-
|
105
88
|
# rubocop:disable Metrics/MethodLength
|
106
89
|
def interpose(sep, &block)
|
107
90
|
enum = Enumerator.new do |val|
|
@@ -128,19 +111,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
128
111
|
end
|
129
112
|
# rubocop:enable Metrics/MethodLength
|
130
113
|
|
131
|
-
def many?
|
132
|
-
found_count = 0
|
133
|
-
|
134
|
-
if block_given?
|
135
|
-
any? do |val|
|
136
|
-
found_count += 1 if yield(val)
|
137
|
-
found_count > 1
|
138
|
-
end
|
139
|
-
else
|
140
|
-
any? { (found_count += 1) > 1 }
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
114
|
def modulate(modulo)
|
145
115
|
if modulo == 1
|
146
116
|
to_a
|
@@ -153,7 +123,8 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
153
123
|
end
|
154
124
|
end
|
155
125
|
|
156
|
-
# rubocop:disable Metrics/AbcSize, Metrics/
|
126
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
127
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
157
128
|
def occur(amount = nil)
|
158
129
|
result = Hash.new { |hash, key| hash[key] = [] }
|
159
130
|
|
@@ -162,7 +133,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
162
133
|
result[key] << item
|
163
134
|
end
|
164
135
|
|
165
|
-
if
|
136
|
+
if defined?(yield)
|
166
137
|
result.select! { |_key, values| yield(values.size) }
|
167
138
|
else
|
168
139
|
raise ArgumentError, 'Invalid occur amount' unless amount
|
@@ -176,18 +147,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
176
147
|
|
177
148
|
result.values.flatten.uniq
|
178
149
|
end
|
179
|
-
# rubocop:enable Metrics/AbcSize, Metrics/
|
180
|
-
|
181
|
-
def pluck(*keys)
|
182
|
-
if keys.many?
|
183
|
-
map { |element| keys.map { |key| element[key] } }
|
184
|
-
else
|
185
|
-
map { |element| element[keys.first] }
|
186
|
-
end
|
187
|
-
end
|
150
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
151
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
188
152
|
|
189
153
|
def produce(identity = 0, &block)
|
190
|
-
if
|
154
|
+
if defined?(yield)
|
191
155
|
map(&block).produce(identity)
|
192
156
|
else
|
193
157
|
inject { |acc, val| acc * val } || identity
|
@@ -195,7 +159,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
195
159
|
end
|
196
160
|
|
197
161
|
def quotient(identity = 0, &block)
|
198
|
-
if
|
162
|
+
if defined?(yield)
|
199
163
|
map(&block).quotient(identity)
|
200
164
|
else
|
201
165
|
inject { |acc, val| acc / val } || identity
|
@@ -205,7 +169,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
205
169
|
def several?
|
206
170
|
found_count = 0
|
207
171
|
|
208
|
-
if
|
172
|
+
if defined?(yield)
|
209
173
|
each { |*opt| found_count += 1 if yield(*opt) }
|
210
174
|
else
|
211
175
|
each { |opt| found_count += 1 if opt }
|
@@ -246,5 +210,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('enumerable')
|
|
246
210
|
end
|
247
211
|
end
|
248
212
|
|
213
|
+
alias occurrences frequency
|
214
|
+
|
249
215
|
end
|
250
216
|
end
|
data/lib/lite/ruby/hash.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
4
|
+
require 'lite/ruby/safe/hash' unless defined?(ActiveSupport)
|
5
|
+
|
4
6
|
class Hash
|
5
7
|
|
6
8
|
class << self
|
@@ -102,7 +104,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
102
104
|
end
|
103
105
|
# rubocop:enable Style/GuardClause
|
104
106
|
|
105
|
-
# rubocop:disable Metrics/MethodLength
|
107
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
106
108
|
def collate(*others)
|
107
109
|
hash = {}
|
108
110
|
|
@@ -121,18 +123,18 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
121
123
|
hash.each_value(&:flatten!)
|
122
124
|
hash
|
123
125
|
end
|
124
|
-
# rubocop:enable Metrics/MethodLength
|
126
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
125
127
|
|
126
128
|
def collate!(other_hash)
|
127
129
|
replace(collate(other_hash))
|
128
130
|
end
|
129
131
|
|
130
|
-
def collect_keys
|
131
|
-
|
132
|
+
def collect_keys(&block)
|
133
|
+
keys.map(&block)
|
132
134
|
end
|
133
135
|
|
134
|
-
def collect_values
|
135
|
-
|
136
|
+
def collect_values(&block)
|
137
|
+
values.map(&block)
|
136
138
|
end
|
137
139
|
|
138
140
|
def dearray_values(idx = 0)
|
@@ -161,39 +163,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
161
163
|
replace(dearray_singular_values)
|
162
164
|
end
|
163
165
|
|
164
|
-
# rubocop:disable Style/CaseEquality
|
165
|
-
def deep_dup
|
166
|
-
hash = dup
|
167
|
-
|
168
|
-
each_pair do |key, value|
|
169
|
-
if key.frozen? && ::String === key
|
170
|
-
hash[key] = value.deep_dup
|
171
|
-
else
|
172
|
-
hash.delete(key)
|
173
|
-
hash[key.deep_dup] = value.deep_dup
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
hash
|
178
|
-
end
|
179
|
-
# rubocop:enable Style/CaseEquality
|
180
|
-
|
181
|
-
def deep_merge(other_hash, &block)
|
182
|
-
dup.deep_merge!(other_hash, &block)
|
183
|
-
end
|
184
|
-
|
185
|
-
def deep_merge!(other_hash, &block)
|
186
|
-
merge!(other_hash) do |key, this_val, other_val|
|
187
|
-
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
188
|
-
this_val.deep_merge(other_val, &block)
|
189
|
-
elsif block_given?
|
190
|
-
yield(key, this_val, other_val)
|
191
|
-
else
|
192
|
-
other_val
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
166
|
def delete_unless
|
198
167
|
delete_if { |key, val| !yield(key, val) }
|
199
168
|
end
|
@@ -232,18 +201,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
232
201
|
h1.merge(h2)
|
233
202
|
end
|
234
203
|
|
235
|
-
def except(*keys)
|
236
|
-
dup.except!(*keys)
|
237
|
-
end
|
238
|
-
|
239
|
-
def except!(*keys)
|
240
|
-
keys.each_with_object(self) { |key, _| delete(key) }
|
241
|
-
end
|
242
|
-
|
243
|
-
def extract!(*keys)
|
244
|
-
keys.each_with_object(self) { |key, hash| hash[key] = delete(key) if key?(key) }
|
245
|
-
end
|
246
|
-
|
247
204
|
def hmap(&block)
|
248
205
|
dup.hmap!(&block)
|
249
206
|
end
|
@@ -274,15 +231,15 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
274
231
|
unknown_keys.empty?
|
275
232
|
end
|
276
233
|
|
277
|
-
alias has_keys? keys?
|
278
|
-
|
279
234
|
def nillify
|
280
235
|
dup.nillify!
|
281
236
|
end
|
282
237
|
|
283
238
|
def nillify!
|
284
239
|
each do |key, val|
|
285
|
-
|
240
|
+
next if val.nil?
|
241
|
+
|
242
|
+
self[key] = nil if respond_to?(:blank?) ? val.blank? : !val
|
286
243
|
end
|
287
244
|
end
|
288
245
|
|
@@ -299,8 +256,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
299
256
|
unknown_keys.empty?
|
300
257
|
end
|
301
258
|
|
302
|
-
alias has_only_keys? only_keys?
|
303
|
-
|
304
259
|
def pair?(key, value)
|
305
260
|
self[key] == value
|
306
261
|
end
|
@@ -324,14 +279,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
324
279
|
keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] }
|
325
280
|
end
|
326
281
|
|
327
|
-
def reverse_merge(other_hash)
|
328
|
-
other_hash.merge(self)
|
329
|
-
end
|
330
|
-
|
331
|
-
def reverse_merge!(other_hash)
|
332
|
-
replace(reverse_merge(other_hash))
|
333
|
-
end
|
334
|
-
|
335
282
|
def sample
|
336
283
|
key = sample_key
|
337
284
|
[key, fetch(key)]
|
@@ -349,7 +296,7 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
349
296
|
end
|
350
297
|
|
351
298
|
def sample_key!
|
352
|
-
key, = sample
|
299
|
+
key, _val = sample
|
353
300
|
delete(key)
|
354
301
|
key
|
355
302
|
end
|
@@ -381,55 +328,28 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
381
328
|
omit
|
382
329
|
end
|
383
330
|
|
384
|
-
alias only slice
|
385
|
-
alias only! slice!
|
386
|
-
|
387
|
-
def stringify_keys
|
388
|
-
each_with_object({}) { |(key, val), hash| hash[key.to_s] = val }
|
389
|
-
end
|
390
|
-
|
391
|
-
def stringify_keys!
|
392
|
-
replace(stringify_keys)
|
393
|
-
end
|
394
|
-
|
395
331
|
def strip
|
396
|
-
|
332
|
+
reject { |_, val| respond_to?(:blank?) ? val.blank? : !val }
|
397
333
|
end
|
398
334
|
|
399
335
|
def strip!
|
400
|
-
reject! { |_, val| val.blank? }
|
401
|
-
end
|
402
|
-
|
403
|
-
def symbolize_keys
|
404
|
-
each_with_object({}) do |(key, val), hash|
|
405
|
-
new_key = begin
|
406
|
-
key.to_s.to_sym
|
407
|
-
rescue StandardError
|
408
|
-
key
|
409
|
-
end
|
410
|
-
|
411
|
-
hash[new_key] = val
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
def symbolize_keys!
|
416
|
-
replace(symbolize_keys)
|
336
|
+
reject! { |_, val| respond_to?(:blank?) ? val.blank? : !val }
|
417
337
|
end
|
418
338
|
|
419
339
|
# rubocop:disable Metrics/MethodLength
|
420
340
|
def symbolize_and_underscore_keys
|
421
341
|
each_with_object({}) do |(key, val), hash|
|
422
342
|
new_key = begin
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
343
|
+
str = key.dup.to_s
|
344
|
+
str = str.gsub!(/::/, '/') || str
|
345
|
+
str = str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str
|
346
|
+
str = str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str
|
347
|
+
str = str.tr!(' -', '_') || str
|
348
|
+
str = str.downcase!
|
349
|
+
str.to_sym
|
350
|
+
rescue StandardError
|
351
|
+
key
|
352
|
+
end
|
433
353
|
|
434
354
|
hash[new_key] = val
|
435
355
|
end
|
@@ -444,8 +364,6 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
444
364
|
JSON.parse(to_json, object_class: OpenStruct)
|
445
365
|
end
|
446
366
|
|
447
|
-
alias to_o to_object
|
448
|
-
|
449
367
|
def to_open_struct(lazy: true)
|
450
368
|
struct = OpenStruct.new(self)
|
451
369
|
struct.methods(lazy)
|
@@ -462,13 +380,13 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
462
380
|
end
|
463
381
|
|
464
382
|
def update_keys
|
465
|
-
return to_enum(:update_keys) unless
|
383
|
+
return to_enum(:update_keys) unless defined?(yield)
|
466
384
|
|
467
385
|
replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
|
468
386
|
end
|
469
387
|
|
470
388
|
def update_values
|
471
|
-
return to_enum(:update_values) unless
|
389
|
+
return to_enum(:update_values) unless defined?(yield)
|
472
390
|
|
473
391
|
replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
|
474
392
|
end
|
@@ -478,5 +396,13 @@ if Lite::Ruby.configuration.monkey_patches.include?('hash')
|
|
478
396
|
respond_to?(:blank?) ? value.blank? : !value
|
479
397
|
end
|
480
398
|
|
399
|
+
alias has_keys? keys?
|
400
|
+
alias has_only_keys? only_keys?
|
401
|
+
alias map_keys collect_keys
|
402
|
+
alias map_values collect_values
|
403
|
+
alias only slice
|
404
|
+
alias only! slice!
|
405
|
+
alias to_o to_object
|
406
|
+
|
481
407
|
end
|
482
408
|
end
|