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 +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +22 -6
- data/docs/HASH.md +13 -0
- data/lib/generators/lite/ruby/templates/install.rb +14 -6
- data/lib/lite/ruby.rb +1 -14
- data/lib/lite/ruby/array.rb +218 -220
- data/lib/lite/ruby/boolean.rb +17 -19
- data/lib/lite/ruby/date.rb +13 -20
- data/lib/lite/ruby/enumerable.rb +153 -155
- data/lib/lite/ruby/formats/date_stamps.yml +34 -0
- data/lib/lite/ruby/formats/date_units.yml +42 -0
- data/lib/lite/ruby/formats/integer_roman_numerals.yml +13 -0
- data/lib/lite/ruby/formats/string_transliterations.yml +189 -0
- data/lib/lite/ruby/formats/time_stamps.yml +42 -0
- data/lib/lite/ruby/formats/time_units.yml +28 -0
- data/lib/lite/ruby/hash.rb +313 -301
- data/lib/lite/ruby/helpers/date_time_helper.rb +24 -0
- data/lib/lite/ruby/integer.rb +62 -63
- data/lib/lite/ruby/kernel.rb +19 -21
- data/lib/lite/ruby/numeric.rb +175 -177
- data/lib/lite/ruby/object.rb +128 -130
- data/lib/lite/ruby/open_struct.rb +17 -19
- data/lib/lite/ruby/range.rb +19 -21
- data/lib/lite/ruby/safe/string.rb +8 -7
- data/lib/lite/ruby/string.rb +348 -384
- data/lib/lite/ruby/struct.rb +7 -9
- data/lib/lite/ruby/time.rb +25 -30
- data/lib/lite/ruby/version.rb +1 -1
- metadata +8 -4
- data/lib/lite/ruby/configuration.rb +0 -38
- data/lib/lite/ruby/helpers/date_helper.rb +0 -107
- data/lib/lite/ruby/helpers/time_helper.rb +0 -86
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 82f5308d3cb93a5fbb19f679165de1a28ea6bd44feb5de03922e9738422e3c87
|
4
|
+
data.tar.gz: 5b0f162bc3bfb1a9c399a81dea94e4d863f9418e7bdd46fddb9bdc173b88e45a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
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
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
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'
|
data/lib/lite/ruby/array.rb
CHANGED
@@ -1,301 +1,299 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Array
|
3
|
+
class Array
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
def assert_min_values!(*valid_values)
|
6
|
+
return self if empty?
|
8
7
|
|
9
|
-
|
10
|
-
|
8
|
+
valid_values.each do |value|
|
9
|
+
next if include?(value)
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
21
|
-
|
16
|
+
self
|
17
|
+
end
|
22
18
|
|
23
|
-
|
24
|
-
|
19
|
+
def assert_all_min_values!(*valid_values)
|
20
|
+
return assert_min_values!(*valid_values) unless empty?
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
next if valid_values.include?(value)
|
22
|
+
raise ArgumentError, 'An empty array is not allowed'
|
23
|
+
end
|
29
24
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
37
|
-
|
35
|
+
def assert_all_valid_values!(*valid_values)
|
36
|
+
return assert_valid_values!(*valid_values) unless empty?
|
38
37
|
|
39
|
-
|
40
|
-
|
38
|
+
raise ArgumentError, 'An empty array is not allowed'
|
39
|
+
end
|
41
40
|
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
def assert_value_presence!
|
42
|
+
each do |value|
|
43
|
+
next if value.respond_to?(:present?) ? value.present? : value
|
45
44
|
|
46
|
-
|
47
|
-
end
|
45
|
+
raise ArgumentError, "A #{value.inspect} value is not allowed"
|
48
46
|
end
|
47
|
+
end
|
49
48
|
|
50
|
-
|
51
|
-
|
49
|
+
def assert_all_value_presence!
|
50
|
+
return assert_value_presence! unless empty?
|
52
51
|
|
53
|
-
|
54
|
-
|
52
|
+
raise ArgumentError, 'An empty array is not allowed'
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
55
|
+
def after(value)
|
56
|
+
return unless include?(value)
|
58
57
|
|
59
|
-
|
60
|
-
|
58
|
+
self[(index(value) + 1) % size]
|
59
|
+
end
|
61
60
|
|
62
|
-
|
63
|
-
|
61
|
+
def all_after(value)
|
62
|
+
return unless include?(value)
|
64
63
|
|
65
|
-
|
66
|
-
|
64
|
+
i = index(value)
|
65
|
+
return if i == (size - 1)
|
67
66
|
|
68
|
-
|
69
|
-
|
67
|
+
self[(i + 1)..-1]
|
68
|
+
end
|
70
69
|
|
71
|
-
|
72
|
-
|
70
|
+
def all_before(value)
|
71
|
+
return unless include?(value)
|
73
72
|
|
74
|
-
|
75
|
-
|
73
|
+
i = index(value)
|
74
|
+
return if i.zero?
|
76
75
|
|
77
|
-
|
78
|
-
|
76
|
+
self[0..(i - 1)]
|
77
|
+
end
|
79
78
|
|
80
|
-
|
81
|
-
|
79
|
+
def before(value)
|
80
|
+
return unless include?(value)
|
82
81
|
|
83
|
-
|
84
|
-
|
82
|
+
self[(index(value) - 1) % size]
|
83
|
+
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
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
|
-
|
117
|
-
|
118
|
-
|
106
|
+
self
|
107
|
+
end
|
108
|
+
# rubocop:enable Style/GuardClause, Style/IfInsideElse
|
109
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
|
119
110
|
|
120
|
-
|
121
|
-
|
122
|
-
|
111
|
+
def contains_all?(other)
|
112
|
+
(other & self) == self
|
113
|
+
end
|
123
114
|
|
124
|
-
|
125
|
-
|
126
|
-
|
115
|
+
def contains_any?(other)
|
116
|
+
!contains_none?(other)
|
117
|
+
end
|
127
118
|
|
128
|
-
|
129
|
-
|
130
|
-
|
119
|
+
def contains_none?(other)
|
120
|
+
(other & self).empty?
|
121
|
+
end
|
131
122
|
|
132
|
-
|
133
|
-
|
134
|
-
|
123
|
+
def delete_first
|
124
|
+
self[1..-1]
|
125
|
+
end
|
135
126
|
|
136
|
-
|
137
|
-
|
138
|
-
|
127
|
+
def delete_first!
|
128
|
+
replace(delete_first)
|
129
|
+
end
|
139
130
|
|
140
|
-
|
141
|
-
|
142
|
-
|
131
|
+
def delete_last
|
132
|
+
self[0...-1]
|
133
|
+
end
|
143
134
|
|
144
|
-
|
145
|
-
|
146
|
-
|
135
|
+
def delete_last!
|
136
|
+
replace(delete_last)
|
137
|
+
end
|
147
138
|
|
148
|
-
|
149
|
-
|
150
|
-
|
139
|
+
def delete_values(*args)
|
140
|
+
args.each_with_object([]) { |val, array| array << delete(val) }
|
141
|
+
end
|
151
142
|
|
152
|
-
|
153
|
-
|
154
|
-
|
143
|
+
def demote(value)
|
144
|
+
sort_by { |val| val == value ? 0 : -1 }
|
145
|
+
end
|
155
146
|
|
156
|
-
|
157
|
-
|
158
|
-
|
147
|
+
def demote!(value)
|
148
|
+
replace(demote(value))
|
149
|
+
end
|
159
150
|
|
160
|
-
|
161
|
-
|
162
|
-
|
151
|
+
def denillify(identity = 0)
|
152
|
+
map { |val| val || identity }
|
153
|
+
end
|
163
154
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
hash.delete_if { |_, val| val < minimum }.keys
|
168
|
-
end
|
155
|
+
def denillify!(identity = 0)
|
156
|
+
replace(denillify(identity))
|
157
|
+
end
|
169
158
|
|
170
|
-
|
171
|
-
|
172
|
-
|
159
|
+
def divergence(other)
|
160
|
+
(self - other) | (other - self)
|
161
|
+
end
|
173
162
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
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
|
-
|
180
|
-
|
169
|
+
def except(*values)
|
170
|
+
reject { |val| values.include?(val) }
|
171
|
+
end
|
181
172
|
|
182
|
-
|
183
|
-
|
173
|
+
def except!(*values)
|
174
|
+
reject! { |val| values.include?(val) }
|
175
|
+
self
|
176
|
+
end
|
184
177
|
|
185
|
-
|
186
|
-
|
178
|
+
def fulfill(value, amount)
|
179
|
+
return self if amount <= size
|
187
180
|
|
188
|
-
|
189
|
-
|
190
|
-
return collection unless rem.positive?
|
181
|
+
fill(value, size..(amount - 1))
|
182
|
+
end
|
191
183
|
|
192
|
-
|
193
|
-
|
184
|
+
def groups(number)
|
185
|
+
return [] if number <= 0
|
194
186
|
|
195
|
-
|
196
|
-
|
197
|
-
|
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
|
-
|
202
|
-
|
203
|
-
end
|
191
|
+
collection << self[-rem, rem]
|
192
|
+
end
|
204
193
|
|
205
|
-
|
206
|
-
|
207
|
-
|
194
|
+
def indexes(value)
|
195
|
+
array = []
|
196
|
+
each_with_index { |val, i| array << i if value == val }
|
197
|
+
array
|
198
|
+
end
|
208
199
|
|
209
|
-
|
210
|
-
|
211
|
-
|
200
|
+
def match(value)
|
201
|
+
find { |val| value == val }
|
202
|
+
end
|
212
203
|
|
213
|
-
|
214
|
-
|
215
|
-
|
204
|
+
def merge(*values)
|
205
|
+
dup.merge!(*values)
|
206
|
+
end
|
216
207
|
|
217
|
-
|
218
|
-
|
219
|
-
|
208
|
+
def merge!(*values)
|
209
|
+
values.each_with_object(self) { |val, arr| arr.concat(val) }
|
210
|
+
end
|
220
211
|
|
221
|
-
|
222
|
-
|
223
|
-
|
212
|
+
def nillify
|
213
|
+
map { |val| !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) ? nil : val }
|
214
|
+
end
|
224
215
|
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
end
|
216
|
+
def nillify!
|
217
|
+
replace(nillify)
|
218
|
+
end
|
229
219
|
|
230
|
-
|
231
|
-
|
232
|
-
|
220
|
+
def only(*values)
|
221
|
+
select { |val| values.include?(val) }
|
222
|
+
end
|
233
223
|
|
234
|
-
|
235
|
-
|
224
|
+
def only!(*values)
|
225
|
+
select! { |val| values.include?(val) }
|
226
|
+
self
|
227
|
+
end
|
236
228
|
|
237
|
-
|
238
|
-
|
239
|
-
|
229
|
+
def position(value)
|
230
|
+
idx = index(value)
|
231
|
+
return idx if idx.nil?
|
240
232
|
|
241
|
-
|
242
|
-
|
243
|
-
differ = 0.0
|
233
|
+
idx + 1
|
234
|
+
end
|
244
235
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
end
|
236
|
+
def positions(value)
|
237
|
+
indexes(value).map { |val| val + 1 }
|
238
|
+
end
|
249
239
|
|
250
|
-
|
251
|
-
|
252
|
-
|
240
|
+
def probability
|
241
|
+
hash = Hash.new(0.0)
|
242
|
+
differ = 0.0
|
253
243
|
|
254
|
-
|
255
|
-
|
244
|
+
each do |val|
|
245
|
+
hash[val] += 1.0
|
246
|
+
differ += 1.0
|
256
247
|
end
|
257
248
|
|
258
|
-
|
259
|
-
|
260
|
-
|
249
|
+
hash.each_key { |val| hash[val] /= differ }
|
250
|
+
hash
|
251
|
+
end
|
261
252
|
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
end
|
253
|
+
def promote(value)
|
254
|
+
sort_by { |val| val == value ? -1 : 0 }
|
255
|
+
end
|
266
256
|
|
267
|
-
|
268
|
-
|
269
|
-
|
257
|
+
def promote!(value)
|
258
|
+
sort_by! { |val| val == value ? -1 : 0 }
|
259
|
+
end
|
270
260
|
|
271
|
-
|
272
|
-
|
261
|
+
def rand_sample(max = nil)
|
262
|
+
amount = rand(1..(max || size))
|
263
|
+
sample(amount)
|
264
|
+
end
|
273
265
|
|
274
|
-
|
275
|
-
|
276
|
-
|
266
|
+
def rposition(value)
|
267
|
+
idx = rindex(value)
|
268
|
+
return idx if idx.nil?
|
277
269
|
|
278
|
-
|
279
|
-
|
280
|
-
end
|
270
|
+
idx + 1
|
271
|
+
end
|
281
272
|
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
end
|
273
|
+
def sample!
|
274
|
+
delete_at(Random.rand(size - 1))
|
275
|
+
end
|
286
276
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
end
|
277
|
+
def strip
|
278
|
+
reject(&:blank?)
|
279
|
+
end
|
291
280
|
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
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
|
-
|
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)
|