lite-ruby 1.0.30 → 1.1.3

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.
@@ -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
@@ -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 block_given?
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 block_given?
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 block_given?
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/CyclomaticComplexity, Metrics/MethodLength
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 block_given?
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/CyclomaticComplexity, Metrics/MethodLength
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 block_given?
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 block_given?
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 block_given?
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
@@ -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/CyclomaticComplexity, 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/CyclomaticComplexity, 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
- collect { |key, _| yield(key) }
132
+ def collect_keys(&block)
133
+ keys.map(&block)
132
134
  end
133
135
 
134
- def collect_values
135
- collect { |_, val| yield(val) }
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
- self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?)
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
- select { |_, val| !val.blank? }
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
- key.to_s
424
- .gsub(/::/, '/')
425
- .gsub(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
426
- .gsub(/([a-z\d])([A-Z])/, '\1_\2')
427
- .tr(' -', '_')
428
- .downcase
429
- .to_sym
430
- rescue StandardError
431
- key
432
- end
343
+ str = key.dup.to_s
344
+ str.gsub!(/::/, '/') || str
345
+ str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str
346
+ str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str
347
+ str.tr!(' -', '_') || str
348
+ 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 block_given?
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 block_given?
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
@@ -9,9 +9,9 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
9
9
  hour: '%H',
10
10
  hour_blank: '%k',
11
11
  hour_padded: '%H',
12
- hour_12: '%I',
13
- hour_12_padded: '%I',
14
- hour_12_blank: '%l',
12
+ hour12: '%I',
13
+ hour12_padded: '%I',
14
+ hour12_blank: '%l',
15
15
  minute: '%M',
16
16
  second: '%S',
17
17
  ampm: '%P',
@@ -25,29 +25,29 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
25
25
  time_padded: '%H:%M',
26
26
  time_tz: '%H:%M %z',
27
27
  time_tzn: '%H:%M %Z',
28
- time_12: '%I:%M %P',
29
- time_12_padded: '%I:%M %P',
30
- time_12_blank: '%l:%M %P',
31
- time_12_tz: '%I:%M %P %z',
32
- time_12_tzn: '%I:%M %P %Z',
28
+ time12: '%I:%M %P',
29
+ time12_padded: '%I:%M %P',
30
+ time12_blank: '%l:%M %P',
31
+ time12_tz: '%I:%M %P %z',
32
+ time12_tzn: '%I:%M %P %Z',
33
33
  daytime: '%B %-d %H:%M',
34
34
  daytime_abbr: '%b %-d %H:%M',
35
35
  daytime_iso: '%m-%d %H:%M',
36
- daytime_12: '%B %-d %I:%M %P',
37
- daytime_12_abbr: '%b %-d %I:%M %P',
38
- daytime_12_iso: '%m-%d %I:%M %P',
36
+ daytime12: '%B %-d %I:%M %P',
37
+ daytime12_abbr: '%b %-d %I:%M %P',
38
+ daytime12_iso: '%m-%d %I:%M %P',
39
39
  datetime: '%B %-d, %Y %H:%M',
40
40
  datetime_abbr: '%b %-d, %Y %H:%M',
41
41
  datetime_iso: '%Y-%m-%d %H:%M',
42
- datetime_12: '%B %-d, %Y %I:%M %P',
43
- datetime_12_abbr: '%b %-d, %Y %I:%M %P',
44
- datetime_12_iso: '%Y-%m-%d %I:%M %P',
42
+ datetime12: '%B %-d, %Y %I:%M %P',
43
+ datetime12_abbr: '%b %-d, %Y %I:%M %P',
44
+ datetime12_iso: '%Y-%m-%d %I:%M %P',
45
45
  datetime_tzn: '%B %-d, %Y %H:%M %Z',
46
46
  datetime_abbr_tzn: '%b %-d, %Y %H:%M %Z',
47
47
  datetime_iso_tzn: '%Y-%m-%d %H:%M %z',
48
- datetime_12_tzn: '%B %-d, %Y %I:%M %P %Z',
49
- datetime_12_abbr_tzn: '%b %-d, %Y %I:%M %P %Z',
50
- datetime_12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
48
+ datetime12_tzn: '%B %-d, %Y %I:%M %P %Z',
49
+ datetime12_abbr_tzn: '%b %-d, %Y %I:%M %P %Z',
50
+ datetime12_iso_tzn: '%Y-%m-%d %I:%M %P %z'
51
51
  }.freeze
52
52
  TIME_UNITS = {
53
53
  h: 'H',
@@ -57,11 +57,11 @@ if Lite::Ruby.configuration.monkey_patches.include?('time')
57
57
  HOUR: 'k',
58
58
  hour_blank: 'k',
59
59
  hhh: 'I',
60
- hour_12: 'I',
61
- hour_12_padded: 'I',
60
+ hour12: 'I',
61
+ hour12_padded: 'I',
62
62
  hhhh: 'l',
63
- HOUR_12: 'l',
64
- hour_12_blank: 'l',
63
+ HOUR12: 'l',
64
+ hour12_blank: 'l',
65
65
  n: 'M',
66
66
  minute: 'M',
67
67
  s: 'S',