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