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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 191d0fb7e901d29b7faa905a312fee5928439267a5f30d3e729ba76c5545de98
4
- data.tar.gz: 1549c4736eeed76d892858775d32cd3914c6baed19d25b0bd4becc164d9cfc60
3
+ metadata.gz: 82f5308d3cb93a5fbb19f679165de1a28ea6bd44feb5de03922e9738422e3c87
4
+ data.tar.gz: 5b0f162bc3bfb1a9c399a81dea94e4d863f9418e7bdd46fddb9bdc173b88e45a
5
5
  SHA512:
6
- metadata.gz: bd7fb280764490cbfd9a581ae64d8985040bb49410169fd8b6c07e048dda78b995eb618155941f67e0ddfe32313def11ce8e3b86971f1564824d7de1c3177579
7
- data.tar.gz: b1981a0e40ffcb31ad5302ace5ecc094e548415fc7e128edc873a2d7b5797450a4c7d6bdfee0e363f90041a0a362f90bb602fa2a4c1085f29700cc6a33948ff4
6
+ metadata.gz: 0b41e76819eac622ca3e3f7cea273130875acb90c3c67d4288d0de74235ce01eba2fda951a54c419dfe148757f0c3c752ff2bd9d7d123db90db6793368f3eb8a
7
+ data.tar.gz: '09032bb0ce82ee5bf9a253bf3646991cf68fc782838d205fdd9dd90565ed6b56e7c24bd5475ec00b3eb040a1afd2ad229717a0ae63b73529b25da0c5e14692b7'
data/CHANGELOG.md CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [2.0.0] - 2021-07-22
10
+ ### Added
11
+ - Added Hash => `deep_key?`
12
+ ### Changed
13
+ - Update install generator to reflect configuration changes
14
+ ### Removed
15
+ - Removed configuration to use explicit inclusions
16
+
9
17
  ## [1.3.3] - 2021-07-21
10
18
  ### Changed
11
19
  - Improved generator support
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-ruby (1.3.3)
4
+ lite-ruby (2.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -33,16 +33,32 @@ Or install it yourself as:
33
33
 
34
34
  ## Configurations
35
35
 
36
+ Any and all monkey patches must be explicitly included anywhere you want to use it.
37
+
38
+ To globally use the money patches, just create an initializer requiring them.
39
+
36
40
  `rails g lite:ruby:install` will generate the following file:
37
41
  `../config/initalizers/lite_ruby.rb`
38
42
 
43
+ They can be disabled by commenting any of them out.
44
+
39
45
  ```ruby
40
- Lite::Ruby.configure do |config|
41
- config.monkey_patches = %w[
42
- array boolean date enumerable hash integer kernel numeric object open_struct range string
43
- struct time
44
- ]
45
- end
46
+ # frozen_string_literal: true
47
+
48
+ require 'lite/ruby/array'
49
+ require 'lite/ruby/boolean'
50
+ require 'lite/ruby/date'
51
+ require 'lite/ruby/enumerable'
52
+ require 'lite/ruby/hash'
53
+ require 'lite/ruby/integer'
54
+ require 'lite/ruby/kernel'
55
+ require 'lite/ruby/numeric'
56
+ require 'lite/ruby/object'
57
+ require 'lite/ruby/open_struct'
58
+ require 'lite/ruby/range'
59
+ require 'lite/ruby/string'
60
+ require 'lite/ruby/struct'
61
+ require 'lite/ruby/time'
46
62
  ```
47
63
 
48
64
  ## Extensions
data/docs/HASH.md CHANGED
@@ -160,6 +160,19 @@ h2 = { a: false, b: { x: [3, 4, 5] } }
160
160
  h1.deep_merge(h2) #=> { a: false, b: { c: [1, 2, 3], x: [3, 4, 5] } }
161
161
  ```
162
162
 
163
+ `deep_key`
164
+ ------
165
+ Returns if a set of nested keys exists.
166
+
167
+ ```ruby
168
+ h1 = { a: { c: { d: 2 } }, b: 1 }
169
+
170
+ h1.deep_key?(:a) #=> true
171
+ h1.deep_key?(:a, :c, :d) #=> true
172
+ h1.deep_key?(:x) #=> false
173
+ h1.deep_key?(:a, :c, :x) #=> false
174
+ ```
175
+
163
176
  `delete_unless`
164
177
  ------
165
178
  Inverse of `delete_if`.
@@ -1,8 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- Lite::Ruby.configure do |config|
4
- config.monkey_patches = %w[
5
- array boolean date enumerable hash integer kernel numeric object open_struct range string
6
- struct time
7
- ]
8
- end
3
+ require 'lite/ruby/array'
4
+ require 'lite/ruby/boolean'
5
+ require 'lite/ruby/date'
6
+ require 'lite/ruby/enumerable'
7
+ require 'lite/ruby/hash'
8
+ require 'lite/ruby/integer'
9
+ require 'lite/ruby/kernel'
10
+ require 'lite/ruby/numeric'
11
+ require 'lite/ruby/object'
12
+ require 'lite/ruby/open_struct'
13
+ require 'lite/ruby/range'
14
+ require 'lite/ruby/string'
15
+ require 'lite/ruby/struct'
16
+ require 'lite/ruby/time'
data/lib/lite/ruby.rb CHANGED
@@ -2,17 +2,4 @@
2
2
 
3
3
  require 'generators/lite/ruby/install_generator' if defined?(Rails::Generators)
4
4
 
5
- %w[version configuration].each do |filename|
6
- require "lite/ruby/#{filename}"
7
- end
8
-
9
- %w[date time].each do |filename|
10
- require "lite/ruby/helpers/#{filename}_helper"
11
- end
12
-
13
- %w[
14
- array boolean date enumerable hash integer kernel numeric object open_struct range string
15
- struct time
16
- ].each do |filename|
17
- require "lite/ruby/#{filename}"
18
- end
5
+ require 'lite/ruby/version'
@@ -1,301 +1,299 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Lite::Ruby.configuration.monkey_patches.include?('array')
4
- class Array
3
+ class Array
5
4
 
6
- def assert_min_values!(*valid_values)
7
- return self if empty?
5
+ def assert_min_values!(*valid_values)
6
+ return self if empty?
8
7
 
9
- valid_values.each do |value|
10
- next if include?(value)
8
+ valid_values.each do |value|
9
+ next if include?(value)
11
10
 
12
- raise ArgumentError,
13
- "Missing value: #{value.inspect}. " \
14
- "Minimum values are: #{valid_values.map(&:inspect).join(', ')}"
15
- end
16
-
17
- self
11
+ raise ArgumentError,
12
+ "Missing value: #{value.inspect}. " \
13
+ "Minimum values are: #{valid_values.map(&:inspect).join(', ')}"
18
14
  end
19
15
 
20
- def assert_all_min_values!(*valid_values)
21
- return assert_min_values!(*valid_values) unless empty?
16
+ self
17
+ end
22
18
 
23
- raise ArgumentError, 'An empty array is not allowed'
24
- end
19
+ def assert_all_min_values!(*valid_values)
20
+ return assert_min_values!(*valid_values) unless empty?
25
21
 
26
- def assert_valid_values!(*valid_values)
27
- each do |value|
28
- next if valid_values.include?(value)
22
+ raise ArgumentError, 'An empty array is not allowed'
23
+ end
29
24
 
30
- raise ArgumentError,
31
- "Invalid value: #{value.inspect}. " \
32
- "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
33
- end
25
+ def assert_valid_values!(*valid_values)
26
+ each do |value|
27
+ next if valid_values.include?(value)
28
+
29
+ raise ArgumentError,
30
+ "Invalid value: #{value.inspect}. " \
31
+ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
34
32
  end
33
+ end
35
34
 
36
- def assert_all_valid_values!(*valid_values)
37
- return assert_valid_values!(*valid_values) unless empty?
35
+ def assert_all_valid_values!(*valid_values)
36
+ return assert_valid_values!(*valid_values) unless empty?
38
37
 
39
- raise ArgumentError, 'An empty array is not allowed'
40
- end
38
+ raise ArgumentError, 'An empty array is not allowed'
39
+ end
41
40
 
42
- def assert_value_presence!
43
- each do |value|
44
- next if value.respond_to?(:present?) ? value.present? : value
41
+ def assert_value_presence!
42
+ each do |value|
43
+ next if value.respond_to?(:present?) ? value.present? : value
45
44
 
46
- raise ArgumentError, "A #{value.inspect} value is not allowed"
47
- end
45
+ raise ArgumentError, "A #{value.inspect} value is not allowed"
48
46
  end
47
+ end
49
48
 
50
- def assert_all_value_presence!
51
- return assert_value_presence! unless empty?
49
+ def assert_all_value_presence!
50
+ return assert_value_presence! unless empty?
52
51
 
53
- raise ArgumentError, 'An empty array is not allowed'
54
- end
52
+ raise ArgumentError, 'An empty array is not allowed'
53
+ end
55
54
 
56
- def after(value)
57
- return unless include?(value)
55
+ def after(value)
56
+ return unless include?(value)
58
57
 
59
- self[(index(value) + 1) % size]
60
- end
58
+ self[(index(value) + 1) % size]
59
+ end
61
60
 
62
- def all_after(value)
63
- return unless include?(value)
61
+ def all_after(value)
62
+ return unless include?(value)
64
63
 
65
- i = index(value)
66
- return if i == (size - 1)
64
+ i = index(value)
65
+ return if i == (size - 1)
67
66
 
68
- self[(i + 1)..-1]
69
- end
67
+ self[(i + 1)..-1]
68
+ end
70
69
 
71
- def all_before(value)
72
- return unless include?(value)
70
+ def all_before(value)
71
+ return unless include?(value)
73
72
 
74
- i = index(value)
75
- return if i.zero?
73
+ i = index(value)
74
+ return if i.zero?
76
75
 
77
- self[0..(i - 1)]
78
- end
76
+ self[0..(i - 1)]
77
+ end
79
78
 
80
- def before(value)
81
- return unless include?(value)
79
+ def before(value)
80
+ return unless include?(value)
82
81
 
83
- self[(index(value) - 1) % size]
84
- end
82
+ self[(index(value) - 1) % size]
83
+ end
85
84
 
86
- # rubocop:disable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
87
- # rubocop:disable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
88
- def bury(*args)
89
- if args.count < 2
90
- raise ArgumentError, '2 or more arguments required'
91
- elsif args.count == 2
92
- if args[0].is_a?(Integer)
93
- self[args[0]] = args[1]
94
- else
95
- self << { args[0] => args[1] }
96
- end
85
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
86
+ # rubocop:disable Style/GuardClause, Style/IfInsideElse
87
+ def bury(*args)
88
+ if args.count < 2
89
+ raise ArgumentError, '2 or more arguments required'
90
+ elsif args.count == 2
91
+ if args[0].is_a?(Integer)
92
+ self[args[0]] = args[1]
97
93
  else
98
- if args[0].is_a?(Integer)
99
- arg = args.shift
100
- self[arg] = [] unless self[arg]
101
- self[arg].bury(*args)
102
- else
103
- self << {}.bury(*args)
104
- end
94
+ self << { args[0] => args[1] }
95
+ end
96
+ else
97
+ if args[0].is_a?(Integer)
98
+ arg = args.shift
99
+ self[arg] = [] unless self[arg]
100
+ self[arg].bury(*args)
101
+ else
102
+ self << {}.bury(*args)
105
103
  end
106
-
107
- self
108
- end
109
- # rubocop:enable Metrics/PerceivedComplexity, Style/GuardClause, Style/IfInsideElse
110
- # rubocop:enable Metrics/AbcSize, Metrics/BlockNesting, Metrics/MethodLength
111
-
112
- def contains_all?(other)
113
- (other & self) == self
114
104
  end
115
105
 
116
- def contains_any?(other)
117
- !contains_none?(other)
118
- end
106
+ self
107
+ end
108
+ # rubocop:enable Style/GuardClause, Style/IfInsideElse
109
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
119
110
 
120
- def contains_none?(other)
121
- (other & self).empty?
122
- end
111
+ def contains_all?(other)
112
+ (other & self) == self
113
+ end
123
114
 
124
- def delete_first
125
- self[1..-1]
126
- end
115
+ def contains_any?(other)
116
+ !contains_none?(other)
117
+ end
127
118
 
128
- def delete_first!
129
- replace(delete_first)
130
- end
119
+ def contains_none?(other)
120
+ (other & self).empty?
121
+ end
131
122
 
132
- def delete_last
133
- self[0...-1]
134
- end
123
+ def delete_first
124
+ self[1..-1]
125
+ end
135
126
 
136
- def delete_last!
137
- replace(delete_last)
138
- end
127
+ def delete_first!
128
+ replace(delete_first)
129
+ end
139
130
 
140
- def delete_values(*args)
141
- args.each_with_object([]) { |val, array| array << delete(val) }
142
- end
131
+ def delete_last
132
+ self[0...-1]
133
+ end
143
134
 
144
- def demote(value)
145
- sort_by { |val| val == value ? 0 : -1 }
146
- end
135
+ def delete_last!
136
+ replace(delete_last)
137
+ end
147
138
 
148
- def demote!(value)
149
- replace(demote(value))
150
- end
139
+ def delete_values(*args)
140
+ args.each_with_object([]) { |val, array| array << delete(val) }
141
+ end
151
142
 
152
- def denillify(identity = 0)
153
- map { |val| val || identity }
154
- end
143
+ def demote(value)
144
+ sort_by { |val| val == value ? 0 : -1 }
145
+ end
155
146
 
156
- def denillify!(identity = 0)
157
- replace(denillify(identity))
158
- end
147
+ def demote!(value)
148
+ replace(demote(value))
149
+ end
159
150
 
160
- def divergence(other)
161
- (self - other) | (other - self)
162
- end
151
+ def denillify(identity = 0)
152
+ map { |val| val || identity }
153
+ end
163
154
 
164
- def duplicates(minimum = 2)
165
- hash = Hash.new(0)
166
- each { |val| hash[val] += 1 }
167
- hash.delete_if { |_, val| val < minimum }.keys
168
- end
155
+ def denillify!(identity = 0)
156
+ replace(denillify(identity))
157
+ end
169
158
 
170
- def except(*values)
171
- reject { |val| values.include?(val) }
172
- end
159
+ def divergence(other)
160
+ (self - other) | (other - self)
161
+ end
173
162
 
174
- def except!(*values)
175
- reject! { |val| values.include?(val) }
176
- self
177
- end
163
+ def duplicates(minimum = 2)
164
+ hash = Hash.new(0)
165
+ each { |val| hash[val] += 1 }
166
+ hash.delete_if { |_, val| val < minimum }.keys
167
+ end
178
168
 
179
- def fulfill(value, amount)
180
- return self if amount <= size
169
+ def except(*values)
170
+ reject { |val| values.include?(val) }
171
+ end
181
172
 
182
- fill(value, size..(amount - 1))
183
- end
173
+ def except!(*values)
174
+ reject! { |val| values.include?(val) }
175
+ self
176
+ end
184
177
 
185
- def groups(number)
186
- return [] if number <= 0
178
+ def fulfill(value, amount)
179
+ return self if amount <= size
187
180
 
188
- num, rem = size.divmod(number)
189
- collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
190
- return collection unless rem.positive?
181
+ fill(value, size..(amount - 1))
182
+ end
191
183
 
192
- collection << self[-rem, rem]
193
- end
184
+ def groups(number)
185
+ return [] if number <= 0
194
186
 
195
- def indexes(value)
196
- array = []
197
- each_with_index { |val, i| array << i if value == val }
198
- array
199
- end
187
+ num, rem = size.divmod(number)
188
+ collection = (0..(num - 1)).collect { |val| self[(val * number), number] }
189
+ return collection unless rem.positive?
200
190
 
201
- def match(value)
202
- find { |val| value == val }
203
- end
191
+ collection << self[-rem, rem]
192
+ end
204
193
 
205
- def merge(*values)
206
- dup.merge!(*values)
207
- end
194
+ def indexes(value)
195
+ array = []
196
+ each_with_index { |val, i| array << i if value == val }
197
+ array
198
+ end
208
199
 
209
- def merge!(*values)
210
- values.each_with_object(self) { |val, arr| arr.concat(val) }
211
- end
200
+ def match(value)
201
+ find { |val| value == val }
202
+ end
212
203
 
213
- def nillify
214
- map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
215
- end
204
+ def merge(*values)
205
+ dup.merge!(*values)
206
+ end
216
207
 
217
- def nillify!
218
- replace(nillify)
219
- end
208
+ def merge!(*values)
209
+ values.each_with_object(self) { |val, arr| arr.concat(val) }
210
+ end
220
211
 
221
- def only(*values)
222
- select { |val| values.include?(val) }
223
- end
212
+ def nillify
213
+ map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
214
+ end
224
215
 
225
- def only!(*values)
226
- select! { |val| values.include?(val) }
227
- self
228
- end
216
+ def nillify!
217
+ replace(nillify)
218
+ end
229
219
 
230
- def position(value)
231
- idx = index(value)
232
- return idx if idx.nil?
220
+ def only(*values)
221
+ select { |val| values.include?(val) }
222
+ end
233
223
 
234
- idx + 1
235
- end
224
+ def only!(*values)
225
+ select! { |val| values.include?(val) }
226
+ self
227
+ end
236
228
 
237
- def positions(value)
238
- indexes(value).map { |val| val + 1 }
239
- end
229
+ def position(value)
230
+ idx = index(value)
231
+ return idx if idx.nil?
240
232
 
241
- def probability
242
- hash = Hash.new(0.0)
243
- differ = 0.0
233
+ idx + 1
234
+ end
244
235
 
245
- each do |val|
246
- hash[val] += 1.0
247
- differ += 1.0
248
- end
236
+ def positions(value)
237
+ indexes(value).map { |val| val + 1 }
238
+ end
249
239
 
250
- hash.each_key { |val| hash[val] /= differ }
251
- hash
252
- end
240
+ def probability
241
+ hash = Hash.new(0.0)
242
+ differ = 0.0
253
243
 
254
- def promote(value)
255
- sort_by { |val| val == value ? -1 : 0 }
244
+ each do |val|
245
+ hash[val] += 1.0
246
+ differ += 1.0
256
247
  end
257
248
 
258
- def promote!(value)
259
- sort_by! { |val| val == value ? -1 : 0 }
260
- end
249
+ hash.each_key { |val| hash[val] /= differ }
250
+ hash
251
+ end
261
252
 
262
- def rand_sample(max = nil)
263
- amount = rand(1..(max || size))
264
- sample(amount)
265
- end
253
+ def promote(value)
254
+ sort_by { |val| val == value ? -1 : 0 }
255
+ end
266
256
 
267
- def rposition(value)
268
- idx = rindex(value)
269
- return idx if idx.nil?
257
+ def promote!(value)
258
+ sort_by! { |val| val == value ? -1 : 0 }
259
+ end
270
260
 
271
- idx + 1
272
- end
261
+ def rand_sample(max = nil)
262
+ amount = rand(1..(max || size))
263
+ sample(amount)
264
+ end
273
265
 
274
- def sample!
275
- delete_at(Random.rand(size - 1))
276
- end
266
+ def rposition(value)
267
+ idx = rindex(value)
268
+ return idx if idx.nil?
277
269
 
278
- def strip
279
- reject(&:blank?)
280
- end
270
+ idx + 1
271
+ end
281
272
 
282
- def strip!
283
- reject!(&:blank?)
284
- self
285
- end
273
+ def sample!
274
+ delete_at(Random.rand(size - 1))
275
+ end
286
276
 
287
- def swap(from, to)
288
- self[from], self[to] = self[to], self[from]
289
- self
290
- end
277
+ def strip
278
+ reject(&:blank?)
279
+ end
291
280
 
292
- alias indices indexes
293
- alias reject_values except
294
- alias reject_values! except!
295
- alias select_values only
296
- alias select_values! only!
281
+ def strip!
282
+ reject!(&:blank?)
283
+ self
284
+ end
297
285
 
286
+ def swap(from, to)
287
+ self[from], self[to] = self[to], self[from]
288
+ self
298
289
  end
299
290
 
300
- require 'lite/ruby/safe/array' unless defined?(ActiveSupport)
291
+ alias indices indexes
292
+ alias reject_values except
293
+ alias reject_values! except!
294
+ alias select_values only
295
+ alias select_values! only!
296
+
301
297
  end
298
+
299
+ require 'lite/ruby/safe/array' unless defined?(ActiveSupport)