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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e4c106fb56b079e4cbec739ab066194d9bd9ad04efd4f937d1e1dc517a99c0f7
4
- data.tar.gz: 256b601530f417a689026780dd0d70b6a75663fa7793733ee06ac0dbbc229ce5
3
+ metadata.gz: a56a9fed1ae0986131374f5cc62aeb0ff75db166a63bbb91a6530cbf15ac6ccc
4
+ data.tar.gz: f8f29474d5b6e38f978304278a442e96b6d1c98dd685bc234db1823c9452f67d
5
5
  SHA512:
6
- metadata.gz: 19ba4210ce3f80451a59958ab7420f714104fffe6ca8af39199842dc34d696247b065380466a82e4d0c0ca3e441cdc3bb4065ba0ccc54b6a474bf9f0fd5cc5cf
7
- data.tar.gz: cfd656670786c5b0f98ffbe92c484e4dd4f9fa10760e250a442bd6095a1c433641a47cebcd89dd8738b4c52916c0cd37bd12aecaa711db9ed42c536aa01d838c
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.13] - 2019-08-17
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-ruby (1.0.14)
4
+ lite-ruby (1.0.15)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,290 +1,292 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Array
3
+ if Lite::Ruby.configuration.monkey_patches.include?('array')
4
+ class Array
4
5
 
5
- def assert_valid_values!(*valid_values)
6
- each do |value|
7
- next if valid_values.include?(value)
6
+ def assert_valid_values!(*valid_values)
7
+ each do |value|
8
+ next if valid_values.include?(value)
8
9
 
9
- raise ArgumentError,
10
- "Invalid value: #{value.inspect}." \
11
- "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
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
- def assert_all_valid_values!(*valid_values)
16
- return assert_valid_values!(*valid_values) unless empty?
16
+ def assert_all_valid_values!(*valid_values)
17
+ return assert_valid_values!(*valid_values) unless empty?
17
18
 
18
- raise ArgumentError, 'An empty array is not allowed'
19
- end
19
+ raise ArgumentError, 'An empty array is not allowed'
20
+ end
20
21
 
21
- def after(value)
22
- return unless include?(value)
22
+ def after(value)
23
+ return unless include?(value)
23
24
 
24
- self[(index(value) + 1) % size]
25
- end
25
+ self[(index(value) + 1) % size]
26
+ end
26
27
 
27
- def before(value)
28
- return unless include?(value)
28
+ def before(value)
29
+ return unless include?(value)
29
30
 
30
- self[(index(value) - 1) % size]
31
- end
31
+ self[(index(value) - 1) % size]
32
+ end
32
33
 
33
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
34
- # rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
35
- def bury(*args)
36
- if args.count < 2
37
- raise ArgumentError, '2 or more arguments required'
38
- elsif args.count == 2
39
- if args[0].is_a?(Integer)
40
- self[args[0]] = args[1]
41
- else
42
- self << { args[0] => args[1] }
43
- end
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
- self << {}.bury(*args)
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
- self
55
- end
56
- # rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
57
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
60
+ def delete_first
61
+ self[1..-1]
62
+ end
58
63
 
59
- def delete_first
60
- self[1..-1]
61
- end
64
+ def delete_first!
65
+ replace(delete_first)
66
+ end
62
67
 
63
- def delete_first!
64
- replace(delete_first)
65
- end
68
+ def delete_last
69
+ self[0...-1]
70
+ end
66
71
 
67
- def delete_last
68
- self[0...-1]
69
- end
72
+ def delete_last!
73
+ replace(delete_last)
74
+ end
70
75
 
71
- def delete_last!
72
- replace(delete_last)
73
- end
76
+ def delete_values(*args)
77
+ args.each_with_object([]) { |val, array| array << delete(val) }
78
+ end
74
79
 
75
- def delete_values(*args)
76
- args.each_with_object([]) { |val, array| array << delete(val) }
77
- end
80
+ def demote(value)
81
+ sort_by { |val| val == value ? 0 : -1 }
82
+ end
78
83
 
79
- def demote(value)
80
- sort_by { |val| val == value ? 0 : -1 }
81
- end
84
+ def demote!(value)
85
+ replace(demote(value))
86
+ end
82
87
 
83
- def demote!(value)
84
- replace(demote(value))
85
- end
88
+ def denillify(identity = 0)
89
+ map { |val| val || identity }
90
+ end
86
91
 
87
- def denillify(identity = 0)
88
- map { |val| val || identity }
89
- end
92
+ def denillify!(identity = 0)
93
+ replace(denillify(identity))
94
+ end
90
95
 
91
- def denillify!(identity = 0)
92
- replace(denillify(identity))
93
- end
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
- def duplicates(minimum = 2)
96
- hash = Hash.new(0)
97
- each { |val| hash[val] += 1 }
98
- hash.delete_if { |_, val| val < minimum }.keys
99
- end
102
+ def from(position)
103
+ self[position, size] || []
104
+ end
100
105
 
101
- def from(position)
102
- self[position, size] || []
103
- end
106
+ def fulfill(value, amount)
107
+ return self if amount <= size
104
108
 
105
- def fulfill(value, amount)
106
- return self if amount <= size
109
+ fill(value, size..(amount - 1))
110
+ end
107
111
 
108
- fill(value, size..(amount - 1))
109
- end
112
+ def groups(number)
113
+ return [] if number <= 0
110
114
 
111
- def groups(number)
112
- return [] if number <= 0
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
- num, rem = size.divmod(number)
115
- collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
116
- return collection unless rem.positive?
119
+ collection << self[-rem, rem]
120
+ end
117
121
 
118
- collection << self[-rem, rem]
119
- end
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
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
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
- collection = []
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
- return collection unless block_given?
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
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Style/GuardClause
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
- sliced_collection = collection.each_slice(number)
155
- return sliced_collection.to_a unless block_given?
156
-
157
- sliced_collection { |val| yield(val) }
158
- end
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
- def indexes(value)
162
- array = []
163
- each_with_index { |val, i| array << i if value == val }
164
- array
165
- end
168
+ alias indices indexes
166
169
 
167
- alias indices indexes
170
+ def merge(*values)
171
+ dup.merge!(*values)
172
+ end
168
173
 
169
- def merge(*values)
170
- dup.merge!(*values)
171
- end
174
+ def merge!(*values)
175
+ values.each_with_object(self) { |val, arr| arr.concat(val) }
176
+ end
172
177
 
173
- def merge!(*values)
174
- values.each_with_object(self) { |val, arr| arr.concat(val) }
175
- end
178
+ def nillify
179
+ map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
180
+ end
176
181
 
177
- def nillify
178
- map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
179
- end
182
+ def nillify!
183
+ replace(nillify)
184
+ end
180
185
 
181
- def nillify!
182
- replace(nillify)
183
- end
186
+ def position(value)
187
+ idx = index(value)
188
+ return idx if idx.nil?
184
189
 
185
- def position(value)
186
- idx = index(value)
187
- return idx if idx.nil?
190
+ idx + 1
191
+ end
188
192
 
189
- idx + 1
190
- end
193
+ def positions(value)
194
+ indexes(value).map { |val| val + 1 }
195
+ end
191
196
 
192
- def positions(value)
193
- indexes(value).map { |val| val + 1 }
194
- end
197
+ def probability
198
+ hash = Hash.new(0.0)
199
+ differ = 0.0
195
200
 
196
- def probability
197
- hash = Hash.new(0.0)
198
- differ = 0.0
201
+ each do |val|
202
+ hash[val] += 1.0
203
+ differ += 1.0
204
+ end
199
205
 
200
- each do |val|
201
- hash[val] += 1.0
202
- differ += 1.0
206
+ hash.each_key { |val| hash[val] /= differ }
207
+ hash
203
208
  end
204
209
 
205
- hash.each_key { |val| hash[val] /= differ }
206
- hash
207
- end
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
- def reject_values(*args)
218
- reject { |val| args.include?(val) }
219
- end
214
+ def promote!(value)
215
+ replace(promote(value))
216
+ end
220
217
 
221
- def rposition(value)
222
- idx = rindex(value)
223
- return idx if idx.nil?
218
+ def reject_values(*args)
219
+ reject { |val| args.include?(val) }
220
+ end
224
221
 
225
- idx + 1
226
- end
222
+ def rposition(value)
223
+ idx = rindex(value)
224
+ return idx if idx.nil?
227
225
 
228
- def sample!
229
- delete_at(Random.rand(size - 1))
230
- end
226
+ idx + 1
227
+ end
231
228
 
232
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
233
- def split(number = nil)
234
- array = [[]]
229
+ def sample!
230
+ delete_at(Random.rand(size - 1))
231
+ end
235
232
 
236
- if block_given?
237
- each { |val| yield(val) ? (array << []) : (array .last << val) }
238
- else
239
- dup_arr = dup
233
+ # rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
234
+ def split(number = nil)
235
+ array = [[]]
240
236
 
241
- until dup_arr.empty?
242
- if (idx = dup_arr.index(number))
243
- array.last << dup_arr.shift(idx)
244
- dup_arr.shift
245
- array << []
246
- else
247
- array.last << arr.shift(dup_arr.size)
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
- array
253
- end
254
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
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
- def strip!
261
- replace(strip)
262
- end
257
+ def strip
258
+ reject(&:blank?)
259
+ end
263
260
 
264
- def swap(from, to)
265
- self[from], self[to] = self[to], self[from]
266
- self
267
- end
261
+ def strip!
262
+ replace(strip)
263
+ end
268
264
 
269
- def to(position)
270
- return first(position + 1) if position >= 0
265
+ def swap(from, to)
266
+ self[from], self[to] = self[to], self[from]
267
+ self
268
+ end
271
269
 
272
- self[0..position]
273
- end
270
+ def to(position)
271
+ return first(position + 1) if position >= 0
274
272
 
275
- # rubocop:disable Metrics/CyclomaticComplexity
276
- def to_sentence(options = {})
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
- case size
282
- when 0 then ''
283
- when 1 then self[0].to_s.dup
284
- when 2 then "#{self[0]}#{two_words_connector}#{self[1]}"
285
- else "#{self[0...-1].join(words_connector)}#{last_word_connector}#{self[-1]}"
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
- end
288
- # rubocop:enable Metrics/CyclomaticComplexity
289
+ # rubocop:enable Metrics/CyclomaticComplexity
289
290
 
291
+ end
290
292
  end
@@ -1,29 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class FalseClass
3
+ if Lite::Ruby.configuration.monkey_patches.include?('boolean')
4
+ class FalseClass
4
5
 
5
- def to_bool
6
- self
7
- end
6
+ def to_bool
7
+ self
8
+ end
9
+
10
+ alias to_b to_bool
8
11
 
9
- alias to_b to_bool
12
+ def to_i
13
+ 0
14
+ end
10
15
 
11
- def to_i
12
- 0
13
16
  end
14
17
 
15
- end
18
+ class TrueClass
16
19
 
17
- class TrueClass
20
+ def to_bool
21
+ self
22
+ end
18
23
 
19
- def to_bool
20
- self
21
- end
24
+ alias to_b to_bool
22
25
 
23
- alias to_b to_bool
26
+ def to_i
27
+ 1
28
+ end
24
29
 
25
- def to_i
26
- 1
27
30
  end
28
-
29
31
  end
@@ -1,27 +1,29 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'date'
3
+ if Lite::Ruby.configuration.monkey_patches.include?('date')
4
+ require 'date'
4
5
 
5
- class Date
6
+ class Date
6
7
 
7
- include Lite::Ruby::DateHelper
8
+ include Lite::Ruby::DateHelper
8
9
 
9
- private
10
+ private
10
11
 
11
- def default_format
12
- 'year-month-day'
13
- end
12
+ def default_format
13
+ 'year-month-day'
14
+ end
14
15
 
15
- def default_stamp
16
- :date_iso
17
- end
16
+ def default_stamp
17
+ :date_iso
18
+ end
18
19
 
19
- def format_for(key)
20
- DATE_UNITS[key]
21
- end
20
+ def format_for(key)
21
+ DATE_UNITS[key]
22
+ end
22
23
 
23
- def stamp_for(key)
24
- DATE_STAMPS[key]
25
- end
24
+ def stamp_for(key)
25
+ DATE_STAMPS[key]
26
+ end
26
27
 
28
+ end
27
29
  end