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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -1
- data/Gemfile.lock +1 -1
- data/lib/lite/ruby/array.rb +221 -219
- data/lib/lite/ruby/boolean.rb +18 -16
- data/lib/lite/ruby/date.rb +18 -16
- data/lib/lite/ruby/enumerable.rb +169 -167
- data/lib/lite/ruby/hash.rb +309 -307
- data/lib/lite/ruby/helpers/date_helper.rb +97 -95
- data/lib/lite/ruby/helpers/time_helper.rb +79 -77
- data/lib/lite/ruby/integer.rb +57 -55
- data/lib/lite/ruby/kernel.rb +21 -19
- data/lib/lite/ruby/numeric.rb +189 -187
- data/lib/lite/ruby/object.rb +138 -136
- data/lib/lite/ruby/open_struct.rb +30 -28
- data/lib/lite/ruby/range.rb +23 -21
- data/lib/lite/ruby/string.rb +495 -493
- data/lib/lite/ruby/struct.rb +15 -13
- data/lib/lite/ruby/time.rb +27 -25
- data/lib/lite/ruby/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a56a9fed1ae0986131374f5cc62aeb0ff75db166a63bbb91a6530cbf15ac6ccc
|
4
|
+
data.tar.gz: f8f29474d5b6e38f978304278a442e96b6d1c98dd685bc234db1823c9452f67d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96f161d9040d4247cc924ccdea3318412828174ba2972e3a445faa0f0cfe7cd8e3ce71870420d78dbd884aa518f3cba467ace1507bf38c65787da3793903b0b1
|
7
|
+
data.tar.gz: 1498b714da8ea3a2f3a8ac542cf77f42e574bd1dadbe3a066e02bbaef9d012ec7f18b55cc29ede07aee4fdf5c95a3ef559b5eea67130e7baa1378daaaac4f35f
|
data/CHANGELOG.md
CHANGED
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
-
## [1.0.
|
9
|
+
## [1.0.15] - 2019-08-17
|
10
|
+
### Changed
|
11
|
+
- Added config check for each monkey patch
|
12
|
+
|
13
|
+
## [1.0.14] - 2019-08-17
|
10
14
|
### Changed
|
11
15
|
- Changed how hash deep_merge works
|
12
16
|
|
data/Gemfile.lock
CHANGED
data/lib/lite/ruby/array.rb
CHANGED
@@ -1,290 +1,292 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('array')
|
4
|
+
class Array
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def assert_valid_values!(*valid_values)
|
7
|
+
each do |value|
|
8
|
+
next if valid_values.include?(value)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
raise ArgumentError,
|
11
|
+
"Invalid value: #{value.inspect}." \
|
12
|
+
"Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
|
13
|
+
end
|
12
14
|
end
|
13
|
-
end
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
def assert_all_valid_values!(*valid_values)
|
17
|
+
return assert_valid_values!(*valid_values) unless empty?
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
raise ArgumentError, 'An empty array is not allowed'
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
def after(value)
|
23
|
+
return unless include?(value)
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
self[(index(value) + 1) % size]
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
def before(value)
|
29
|
+
return unless include?(value)
|
29
30
|
|
30
|
-
|
31
|
-
|
31
|
+
self[(index(value) - 1) % size]
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
else
|
45
|
-
if args[0].is_a?(Integer)
|
46
|
-
arg = args.shift
|
47
|
-
self[arg] = [] unless self[arg]
|
48
|
-
self[arg].bury(*args)
|
34
|
+
# rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
35
|
+
# rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
|
36
|
+
def bury(*args)
|
37
|
+
if args.count < 2
|
38
|
+
raise ArgumentError, '2 or more arguments required'
|
39
|
+
elsif args.count == 2
|
40
|
+
if args[0].is_a?(Integer)
|
41
|
+
self[args[0]] = args[1]
|
42
|
+
else
|
43
|
+
self << { args[0] => args[1] }
|
44
|
+
end
|
49
45
|
else
|
50
|
-
|
46
|
+
if args[0].is_a?(Integer)
|
47
|
+
arg = args.shift
|
48
|
+
self[arg] = [] unless self[arg]
|
49
|
+
self[arg].bury(*args)
|
50
|
+
else
|
51
|
+
self << {}.bury(*args)
|
52
|
+
end
|
51
53
|
end
|
54
|
+
|
55
|
+
self
|
52
56
|
end
|
57
|
+
# rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
|
58
|
+
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
53
59
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
60
|
+
def delete_first
|
61
|
+
self[1..-1]
|
62
|
+
end
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
|
64
|
+
def delete_first!
|
65
|
+
replace(delete_first)
|
66
|
+
end
|
62
67
|
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
def delete_last
|
69
|
+
self[0...-1]
|
70
|
+
end
|
66
71
|
|
67
|
-
|
68
|
-
|
69
|
-
|
72
|
+
def delete_last!
|
73
|
+
replace(delete_last)
|
74
|
+
end
|
70
75
|
|
71
|
-
|
72
|
-
|
73
|
-
|
76
|
+
def delete_values(*args)
|
77
|
+
args.each_with_object([]) { |val, array| array << delete(val) }
|
78
|
+
end
|
74
79
|
|
75
|
-
|
76
|
-
|
77
|
-
|
80
|
+
def demote(value)
|
81
|
+
sort_by { |val| val == value ? 0 : -1 }
|
82
|
+
end
|
78
83
|
|
79
|
-
|
80
|
-
|
81
|
-
|
84
|
+
def demote!(value)
|
85
|
+
replace(demote(value))
|
86
|
+
end
|
82
87
|
|
83
|
-
|
84
|
-
|
85
|
-
|
88
|
+
def denillify(identity = 0)
|
89
|
+
map { |val| val || identity }
|
90
|
+
end
|
86
91
|
|
87
|
-
|
88
|
-
|
89
|
-
|
92
|
+
def denillify!(identity = 0)
|
93
|
+
replace(denillify(identity))
|
94
|
+
end
|
90
95
|
|
91
|
-
|
92
|
-
|
93
|
-
|
96
|
+
def duplicates(minimum = 2)
|
97
|
+
hash = Hash.new(0)
|
98
|
+
each { |val| hash[val] += 1 }
|
99
|
+
hash.delete_if { |_, val| val < minimum }.keys
|
100
|
+
end
|
94
101
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
hash.delete_if { |_, val| val < minimum }.keys
|
99
|
-
end
|
102
|
+
def from(position)
|
103
|
+
self[position, size] || []
|
104
|
+
end
|
100
105
|
|
101
|
-
|
102
|
-
|
103
|
-
end
|
106
|
+
def fulfill(value, amount)
|
107
|
+
return self if amount <= size
|
104
108
|
|
105
|
-
|
106
|
-
|
109
|
+
fill(value, size..(amount - 1))
|
110
|
+
end
|
107
111
|
|
108
|
-
|
109
|
-
|
112
|
+
def groups(number)
|
113
|
+
return [] if number <= 0
|
110
114
|
|
111
|
-
|
112
|
-
|
115
|
+
num, rem = size.divmod(number)
|
116
|
+
collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
|
117
|
+
return collection unless rem.positive?
|
113
118
|
|
114
|
-
|
115
|
-
|
116
|
-
return collection unless rem.positive?
|
119
|
+
collection << self[-rem, rem]
|
120
|
+
end
|
117
121
|
|
118
|
-
|
119
|
-
|
122
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
123
|
+
def in_groups(number, fill_with = nil)
|
124
|
+
collection_size = size
|
125
|
+
division = collection_size.div(number)
|
126
|
+
modulo = collection_size % number
|
127
|
+
|
128
|
+
collection = []
|
129
|
+
start = 0
|
130
|
+
number.times do |int|
|
131
|
+
mod_gt_zero = modulo.positive?
|
132
|
+
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
133
|
+
collection << last_group = slice(start, grouping)
|
134
|
+
last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
|
135
|
+
start += grouping
|
136
|
+
end
|
120
137
|
|
121
|
-
|
122
|
-
def in_groups(number, fill_with = nil)
|
123
|
-
collection_size = size
|
124
|
-
division = collection_size.div(number)
|
125
|
-
modulo = collection_size % number
|
138
|
+
return collection unless block_given?
|
126
139
|
|
127
|
-
|
128
|
-
start = 0
|
129
|
-
number.times do |int|
|
130
|
-
mod_gt_zero = modulo.positive?
|
131
|
-
grouping = division + (mod_gt_zero && modulo > int ? 1 : 0)
|
132
|
-
collection << last_group = slice(start, grouping)
|
133
|
-
last_group << fill_with if (fill_with != false) && mod_gt_zero && (grouping == division)
|
134
|
-
start += grouping
|
140
|
+
collection.each { |val| yield(val) }
|
135
141
|
end
|
142
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
143
|
+
|
144
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
|
145
|
+
def in_groups_of(number, fill_with = nil)
|
146
|
+
if number.to_i <= 0
|
147
|
+
raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
|
148
|
+
elsif fill_with == false
|
149
|
+
collection = self
|
150
|
+
else
|
151
|
+
padding = (number - size % number) % number
|
152
|
+
collection = dup.concat(Array.new(padding, fill_with))
|
153
|
+
end
|
136
154
|
|
137
|
-
|
138
|
-
|
139
|
-
collection.each { |val| yield(val) }
|
140
|
-
end
|
141
|
-
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
|
155
|
+
sliced_collection = collection.each_slice(number)
|
156
|
+
return sliced_collection.to_a unless block_given?
|
142
157
|
|
143
|
-
|
144
|
-
def in_groups_of(number, fill_with = nil)
|
145
|
-
if number.to_i <= 0
|
146
|
-
raise ArgumentError, "Group size must be a positive integer, was #{number.inspect}"
|
147
|
-
elsif fill_with == false
|
148
|
-
collection = self
|
149
|
-
else
|
150
|
-
padding = (number - size % number) % number
|
151
|
-
collection = dup.concat(Array.new(padding, fill_with))
|
158
|
+
sliced_collection { |val| yield(val) }
|
152
159
|
end
|
160
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
|
153
161
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
|
162
|
+
def indexes(value)
|
163
|
+
array = []
|
164
|
+
each_with_index { |val, i| array << i if value == val }
|
165
|
+
array
|
166
|
+
end
|
160
167
|
|
161
|
-
|
162
|
-
array = []
|
163
|
-
each_with_index { |val, i| array << i if value == val }
|
164
|
-
array
|
165
|
-
end
|
168
|
+
alias indices indexes
|
166
169
|
|
167
|
-
|
170
|
+
def merge(*values)
|
171
|
+
dup.merge!(*values)
|
172
|
+
end
|
168
173
|
|
169
|
-
|
170
|
-
|
171
|
-
|
174
|
+
def merge!(*values)
|
175
|
+
values.each_with_object(self) { |val, arr| arr.concat(val) }
|
176
|
+
end
|
172
177
|
|
173
|
-
|
174
|
-
|
175
|
-
|
178
|
+
def nillify
|
179
|
+
map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
|
180
|
+
end
|
176
181
|
|
177
|
-
|
178
|
-
|
179
|
-
|
182
|
+
def nillify!
|
183
|
+
replace(nillify)
|
184
|
+
end
|
180
185
|
|
181
|
-
|
182
|
-
|
183
|
-
|
186
|
+
def position(value)
|
187
|
+
idx = index(value)
|
188
|
+
return idx if idx.nil?
|
184
189
|
|
185
|
-
|
186
|
-
|
187
|
-
return idx if idx.nil?
|
190
|
+
idx + 1
|
191
|
+
end
|
188
192
|
|
189
|
-
|
190
|
-
|
193
|
+
def positions(value)
|
194
|
+
indexes(value).map { |val| val + 1 }
|
195
|
+
end
|
191
196
|
|
192
|
-
|
193
|
-
|
194
|
-
|
197
|
+
def probability
|
198
|
+
hash = Hash.new(0.0)
|
199
|
+
differ = 0.0
|
195
200
|
|
196
|
-
|
197
|
-
|
198
|
-
|
201
|
+
each do |val|
|
202
|
+
hash[val] += 1.0
|
203
|
+
differ += 1.0
|
204
|
+
end
|
199
205
|
|
200
|
-
|
201
|
-
hash
|
202
|
-
differ += 1.0
|
206
|
+
hash.each_key { |val| hash[val] /= differ }
|
207
|
+
hash
|
203
208
|
end
|
204
209
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
def promote(value)
|
210
|
-
sort_by { |val| val == value ? -1 : 0 }
|
211
|
-
end
|
212
|
-
|
213
|
-
def promote!(value)
|
214
|
-
replace(promote(value))
|
215
|
-
end
|
210
|
+
def promote(value)
|
211
|
+
sort_by { |val| val == value ? -1 : 0 }
|
212
|
+
end
|
216
213
|
|
217
|
-
|
218
|
-
|
219
|
-
|
214
|
+
def promote!(value)
|
215
|
+
replace(promote(value))
|
216
|
+
end
|
220
217
|
|
221
|
-
|
222
|
-
|
223
|
-
|
218
|
+
def reject_values(*args)
|
219
|
+
reject { |val| args.include?(val) }
|
220
|
+
end
|
224
221
|
|
225
|
-
|
226
|
-
|
222
|
+
def rposition(value)
|
223
|
+
idx = rindex(value)
|
224
|
+
return idx if idx.nil?
|
227
225
|
|
228
|
-
|
229
|
-
|
230
|
-
end
|
226
|
+
idx + 1
|
227
|
+
end
|
231
228
|
|
232
|
-
|
233
|
-
|
234
|
-
|
229
|
+
def sample!
|
230
|
+
delete_at(Random.rand(size - 1))
|
231
|
+
end
|
235
232
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
dup_arr = dup
|
233
|
+
# rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
234
|
+
def split(number = nil)
|
235
|
+
array = [[]]
|
240
236
|
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
237
|
+
if block_given?
|
238
|
+
each { |val| yield(val) ? (array << []) : (array .last << val) }
|
239
|
+
else
|
240
|
+
dup_arr = dup
|
241
|
+
|
242
|
+
until dup_arr.empty?
|
243
|
+
if (idx = dup_arr.index(number))
|
244
|
+
array.last << dup_arr.shift(idx)
|
245
|
+
dup_arr.shift
|
246
|
+
array << []
|
247
|
+
else
|
248
|
+
array.last << arr.shift(dup_arr.size)
|
249
|
+
end
|
248
250
|
end
|
249
251
|
end
|
250
|
-
end
|
251
252
|
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
def strip
|
257
|
-
reject(&:blank?)
|
258
|
-
end
|
253
|
+
array
|
254
|
+
end
|
255
|
+
# rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
|
259
256
|
|
260
|
-
|
261
|
-
|
262
|
-
|
257
|
+
def strip
|
258
|
+
reject(&:blank?)
|
259
|
+
end
|
263
260
|
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
end
|
261
|
+
def strip!
|
262
|
+
replace(strip)
|
263
|
+
end
|
268
264
|
|
269
|
-
|
270
|
-
|
265
|
+
def swap(from, to)
|
266
|
+
self[from], self[to] = self[to], self[from]
|
267
|
+
self
|
268
|
+
end
|
271
269
|
|
272
|
-
|
273
|
-
|
270
|
+
def to(position)
|
271
|
+
return first(position + 1) if position >= 0
|
274
272
|
|
275
|
-
|
276
|
-
|
277
|
-
words_connector = options[:words_connector] || ', '
|
278
|
-
two_words_connector = options[:two_words_connector] || ' and '
|
279
|
-
last_word_connector = options[:last_word_connector] || ', and '
|
273
|
+
self[0..position]
|
274
|
+
end
|
280
275
|
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
276
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
277
|
+
def to_sentence(options = {})
|
278
|
+
words_connector = options[:words_connector] || ', '
|
279
|
+
two_words_connector = options[:two_words_connector] || ' and '
|
280
|
+
last_word_connector = options[:last_word_connector] || ', and '
|
281
|
+
|
282
|
+
case size
|
283
|
+
when 0 then ''
|
284
|
+
when 1 then self[0].to_s.dup
|
285
|
+
when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
|
286
|
+
else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
|
287
|
+
end
|
286
288
|
end
|
287
|
-
|
288
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
289
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
289
290
|
|
291
|
+
end
|
290
292
|
end
|
data/lib/lite/ruby/boolean.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('boolean')
|
4
|
+
class FalseClass
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def to_bool
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
alias to_b to_bool
|
8
11
|
|
9
|
-
|
12
|
+
def to_i
|
13
|
+
0
|
14
|
+
end
|
10
15
|
|
11
|
-
def to_i
|
12
|
-
0
|
13
16
|
end
|
14
17
|
|
15
|
-
|
18
|
+
class TrueClass
|
16
19
|
|
17
|
-
|
20
|
+
def to_bool
|
21
|
+
self
|
22
|
+
end
|
18
23
|
|
19
|
-
|
20
|
-
self
|
21
|
-
end
|
24
|
+
alias to_b to_bool
|
22
25
|
|
23
|
-
|
26
|
+
def to_i
|
27
|
+
1
|
28
|
+
end
|
24
29
|
|
25
|
-
def to_i
|
26
|
-
1
|
27
30
|
end
|
28
|
-
|
29
31
|
end
|
data/lib/lite/ruby/date.rb
CHANGED
@@ -1,27 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
if Lite::Ruby.configuration.monkey_patches.include?('date')
|
4
|
+
require 'date'
|
4
5
|
|
5
|
-
class Date
|
6
|
+
class Date
|
6
7
|
|
7
|
-
|
8
|
+
include Lite::Ruby::DateHelper
|
8
9
|
|
9
|
-
|
10
|
+
private
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def default_format
|
13
|
+
'year-month-day'
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def default_stamp
|
17
|
+
:date_iso
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def format_for(key)
|
21
|
+
DATE_UNITS[key]
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def stamp_for(key)
|
25
|
+
DATE_STAMPS[key]
|
26
|
+
end
|
26
27
|
|
28
|
+
end
|
27
29
|
end
|