active_object 3.1.0 → 4.0.0
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/.DS_Store +0 -0
- data/.fasterer.yml +19 -0
- data/.reek +39 -0
- data/.rubocop.yml +38 -0
- data/Gemfile +1 -1
- data/README.md +224 -239
- data/Rakefile +1 -1
- data/active_object.gemspec +19 -16
- data/bin/console +4 -4
- data/bin/rake +6 -7
- data/lib/.DS_Store +0 -0
- data/lib/active_object.rb +4 -33
- data/lib/active_object/.DS_Store +0 -0
- data/lib/active_object/array.rb +55 -41
- data/lib/active_object/date.rb +17 -22
- data/lib/active_object/enumerable.rb +73 -56
- data/lib/active_object/hash.rb +47 -37
- data/lib/active_object/integer.rb +8 -9
- data/lib/active_object/numeric.rb +132 -122
- data/lib/active_object/object.rb +10 -10
- data/lib/active_object/range.rb +1 -1
- data/lib/active_object/settings.rb +17 -0
- data/lib/active_object/string.rb +93 -90
- data/lib/active_object/time.rb +37 -48
- data/lib/active_object/version.rb +1 -1
- data/lib/generators/active_object/install_generator.rb +6 -8
- data/lib/generators/active_object/templates/install.rb +1 -1
- metadata +51 -3
- data/lib/active_object/configuration.rb +0 -21
data/lib/active_object/hash.rb
CHANGED
@@ -3,47 +3,52 @@ module ActiveObject::Hash
|
|
3
3
|
def assert_valid_keys(*valid_keys)
|
4
4
|
valid_keys.flatten!
|
5
5
|
|
6
|
-
each_key do |
|
7
|
-
unless valid_keys.include?(
|
6
|
+
each_key do |key|
|
7
|
+
unless valid_keys.include?(key)
|
8
8
|
raise ArgumentError,
|
9
|
-
|
9
|
+
"Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
def compact
|
15
|
-
select { |
|
15
|
+
select { |_, val| !val.nil? }
|
16
16
|
end
|
17
17
|
|
18
18
|
def compact!
|
19
|
-
reject! { |
|
19
|
+
reject! { |_, val| val.nil? }
|
20
20
|
end
|
21
21
|
|
22
22
|
def deep_merge(other_hash, &block)
|
23
|
-
dup.deep_merge!(other_hash,
|
23
|
+
dup.deep_merge!(other_hash, yield(block))
|
24
24
|
end
|
25
25
|
|
26
|
+
# rubocop:disable Metrics/MethodLength
|
26
27
|
def deep_merge!(other_hash, &block)
|
27
28
|
other_hash.each_pair do |current_key, other_value|
|
28
29
|
this_value = self[current_key]
|
29
30
|
|
30
31
|
self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
this_value.deep_merge(other_value, yield(block))
|
33
|
+
elsif block_given? && key?(current_key)
|
34
|
+
yield(current_key, this_value, other_value)
|
35
|
+
else
|
36
|
+
other_value
|
37
|
+
end
|
35
38
|
end
|
36
39
|
|
37
40
|
self
|
38
41
|
end
|
42
|
+
# rubocop:enable Metrics/MethodLength
|
39
43
|
|
40
44
|
def dig(key, *rest)
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
45
|
+
value = (self[key] rescue nil)
|
46
|
+
return if value.nil?
|
47
|
+
|
48
|
+
if rest.empty?
|
49
|
+
value
|
50
|
+
elsif value.respond_to?(:dig)
|
51
|
+
value.dig(*rest)
|
47
52
|
end
|
48
53
|
end
|
49
54
|
|
@@ -52,7 +57,7 @@ module ActiveObject::Hash
|
|
52
57
|
end
|
53
58
|
|
54
59
|
def except!(*keys)
|
55
|
-
keys.flatten.each { |
|
60
|
+
keys.flatten.each { |key| delete(key) }
|
56
61
|
self
|
57
62
|
end
|
58
63
|
|
@@ -60,16 +65,18 @@ module ActiveObject::Hash
|
|
60
65
|
dup.hmap!(&block)
|
61
66
|
end
|
62
67
|
|
68
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
63
69
|
def hmap!(&block)
|
64
|
-
inject({}) { |hash, (
|
70
|
+
inject({}) { |hash, (key, val)| hash.merge(yield(key, val)) }
|
65
71
|
end
|
72
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
66
73
|
|
67
74
|
def nillify
|
68
75
|
dup.nillify!
|
69
76
|
end
|
70
77
|
|
71
78
|
def nillify!
|
72
|
-
each { |
|
79
|
+
each { |key, val| self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?) }
|
73
80
|
end
|
74
81
|
|
75
82
|
def only(*keys)
|
@@ -78,7 +85,7 @@ module ActiveObject::Hash
|
|
78
85
|
|
79
86
|
def only!(*keys)
|
80
87
|
hash = {}
|
81
|
-
keys.flatten.each { |
|
88
|
+
keys.flatten.each { |key| hash[key] = self[key] if key?(key) }
|
82
89
|
replace(hash)
|
83
90
|
end
|
84
91
|
|
@@ -88,7 +95,7 @@ module ActiveObject::Hash
|
|
88
95
|
|
89
96
|
def rename_keys!(*keys)
|
90
97
|
keys = Hash[*keys.flatten]
|
91
|
-
keys.each { |
|
98
|
+
keys.each { |key, val| self[val] = delete(key) if self[key] }
|
92
99
|
self
|
93
100
|
end
|
94
101
|
|
@@ -117,7 +124,7 @@ module ActiveObject::Hash
|
|
117
124
|
end
|
118
125
|
|
119
126
|
def sample_key!
|
120
|
-
key,
|
127
|
+
key, = sample
|
121
128
|
delete(key)
|
122
129
|
key
|
123
130
|
end
|
@@ -141,7 +148,9 @@ module ActiveObject::Hash
|
|
141
148
|
end
|
142
149
|
|
143
150
|
def slice(*keys)
|
144
|
-
keys
|
151
|
+
keys
|
152
|
+
.flatten
|
153
|
+
.each_with_object(self.class.new) { |key, hsh| hsh[key] = self[key] if key?(key) }
|
145
154
|
end
|
146
155
|
|
147
156
|
def slice!(*keys)
|
@@ -160,18 +169,17 @@ module ActiveObject::Hash
|
|
160
169
|
end
|
161
170
|
|
162
171
|
def stringify_keys!
|
163
|
-
|
164
|
-
options[key.to_s] =
|
165
|
-
options
|
172
|
+
each_with_object({}) do |(key, val), options|
|
173
|
+
options[key.to_s] = val
|
166
174
|
end
|
167
175
|
end
|
168
176
|
|
169
177
|
def strip
|
170
|
-
select { |
|
178
|
+
select { |_, val| !val.blank? }
|
171
179
|
end
|
172
180
|
|
173
181
|
def strip!
|
174
|
-
reject! { |
|
182
|
+
reject! { |_, val| val.blank? }
|
175
183
|
end
|
176
184
|
|
177
185
|
def symbolize_keys
|
@@ -179,9 +187,8 @@ module ActiveObject::Hash
|
|
179
187
|
end
|
180
188
|
|
181
189
|
def symbolize_keys!
|
182
|
-
|
183
|
-
options[(key.to_sym rescue key) || key] =
|
184
|
-
options
|
190
|
+
each_with_object({}) do |(key, val), options|
|
191
|
+
options[(key.to_sym rescue key) || key] = val
|
185
192
|
end
|
186
193
|
end
|
187
194
|
|
@@ -190,9 +197,8 @@ module ActiveObject::Hash
|
|
190
197
|
end
|
191
198
|
|
192
199
|
def symbolize_and_underscore_keys!
|
193
|
-
|
194
|
-
options[(key.to_s.
|
195
|
-
options
|
200
|
+
each_with_object({}) do |(key, val), options|
|
201
|
+
options[(key.to_s.tr(' ', '_').underscore.to_sym rescue key) || key] = val
|
196
202
|
end
|
197
203
|
end
|
198
204
|
|
@@ -200,23 +206,27 @@ module ActiveObject::Hash
|
|
200
206
|
dup.transform_keys!(&block)
|
201
207
|
end
|
202
208
|
|
209
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
203
210
|
def transform_keys!(&block)
|
204
211
|
return(enum_for(:transform_keys!)) unless block_given?
|
205
212
|
|
206
|
-
|
213
|
+
each_key { |key| self[yield(key)] = delete(key) }
|
207
214
|
self
|
208
215
|
end
|
216
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
209
217
|
|
210
218
|
def transform_values(&block)
|
211
219
|
dup.transform_values!(&block)
|
212
220
|
end
|
213
221
|
|
222
|
+
# rubocop:disable Lint/UnusedMethodArgument
|
214
223
|
def transform_values!(&block)
|
215
224
|
return(enum_for(:transform_values!)) unless block_given?
|
216
225
|
|
217
|
-
each { |
|
226
|
+
each { |key, val| self[key] = yield(val) }
|
218
227
|
end
|
228
|
+
# rubocop:enable Lint/UnusedMethodArgument
|
219
229
|
|
220
230
|
end
|
221
231
|
|
222
|
-
Hash.send(:include, ActiveObject::Hash) if ActiveObject.
|
232
|
+
Hash.send(:include, ActiveObject::Hash) if ActiveObject::Settings.config.autoload_hash
|
@@ -1,13 +1,12 @@
|
|
1
1
|
module ActiveObject::Integer
|
2
|
-
|
3
2
|
ROMAN_VALUES = {
|
4
|
-
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10,
|
5
|
-
|
6
|
-
}
|
3
|
+
M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4,
|
4
|
+
I: 1
|
5
|
+
}.freeze
|
7
6
|
|
8
7
|
def factorial
|
9
8
|
return(1) if zero?
|
10
|
-
2.upto(self).inject(1) { |
|
9
|
+
2.upto(self).inject(1) { |a, e| a * e }
|
11
10
|
end
|
12
11
|
|
13
12
|
def of(&block)
|
@@ -15,10 +14,10 @@ module ActiveObject::Integer
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def roman
|
18
|
-
return(
|
19
|
-
return("-#{(-self).roman}") if
|
17
|
+
return('') if zero?
|
18
|
+
return("-#{(-self).roman}") if negative?
|
20
19
|
|
21
|
-
ROMAN_VALUES.each { |
|
20
|
+
ROMAN_VALUES.each { |key, val| return("#{key}#{(self - val).roman}") if val <= self }
|
22
21
|
end
|
23
22
|
|
24
23
|
def time
|
@@ -27,4 +26,4 @@ module ActiveObject::Integer
|
|
27
26
|
|
28
27
|
end
|
29
28
|
|
30
|
-
Integer.send(:include, ActiveObject::Integer) if ActiveObject.
|
29
|
+
Integer.send(:include, ActiveObject::Integer) if ActiveObject::Settings.config.autoload_integer
|
@@ -1,29 +1,24 @@
|
|
1
1
|
module ActiveObject::Numeric
|
2
|
-
|
3
2
|
MILLI = 0.001
|
4
3
|
CENTI = MILLI * 10.0
|
5
4
|
DECI = CENTI * 10.0
|
6
5
|
DECA = 10.0
|
7
6
|
HECTO = DECA * 10.0
|
8
7
|
KILO = HECTO * 10.0
|
9
|
-
|
10
8
|
KILOBYTE = 1024.0
|
11
9
|
MEGABYTE = KILOBYTE * 1024.0
|
12
10
|
GIGABYTE = MEGABYTE * 1024.0
|
13
11
|
TERABYTE = GIGABYTE * 1024.0
|
14
12
|
PETABYTE = TERABYTE * 1024.0
|
15
13
|
EXABYTE = PETABYTE * 1024.0
|
16
|
-
|
17
14
|
FEET = 12.0
|
18
15
|
YARD = FEET * 3.0
|
19
16
|
MILE = YARD * 1760.0
|
20
17
|
NAUTICAL_MILE = MILE * 1.15078
|
21
|
-
|
22
18
|
METRIC_TON = KILO * 1000.0
|
23
19
|
POUND = 16.0
|
24
20
|
STONE = POUND * 14.0
|
25
21
|
TON = POUND * 2000.0
|
26
|
-
|
27
22
|
MINUTE = 60.0
|
28
23
|
HOUR = MINUTE * 60.0
|
29
24
|
DAY = HOUR * 24.0
|
@@ -34,37 +29,34 @@ module ActiveObject::Numeric
|
|
34
29
|
MILLENNIUM = CENTURY * 10.0
|
35
30
|
|
36
31
|
BYTE_KEYS = [
|
37
|
-
:byte, :bytes, :kilobyte, :kilobytes, :megabyte, :megabytes, :gigabyte,
|
38
|
-
:
|
39
|
-
]
|
32
|
+
:byte, :bytes, :kilobyte, :kilobytes, :megabyte, :megabytes, :gigabyte, :gigabytes, :terabyte,
|
33
|
+
:terabytes, :petabyte, :petabytes, :exabyte, :exabytes
|
34
|
+
].freeze
|
40
35
|
LENGTH_KEYS = {
|
41
36
|
metric: [
|
42
|
-
:meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters,
|
43
|
-
:
|
44
|
-
:kilometer, :kilometers
|
37
|
+
:meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
|
38
|
+
:decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
|
45
39
|
],
|
46
40
|
imperical: [
|
47
|
-
:inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles,
|
48
|
-
:nautical_mile, :nautical_miles
|
41
|
+
:inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
|
49
42
|
]
|
50
|
-
}
|
43
|
+
}.freeze
|
51
44
|
MASS_KEYS = {
|
52
45
|
metric: [
|
53
|
-
:gram, :grams, :milligram, :milligrams, :centigram, :centigrams,
|
54
|
-
:
|
55
|
-
:
|
46
|
+
:gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
|
47
|
+
:decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
|
48
|
+
:metric_tons
|
56
49
|
],
|
57
50
|
imperical: [:ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons]
|
58
|
-
}
|
59
|
-
TEMPERATURE_KEYS = [:celsius, :fahrenheit, :kelvin]
|
51
|
+
}.freeze
|
52
|
+
TEMPERATURE_KEYS = [:celsius, :fahrenheit, :kelvin].freeze
|
60
53
|
TIME_KEYS = [
|
61
|
-
:second, :seconds, :minute, :minutes, :hour, :hours, :day, :days,
|
62
|
-
:
|
63
|
-
|
64
|
-
]
|
54
|
+
:second, :seconds, :minute, :minutes, :hour, :hours, :day, :days, :week, :weeks, :year, :years,
|
55
|
+
:decade, :decades, :century, :centuries, :millennium, :millenniums
|
56
|
+
].freeze
|
65
57
|
|
66
|
-
def add(
|
67
|
-
self +
|
58
|
+
def add(num)
|
59
|
+
self + num
|
68
60
|
end
|
69
61
|
|
70
62
|
def bytes_in_bytes
|
@@ -91,13 +83,20 @@ module ActiveObject::Numeric
|
|
91
83
|
|
92
84
|
alias_method :century_in_seconds, :centuries_in_seconds
|
93
85
|
|
94
|
-
|
95
|
-
|
96
|
-
|
86
|
+
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
87
|
+
def clamp(minimum, maximum = nil)
|
88
|
+
if maximum.nil? && minimum.is_a?(Range)
|
89
|
+
min_min = minimum.min
|
90
|
+
min_max = minimum.max
|
91
|
+
|
92
|
+
return(min_min) if self < min_min
|
93
|
+
self > min_max ? min_max : self
|
97
94
|
else
|
98
|
-
|
95
|
+
return(minimum) if self < minimum
|
96
|
+
self > maximum ? maximum : self
|
99
97
|
end
|
100
98
|
end
|
99
|
+
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
101
100
|
|
102
101
|
def days_in_seconds
|
103
102
|
self * DAY
|
@@ -135,7 +134,7 @@ module ActiveObject::Numeric
|
|
135
134
|
|
136
135
|
alias_method :decimeter_in_meters, :decimeters_in_meters
|
137
136
|
|
138
|
-
def decrement(amount=1.0)
|
137
|
+
def decrement(amount = 1.0)
|
139
138
|
self + amount
|
140
139
|
end
|
141
140
|
|
@@ -145,12 +144,12 @@ module ActiveObject::Numeric
|
|
145
144
|
|
146
145
|
alias_method :degree_to_radians, :degrees_to_radians
|
147
146
|
|
148
|
-
def distance(
|
149
|
-
(self -
|
147
|
+
def distance(num)
|
148
|
+
(self - num).abs
|
150
149
|
end
|
151
150
|
|
152
|
-
def divide(
|
153
|
-
self /
|
151
|
+
def divide(num)
|
152
|
+
self / num
|
154
153
|
end
|
155
154
|
|
156
155
|
def exabytes_in_bytes
|
@@ -177,12 +176,12 @@ module ActiveObject::Numeric
|
|
177
176
|
|
178
177
|
alias_method :gram_in_grams, :grams_in_grams
|
179
178
|
|
180
|
-
def greater_than?(
|
181
|
-
self >
|
179
|
+
def greater_than?(num)
|
180
|
+
self > num
|
182
181
|
end
|
183
182
|
|
184
|
-
def greater_than_or_equal_to?(
|
185
|
-
self >=
|
183
|
+
def greater_than_or_equal_to?(num)
|
184
|
+
self >= num
|
186
185
|
end
|
187
186
|
|
188
187
|
def hectograms_in_grams
|
@@ -209,7 +208,7 @@ module ActiveObject::Numeric
|
|
209
208
|
|
210
209
|
alias_method :inch_in_inches, :inches_in_inches
|
211
210
|
|
212
|
-
def increment(amount=1.0)
|
211
|
+
def increment(amount = 1.0)
|
213
212
|
self + amount
|
214
213
|
end
|
215
214
|
|
@@ -235,12 +234,12 @@ module ActiveObject::Numeric
|
|
235
234
|
|
236
235
|
alias_method :kilogram_in_grams, :kilograms_in_grams
|
237
236
|
|
238
|
-
def less_than?(
|
239
|
-
self <
|
237
|
+
def less_than?(num)
|
238
|
+
self < num
|
240
239
|
end
|
241
240
|
|
242
|
-
def less_than_or_equal_to?(
|
243
|
-
self <=
|
241
|
+
def less_than_or_equal_to?(num)
|
242
|
+
self <= num
|
244
243
|
end
|
245
244
|
|
246
245
|
def metric_tons_in_grams
|
@@ -291,12 +290,12 @@ module ActiveObject::Numeric
|
|
291
290
|
|
292
291
|
alias_method :minute_in_seconds, :minutes_in_seconds
|
293
292
|
|
294
|
-
def multiply(
|
295
|
-
self *
|
293
|
+
def multiply(num)
|
294
|
+
self * num
|
296
295
|
end
|
297
296
|
|
298
297
|
def multiple_of?(number)
|
299
|
-
number
|
298
|
+
number.zero? ? zero? : modulo(number).zero?
|
300
299
|
end
|
301
300
|
|
302
301
|
def nautical_miles_in_inches
|
@@ -305,27 +304,27 @@ module ActiveObject::Numeric
|
|
305
304
|
|
306
305
|
alias_method :nautical_mile_in_inches, :nautical_miles_in_inches
|
307
306
|
|
307
|
+
# rubocop:disable Style/NumericPredicate
|
308
308
|
def negative?
|
309
309
|
self < 0
|
310
310
|
end
|
311
|
+
# rubocop:enable Style/NumericPredicate
|
311
312
|
|
312
313
|
def ordinal
|
313
|
-
|
314
|
-
|
315
|
-
if (11..13).cover?(abs_number % 100)
|
316
|
-
"th"
|
314
|
+
if (11..13).cover?(abs % 100)
|
315
|
+
'th'
|
317
316
|
else
|
318
|
-
case
|
319
|
-
when 1
|
320
|
-
when 2
|
321
|
-
when 3
|
322
|
-
else
|
317
|
+
case abs % 10
|
318
|
+
when 1 then 'st'
|
319
|
+
when 2 then 'nd'
|
320
|
+
when 3 then 'rd'
|
321
|
+
else 'th'
|
323
322
|
end
|
324
323
|
end
|
325
324
|
end
|
326
325
|
|
327
326
|
def ordinalize
|
328
|
-
"#{self}#{
|
327
|
+
"#{self}#{ordinal}"
|
329
328
|
end
|
330
329
|
|
331
330
|
def ounces_in_ounces
|
@@ -338,25 +337,32 @@ module ActiveObject::Numeric
|
|
338
337
|
(self < start) || (finish < self)
|
339
338
|
end
|
340
339
|
|
341
|
-
def pad(options={})
|
342
|
-
pad_number = options
|
343
|
-
precision = options
|
340
|
+
def pad(options = {})
|
341
|
+
pad_number = options[:pad_number] || 0
|
342
|
+
precision = options[:precision] || 3
|
344
343
|
|
345
344
|
to_s.rjust(precision, pad_number.to_s)
|
346
345
|
end
|
347
346
|
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
347
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
348
|
+
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
|
349
|
+
def pad_precision(options = {})
|
350
|
+
pad_number = options[:pad_number] || 0
|
351
|
+
precision = options[:precision] || 2
|
352
|
+
separator = options[:separator] || '.'
|
352
353
|
string = to_s
|
353
354
|
|
354
355
|
string << separator unless string.include?(separator)
|
355
|
-
ljust_count =
|
356
|
-
ljust_count += (string.count(separator) + precision) if precision
|
357
|
-
|
358
|
-
|
356
|
+
ljust_count = string.split(separator).first.length
|
357
|
+
ljust_count += (string.count(separator) + precision) if precision.positive?
|
358
|
+
if ljust_count >= string.length
|
359
|
+
string.ljust(ljust_count, pad_number.to_s)
|
360
|
+
else
|
361
|
+
string[0..(ljust_count - 1)]
|
362
|
+
end
|
359
363
|
end
|
364
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
365
|
+
# rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
|
360
366
|
|
361
367
|
def petabytes_in_bytes
|
362
368
|
self * PETABYTE
|
@@ -364,9 +370,11 @@ module ActiveObject::Numeric
|
|
364
370
|
|
365
371
|
alias_method :petabyte_in_bytes, :petabytes_in_bytes
|
366
372
|
|
373
|
+
# rubocop:disable Style/NumericPredicate
|
367
374
|
def positive?
|
368
375
|
self > 0
|
369
376
|
end
|
377
|
+
# rubocop:enable Style/NumericPredicate
|
370
378
|
|
371
379
|
def pounds_in_ounces
|
372
380
|
self * POUND
|
@@ -374,12 +382,12 @@ module ActiveObject::Numeric
|
|
374
382
|
|
375
383
|
alias_method :pound_in_ounces, :pounds_in_ounces
|
376
384
|
|
377
|
-
def power(
|
378
|
-
self
|
385
|
+
def power(num)
|
386
|
+
self**num
|
379
387
|
end
|
380
388
|
|
381
|
-
def root(
|
382
|
-
self
|
389
|
+
def root(num)
|
390
|
+
self**(1.0 / num)
|
383
391
|
end
|
384
392
|
|
385
393
|
def seconds_in_seconds
|
@@ -394,8 +402,8 @@ module ActiveObject::Numeric
|
|
394
402
|
|
395
403
|
alias_method :stone_in_ounces, :stones_in_ounces
|
396
404
|
|
397
|
-
def subtract(
|
398
|
-
self -
|
405
|
+
def subtract(num)
|
406
|
+
self - num
|
399
407
|
end
|
400
408
|
|
401
409
|
def terabytes_in_bytes
|
@@ -405,105 +413,96 @@ module ActiveObject::Numeric
|
|
405
413
|
alias_method :terabyte_in_bytes, :terabytes_in_bytes
|
406
414
|
|
407
415
|
def to_byte(from, to)
|
408
|
-
|
409
|
-
raise ArgumentError,
|
410
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{BYTE_KEYS.map(&:inspect).join(', ')}"
|
411
|
-
end
|
416
|
+
assert_valid_keys!(BYTE_KEYS, from, to)
|
412
417
|
|
413
418
|
to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f
|
414
419
|
end
|
415
420
|
|
416
|
-
def to_currency(options={})
|
417
|
-
unit = options
|
421
|
+
def to_currency(options = {})
|
422
|
+
unit = options[:unit] || '$'
|
418
423
|
|
419
424
|
"#{unit}#{pad_precision(options.only(:precision))}"
|
420
425
|
end
|
421
426
|
|
427
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
422
428
|
def to_length(from, to)
|
429
|
+
assert_valid_keys!(LENGTH_KEYS.values.flatten, from, to)
|
423
430
|
metric_keys = LENGTH_KEYS.fetch(:metric)
|
424
|
-
|
425
|
-
|
426
|
-
unless valid_keys.include?(from) && valid_keys.include?(to)
|
427
|
-
raise ArgumentError,
|
428
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
429
|
-
end
|
431
|
+
return(self) if from == to
|
432
|
+
metrics_included_from = metric_keys.include?(from)
|
430
433
|
|
431
434
|
case to
|
432
|
-
when
|
433
|
-
|
434
|
-
|
435
|
-
if metric_keys.include?(from)
|
435
|
+
when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
|
436
|
+
:decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
|
437
|
+
if metrics_included_from
|
436
438
|
to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f
|
437
439
|
else
|
438
440
|
to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f)
|
439
441
|
end
|
440
442
|
when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
|
441
|
-
if
|
443
|
+
if metrics_included_from
|
442
444
|
to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f)
|
443
445
|
else
|
444
446
|
to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f
|
445
447
|
end
|
446
448
|
end
|
447
449
|
end
|
450
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
448
451
|
|
452
|
+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
449
453
|
def to_mass(from, to)
|
454
|
+
assert_valid_keys!(MASS_KEYS.values.flatten, from, to)
|
450
455
|
metric_keys = MASS_KEYS.fetch(:metric)
|
451
|
-
|
452
|
-
|
453
|
-
unless valid_keys.include?(from) && valid_keys.include?(to)
|
454
|
-
raise ArgumentError,
|
455
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
|
456
|
-
end
|
456
|
+
return(self) if from == to
|
457
|
+
metrics_included_from = metric_keys.include?(from)
|
457
458
|
|
458
459
|
case to
|
459
|
-
when
|
460
|
-
|
461
|
-
|
462
|
-
if
|
460
|
+
when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
|
461
|
+
:decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
|
462
|
+
:metric_tons
|
463
|
+
if metrics_included_from
|
463
464
|
to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f
|
464
465
|
else
|
465
466
|
to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f)
|
466
467
|
end
|
467
468
|
when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons
|
468
|
-
if
|
469
|
+
if metrics_included_from
|
469
470
|
to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f)
|
470
471
|
else
|
471
472
|
to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f
|
472
473
|
end
|
473
474
|
end
|
474
475
|
end
|
476
|
+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
|
475
477
|
|
476
|
-
def to_nearest_value(values=[])
|
478
|
+
def to_nearest_value(values = [])
|
477
479
|
return(self) if values.length.zero?
|
478
480
|
|
479
481
|
value = values.first
|
480
482
|
difference = (self - value).abs
|
481
483
|
|
482
|
-
values.each do |
|
483
|
-
if (self -
|
484
|
-
difference = (self -
|
485
|
-
value =
|
484
|
+
values.each do |val|
|
485
|
+
if (self - val).abs < difference
|
486
|
+
difference = (self - val).abs
|
487
|
+
value = val
|
486
488
|
end
|
487
489
|
end
|
488
490
|
|
489
491
|
value
|
490
492
|
end
|
491
493
|
|
492
|
-
def to_percentage(options={})
|
493
|
-
unit = options
|
494
|
+
def to_percentage(options = {})
|
495
|
+
unit = options[:unit] || '%'
|
494
496
|
|
495
497
|
"#{pad_precision(options.only(:precision))}#{unit}"
|
496
498
|
end
|
497
499
|
|
500
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
498
501
|
def to_temperature(from, to)
|
499
|
-
|
500
|
-
|
501
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{TEMPERATURE_KEYS.map(&:inspect).join(', ')}"
|
502
|
-
end
|
502
|
+
assert_valid_keys!(TEMPERATURE_KEYS, from, to)
|
503
|
+
return(self) if from == to
|
503
504
|
|
504
505
|
case to
|
505
|
-
when from
|
506
|
-
self
|
507
506
|
when :celsius
|
508
507
|
from == :kelvin ? (self - 273.15) : ((self - 32.0) * 5.0 / 9.0)
|
509
508
|
when :fahrenheit
|
@@ -512,12 +511,10 @@ module ActiveObject::Numeric
|
|
512
511
|
from == :celsius ? (self + 273.15) : (((self - 32.0) * 5.0 / 9.0) + 273.15)
|
513
512
|
end
|
514
513
|
end
|
514
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
515
515
|
|
516
516
|
def to_time(from, to)
|
517
|
-
|
518
|
-
raise ArgumentError,
|
519
|
-
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{TIME_KEYS.map(&:inspect).join(', ')}"
|
520
|
-
end
|
517
|
+
assert_valid_keys!(TIME_KEYS, from, to)
|
521
518
|
|
522
519
|
(to_f * 1.send("#{from}_in_seconds").to_f) / 1.send("#{to}_in_seconds").to_f
|
523
520
|
end
|
@@ -534,12 +531,13 @@ module ActiveObject::Numeric
|
|
534
531
|
|
535
532
|
alias_method :week_in_seconds, :weeks_in_seconds
|
536
533
|
|
537
|
-
def within?(number, epsilon=0.01)
|
534
|
+
def within?(number, epsilon = 0.01)
|
538
535
|
return(self == number) if epsilon.zero?
|
539
536
|
|
540
|
-
|
537
|
+
alpha = to_f
|
538
|
+
beta = number.to_f
|
541
539
|
|
542
|
-
|
540
|
+
alpha.zero? || beta.zero? ? (alpha - beta).abs < epsilon : (alpha / beta - 1).abs < epsilon
|
543
541
|
end
|
544
542
|
|
545
543
|
def yards_in_inches
|
@@ -554,6 +552,18 @@ module ActiveObject::Numeric
|
|
554
552
|
|
555
553
|
alias_method :year_in_seconds, :years_in_seconds
|
556
554
|
|
555
|
+
private
|
556
|
+
|
557
|
+
def assert_valid_keys!(cns, from, to)
|
558
|
+
unless cns.include?(from) && cns.include?(to)
|
559
|
+
raise ArgumentError,
|
560
|
+
[
|
561
|
+
"Unknown key(s): from: #{from.inspect} and to: #{to.inspect}.",
|
562
|
+
"Valid keys are: #{cns.map(&:inspect).join(', ')}"
|
563
|
+
].join(' ')
|
564
|
+
end
|
565
|
+
end
|
566
|
+
|
557
567
|
end
|
558
568
|
|
559
|
-
Numeric.send(:include, ActiveObject::Numeric) if ActiveObject.
|
569
|
+
Numeric.send(:include, ActiveObject::Numeric) if ActiveObject::Settings.config.autoload_numeric
|