lite-ruby 1.3.1 → 2.0.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -1
- data/Gemfile.lock +1 -1
- data/README.md +26 -6
- data/docs/HASH.md +13 -0
- data/lib/generators/lite/ruby/install_generator.rb +0 -2
- data/lib/generators/lite/ruby/templates/install.rb +18 -6
- data/lib/lite/ruby.rb +2 -15
- data/lib/lite/ruby/all.rb +16 -0
- 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/object.rb +10 -8
- 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 +10 -5
- 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
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lite
|
4
|
+
module Ruby
|
5
|
+
module DateTimeHelper
|
6
|
+
|
7
|
+
def format(string = nil)
|
8
|
+
string ||= self.class::DEFAULT_UNIT
|
9
|
+
delimiters = string.scan(/\W+/)
|
10
|
+
formatters = string.scan(/[a-z0-9_]+/i)
|
11
|
+
string = formatters.map { |key| "%#{self.class::UNITS[key.to_s]}#{delimiters.shift}" }
|
12
|
+
strftime(string.join)
|
13
|
+
end
|
14
|
+
|
15
|
+
def stamp(key = nil)
|
16
|
+
key = key.nil? ? self.class::DEFAULT_STAMP : key.to_s
|
17
|
+
strftime(self.class::STAMPS[key])
|
18
|
+
end
|
19
|
+
|
20
|
+
alias to_format stamp
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/lite/ruby/integer.rb
CHANGED
@@ -1,85 +1,84 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Integer
|
5
|
-
|
6
|
-
ROMAN_NUMERALS = {
|
7
|
-
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50,
|
8
|
-
XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1
|
9
|
-
}.freeze
|
10
|
-
|
11
|
-
SQL_SMALLINT = 32_767
|
12
|
-
SQL_INTEGER = 2_147_483_647
|
13
|
-
SQL_BIGINT = 9_223_372_036_854_775_807
|
14
|
-
|
15
|
-
def bit(bit)
|
16
|
-
if bit.negative?
|
17
|
-
mask = (1 << ~bit)
|
18
|
-
self & ~mask
|
19
|
-
else
|
20
|
-
mask = (1 << bit)
|
21
|
-
self | mask
|
22
|
-
end
|
23
|
-
end
|
3
|
+
require 'yaml' unless defined?(YAML)
|
24
4
|
|
25
|
-
|
26
|
-
mask = (1 << bit)
|
27
|
-
(self & mask) != 0
|
28
|
-
end
|
5
|
+
class Integer
|
29
6
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
7
|
+
ROMAN_NUMERALS = YAML.load_file(
|
8
|
+
File.expand_path('formats/integer_roman_numerals.yml', File.dirname(__FILE__))
|
9
|
+
).freeze
|
34
10
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
else
|
39
|
-
self | mask
|
40
|
-
end
|
41
|
-
end
|
11
|
+
SQL_SMALLINT = 32_767
|
12
|
+
SQL_INTEGER = 2_147_483_647
|
13
|
+
SQL_BIGINT = 9_223_372_036_854_775_807
|
42
14
|
|
43
|
-
|
44
|
-
|
15
|
+
def bit(bit)
|
16
|
+
if bit.negative?
|
17
|
+
mask = (1 << ~bit)
|
18
|
+
self & ~mask
|
19
|
+
else
|
20
|
+
mask = (1 << bit)
|
21
|
+
self | mask
|
45
22
|
end
|
23
|
+
end
|
46
24
|
|
47
|
-
|
48
|
-
|
49
|
-
|
25
|
+
def bit?(bit)
|
26
|
+
mask = (1 << bit)
|
27
|
+
(self & mask) != 0
|
28
|
+
end
|
29
|
+
|
30
|
+
def bit_clear(bit)
|
31
|
+
mask = (1 << bit)
|
32
|
+
self & ~mask
|
33
|
+
end
|
50
34
|
|
51
|
-
|
52
|
-
|
35
|
+
def bitmask(mask)
|
36
|
+
if mask.negative?
|
37
|
+
self & mask
|
38
|
+
else
|
39
|
+
self | mask
|
53
40
|
end
|
41
|
+
end
|
54
42
|
|
55
|
-
|
56
|
-
|
43
|
+
def bitmask?(mask)
|
44
|
+
(self & mask) != 0
|
45
|
+
end
|
57
46
|
|
58
|
-
|
59
|
-
|
47
|
+
def combinatorial(num)
|
48
|
+
(0...num).inject(1) { |acc, i| (acc * (self - i)) / (i + 1) }
|
49
|
+
end
|
60
50
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
end
|
51
|
+
def factorial
|
52
|
+
(1..self).inject { |acc, i| acc * i } || 0
|
53
|
+
end
|
66
54
|
|
67
|
-
|
68
|
-
|
69
|
-
end
|
55
|
+
def factors
|
56
|
+
limit = Math.sqrt(self).floor
|
70
57
|
|
71
|
-
|
72
|
-
|
73
|
-
return "-#{(-self).roman_numeral}" if negative?
|
58
|
+
(1..limit).each_with_object([]) do |i, array|
|
59
|
+
next unless (self % i).zero?
|
74
60
|
|
75
|
-
|
61
|
+
sq = (self / i)
|
62
|
+
array.push(i)
|
63
|
+
array.push(sq) if sq != i
|
76
64
|
end
|
65
|
+
end
|
77
66
|
|
78
|
-
|
79
|
-
|
80
|
-
|
67
|
+
def of(&block)
|
68
|
+
Array.new(self, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def roman_numeral
|
72
|
+
return '' if zero?
|
73
|
+
return "-#{(-self).roman_numeral}" if negative?
|
81
74
|
|
82
|
-
|
75
|
+
ROMAN_NUMERALS.each { |key, val| break "#{key}#{(self - val).roman_numeral}" if val <= self }
|
76
|
+
end
|
83
77
|
|
78
|
+
def to_time
|
79
|
+
Time.at(self)
|
84
80
|
end
|
81
|
+
|
82
|
+
alias to_t to_time unless defined?(to_t)
|
83
|
+
|
85
84
|
end
|
data/lib/lite/ruby/kernel.rb
CHANGED
@@ -1,31 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
module Kernel
|
3
|
+
module Kernel
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
def try_eval
|
14
|
-
val = /\[\d*,?\d*,?\d*\]/.match(to_s).to_s
|
15
|
-
return val if val.nil?
|
5
|
+
# rubocop:disable Lint/RescueException, Security/Eval
|
6
|
+
def safe_eval
|
7
|
+
eval(self)
|
8
|
+
rescue Exception
|
9
|
+
self
|
10
|
+
end
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
12
|
+
def try_eval
|
13
|
+
val = /\[\d*,?\d*,?\d*\]/.match(to_s).to_s
|
14
|
+
return val if val.nil?
|
20
15
|
|
21
|
-
|
16
|
+
eval(val)
|
17
|
+
end
|
18
|
+
# rubocop:enable Lint/RescueException, Security/Eval
|
22
19
|
|
23
|
-
|
24
|
-
val = caller[depth][/`([^']*)'/, 1]
|
25
|
-
return val if depth.zero? || !val.include?('<top (required)>')
|
20
|
+
private
|
26
21
|
|
27
|
-
|
28
|
-
|
22
|
+
def caller_name(depth = 0)
|
23
|
+
val = caller[depth][/`([^']*)'/, 1]
|
24
|
+
return val if depth.zero? || !val.include?('<top (required)>')
|
29
25
|
|
26
|
+
caller[depth - 1][/`([^']*)'/, 1]
|
30
27
|
end
|
28
|
+
|
31
29
|
end
|
data/lib/lite/ruby/numeric.rb
CHANGED
@@ -1,244 +1,242 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
class Numeric
|
3
|
+
class Numeric
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def add(num)
|
6
|
+
self + num
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def at_least(lower)
|
10
|
+
self >= lower ? self : lower
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def at_most(upper)
|
14
|
+
self <= upper ? self : upper
|
15
|
+
end
|
17
16
|
|
18
|
-
|
19
|
-
|
17
|
+
def close?(number, epsilon = 0.01)
|
18
|
+
return self == number if epsilon.zero?
|
20
19
|
|
21
|
-
|
22
|
-
|
20
|
+
a = to_f
|
21
|
+
b = number.to_f
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
23
|
+
if a.zero? || b.zero?
|
24
|
+
(a - b).abs < epsilon
|
25
|
+
else
|
26
|
+
(a / b - 1).abs < epsilon
|
29
27
|
end
|
28
|
+
end
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
def decrement(amount = 1.0)
|
31
|
+
self - amount
|
32
|
+
end
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
34
|
+
def delimit(options = {})
|
35
|
+
delimiter = options[:delimiter] || ','
|
36
|
+
separator = options[:separator] || '.'
|
38
37
|
|
39
|
-
|
40
|
-
|
38
|
+
digits, decimals = to_s.split('.')
|
39
|
+
digits = digits.reverse.chars.each_slice(3).map(&:join).join(delimiter).reverse
|
41
40
|
|
42
|
-
|
41
|
+
return digits unless decimals
|
43
42
|
|
44
|
-
|
45
|
-
|
43
|
+
[digits, decimals].join(separator)
|
44
|
+
end
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
def delta(num)
|
47
|
+
(self - num).abs
|
48
|
+
end
|
50
49
|
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
def distance(num)
|
51
|
+
self - num
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
54
|
+
def divide(num)
|
55
|
+
return num if num.zero?
|
57
56
|
|
58
|
-
|
59
|
-
|
57
|
+
self / num
|
58
|
+
end
|
60
59
|
|
61
|
-
|
62
|
-
|
63
|
-
|
60
|
+
def equal_to?(num)
|
61
|
+
self == num
|
62
|
+
end
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
def fraction
|
65
|
+
(self - truncate).abs
|
66
|
+
end
|
68
67
|
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
def fraction?
|
69
|
+
fraction.to_d != 0.0.to_d
|
70
|
+
end
|
72
71
|
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
def greater_than?(num)
|
73
|
+
num < self
|
74
|
+
end
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
def greater_than_or_equal_to?(num)
|
77
|
+
num <= self
|
78
|
+
end
|
80
79
|
|
81
|
-
|
82
|
-
|
83
|
-
|
80
|
+
def increment(amount = 1.0)
|
81
|
+
self + amount
|
82
|
+
end
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
|
84
|
+
def inside?(start, finish)
|
85
|
+
(start < self) && (finish > self)
|
86
|
+
end
|
88
87
|
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
def less_than?(num)
|
89
|
+
num > self
|
90
|
+
end
|
92
91
|
|
93
|
-
|
94
|
-
|
95
|
-
|
92
|
+
def less_than_or_equal_to?(num)
|
93
|
+
num >= self
|
94
|
+
end
|
96
95
|
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
def markdown_percentage(percent)
|
97
|
+
to_f * ((100.0 - percent.to_f) / 100.0)
|
98
|
+
end
|
100
99
|
|
101
|
-
|
102
|
-
|
103
|
-
|
100
|
+
def markup_percentage(percent)
|
101
|
+
to_f + (to_f * (percent.to_f / 100.0))
|
102
|
+
end
|
104
103
|
|
105
|
-
|
106
|
-
|
107
|
-
|
104
|
+
def multiply(num)
|
105
|
+
self * num
|
106
|
+
end
|
108
107
|
|
109
|
-
|
110
|
-
|
108
|
+
def multiple_of?(number)
|
109
|
+
return zero? if number.zero?
|
111
110
|
|
112
|
-
|
113
|
-
|
111
|
+
modulo(number).zero?
|
112
|
+
end
|
114
113
|
|
115
|
-
|
116
|
-
|
117
|
-
|
114
|
+
def not_equal_to?(num)
|
115
|
+
self != num
|
116
|
+
end
|
118
117
|
|
119
|
-
|
120
|
-
|
118
|
+
def ordinal
|
119
|
+
return 'th' if (11..13).cover?(abs % 100)
|
121
120
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
end
|
121
|
+
case abs % 10
|
122
|
+
when 1 then 'st'
|
123
|
+
when 2 then 'nd'
|
124
|
+
when 3 then 'rd'
|
125
|
+
else 'th'
|
128
126
|
end
|
127
|
+
end
|
129
128
|
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
def ordinalize
|
130
|
+
"#{self}#{ordinal}"
|
131
|
+
end
|
133
132
|
|
134
|
-
|
135
|
-
|
136
|
-
|
133
|
+
def outside?(start, finish)
|
134
|
+
(start > self) || (finish < self)
|
135
|
+
end
|
137
136
|
|
138
|
-
|
139
|
-
|
140
|
-
|
137
|
+
def pad(options = {})
|
138
|
+
pad_number = options[:pad_number] || 0
|
139
|
+
precision = options[:precision] || 3
|
141
140
|
|
142
|
-
|
143
|
-
|
141
|
+
to_s.rjust(precision, pad_number.to_s)
|
142
|
+
end
|
144
143
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
string << separator unless string.include?(separator)
|
153
|
-
ljust_count = string.split(separator).first.length
|
154
|
-
ljust_count += (string.count(separator) + precision) if precision.positive?
|
155
|
-
|
156
|
-
if ljust_count >= string.length
|
157
|
-
string.ljust(ljust_count, pad_number.to_s)
|
158
|
-
else
|
159
|
-
string[0..(ljust_count - 1)]
|
160
|
-
end
|
161
|
-
end
|
162
|
-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
144
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
145
|
+
def pad_precision(options = {})
|
146
|
+
pad_number = options[:pad_number] || 0
|
147
|
+
precision = options[:precision] || 2
|
148
|
+
separator = options[:separator] || '.'
|
149
|
+
string = to_s
|
163
150
|
|
164
|
-
|
165
|
-
|
151
|
+
string << separator unless string.include?(separator)
|
152
|
+
ljust_count = string.split(separator).first.length
|
153
|
+
ljust_count += (string.count(separator) + precision) if precision.positive?
|
166
154
|
|
167
|
-
|
155
|
+
if ljust_count >= string.length
|
156
|
+
string.ljust(ljust_count, pad_number.to_s)
|
157
|
+
else
|
158
|
+
string[0..(ljust_count - 1)]
|
168
159
|
end
|
160
|
+
end
|
161
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
169
162
|
|
170
|
-
|
171
|
-
|
172
|
-
end
|
163
|
+
def percentage_of(number)
|
164
|
+
return 0 if zero? || number.zero?
|
173
165
|
|
174
|
-
|
175
|
-
|
176
|
-
end
|
166
|
+
(self / number.to_f) * 100.0
|
167
|
+
end
|
177
168
|
|
178
|
-
|
179
|
-
|
180
|
-
|
169
|
+
def power(num)
|
170
|
+
self**num
|
171
|
+
end
|
181
172
|
|
182
|
-
|
183
|
-
|
173
|
+
def range(value)
|
174
|
+
(self - value)..(self + value)
|
175
|
+
end
|
184
176
|
|
185
|
-
|
186
|
-
|
177
|
+
def root(num)
|
178
|
+
self**(1.0 / num)
|
179
|
+
end
|
187
180
|
|
188
|
-
|
189
|
-
|
190
|
-
end
|
181
|
+
def round_down(num = 0)
|
182
|
+
int, dec = to_f.to_s.split('.')
|
191
183
|
|
192
|
-
|
193
|
-
|
184
|
+
"#{int}.#{dec[0...num]}".to_f
|
185
|
+
end
|
194
186
|
|
195
|
-
|
196
|
-
|
187
|
+
def subtract(num)
|
188
|
+
self - num
|
189
|
+
end
|
190
|
+
|
191
|
+
def to_currency(options = {})
|
192
|
+
unit = options[:unit] || '$'
|
197
193
|
|
198
|
-
|
199
|
-
|
194
|
+
"#{unit}#{pad_precision(options.only(:precision))}"
|
195
|
+
end
|
200
196
|
|
201
|
-
|
202
|
-
|
197
|
+
def to_nearest_value(values = [])
|
198
|
+
return self if values.length.zero?
|
203
199
|
|
204
|
-
|
205
|
-
|
200
|
+
value = values.first
|
201
|
+
difference = (self - value).abs
|
206
202
|
|
207
|
-
|
208
|
-
|
209
|
-
end
|
203
|
+
values.each do |val|
|
204
|
+
next unless (self - val).abs < difference
|
210
205
|
|
211
|
-
|
206
|
+
difference = (self - val).abs
|
207
|
+
value = val
|
212
208
|
end
|
213
209
|
|
214
|
-
|
215
|
-
|
210
|
+
value
|
211
|
+
end
|
216
212
|
|
217
|
-
|
218
|
-
|
213
|
+
def to_percentage(options = {})
|
214
|
+
unit = options[:unit] || '%'
|
219
215
|
|
220
|
-
|
221
|
-
|
216
|
+
"#{pad_precision(options.only(:precision))}#{unit}"
|
217
|
+
end
|
218
|
+
|
219
|
+
def within?(number, epsilon = 0.01)
|
220
|
+
return number == self if epsilon.zero?
|
222
221
|
|
223
|
-
|
224
|
-
|
222
|
+
alpha = to_f
|
223
|
+
beta = number.to_f
|
225
224
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
end
|
225
|
+
if alpha.zero? || beta.zero?
|
226
|
+
(alpha - beta).abs < epsilon
|
227
|
+
else
|
228
|
+
(alpha / beta - 1).abs < epsilon
|
231
229
|
end
|
230
|
+
end
|
232
231
|
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
232
|
+
alias eq? equal_to?
|
233
|
+
alias gt? greater_than?
|
234
|
+
alias gteq? greater_than_or_equal_to?
|
235
|
+
alias inequal_to? not_equal_to?
|
236
|
+
alias ineq? not_equal_to?
|
237
|
+
alias lt? less_than?
|
238
|
+
alias lteq? less_than_or_equal_to?
|
239
|
+
alias not_eq? not_equal_to?
|
240
|
+
alias plus_minus range
|
242
241
|
|
243
|
-
end
|
244
242
|
end
|