active_object 4.0.14 → 5.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.
@@ -1,579 +1,583 @@
1
- module ActiveObject::Numeric
2
- MILLI ||= 0.001
3
- CENTI ||= MILLI * 10.0
4
- DECI ||= CENTI * 10.0
5
- DECA ||= 10.0
6
- HECTO ||= DECA * 10.0
7
- KILO ||= HECTO * 10.0
8
- KILOBYTE ||= 1024.0
9
- MEGABYTE ||= KILOBYTE * 1024.0
10
- GIGABYTE ||= MEGABYTE * 1024.0
11
- TERABYTE ||= GIGABYTE * 1024.0
12
- PETABYTE ||= TERABYTE * 1024.0
13
- EXABYTE ||= PETABYTE * 1024.0
14
- FEET ||= 12.0
15
- YARD ||= FEET * 3.0
16
- MILE ||= YARD * 1760.0
17
- NAUTICAL_MILE ||= MILE * 1.15078
18
- METRIC_TON ||= KILO * 1000.0
19
- POUND ||= 16.0
20
- STONE ||= POUND * 14.0
21
- TON ||= POUND * 2000.0
22
- MINUTE ||= 60.0
23
- HOUR ||= MINUTE * 60.0
24
- DAY ||= HOUR * 24.0
25
- WEEK ||= DAY * 7.0
26
- YEAR ||= DAY * 365.25
27
- DECADE ||= YEAR * 10.0
28
- CENTURY ||= DECADE * 10.0
29
- MILLENNIUM ||= CENTURY * 10.0
30
-
31
- BYTE_KEYS ||= %i[
32
- byte bytes kilobyte kilobytes megabyte megabytes gigabyte gigabytes terabyte terabytes petabyte
33
- petabytes exabyte exabytes
34
- ].freeze
35
- LENGTH_KEYS ||= {
36
- metric: %i[
37
- meter meters millimeter millimeters centimeter centimeters decimeter decimeters decameter
38
- decameters hectometer hectometers kilometer kilometers
39
- ],
40
- imperical: %i[inch inches foot feet yard yards mile miles nautical_mile nautical_miles]
41
- }.freeze
42
- MASS_KEYS ||= {
43
- metric: %i[
44
- gram grams milligram milligrams centigram centigrams decigram decigrams decagram decagrams
45
- hectogram hectograms kilogram kilograms metric_ton metric_tons
46
- ],
47
- imperical: %i[ounce ounces pound pounds stone stones ton tons]
48
- }.freeze
49
- TEMPERATURE_KEYS ||= %i[celsius fahrenheit kelvin].freeze
50
- TIME_KEYS ||= %i[
51
- second seconds minute minutes hour hours day days week weeks year years decade decades century
52
- centuries millennium millenniums
53
- ].freeze
54
-
55
- def add(num)
56
- self + num
57
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveObject
4
+ module Numeric
5
+ MILLI ||= 0.001
6
+ CENTI ||= MILLI * 10.0
7
+ DECI ||= CENTI * 10.0
8
+ DECA ||= 10.0
9
+ HECTO ||= DECA * 10.0
10
+ KILO ||= HECTO * 10.0
11
+ KILOBYTE ||= 1024.0
12
+ MEGABYTE ||= KILOBYTE * 1024.0
13
+ GIGABYTE ||= MEGABYTE * 1024.0
14
+ TERABYTE ||= GIGABYTE * 1024.0
15
+ PETABYTE ||= TERABYTE * 1024.0
16
+ EXABYTE ||= PETABYTE * 1024.0
17
+ FEET ||= 12.0
18
+ YARD ||= FEET * 3.0
19
+ MILE ||= YARD * 1760.0
20
+ NAUTICAL_MILE ||= MILE * 1.15078
21
+ METRIC_TON ||= KILO * 1000.0
22
+ POUND ||= 16.0
23
+ STONE ||= POUND * 14.0
24
+ TON ||= POUND * 2000.0
25
+ MINUTE ||= 60.0
26
+ HOUR ||= MINUTE * 60.0
27
+ DAY ||= HOUR * 24.0
28
+ WEEK ||= DAY * 7.0
29
+ YEAR ||= DAY * 365.25
30
+ DECADE ||= YEAR * 10.0
31
+ CENTURY ||= DECADE * 10.0
32
+ MILLENNIUM ||= CENTURY * 10.0
33
+
34
+ BYTE_KEYS ||= %i[
35
+ byte bytes kilobyte kilobytes megabyte megabytes gigabyte gigabytes terabyte terabytes petabyte
36
+ petabytes exabyte exabytes
37
+ ].freeze
38
+ LENGTH_KEYS ||= {
39
+ metric: %i[
40
+ meter meters millimeter millimeters centimeter centimeters decimeter decimeters decameter
41
+ decameters hectometer hectometers kilometer kilometers
42
+ ],
43
+ imperical: %i[inch inches foot feet yard yards mile miles nautical_mile nautical_miles]
44
+ }.freeze
45
+ MASS_KEYS ||= {
46
+ metric: %i[
47
+ gram grams milligram milligrams centigram centigrams decigram decigrams decagram decagrams
48
+ hectogram hectograms kilogram kilograms metric_ton metric_tons
49
+ ],
50
+ imperical: %i[ounce ounces pound pounds stone stones ton tons]
51
+ }.freeze
52
+ TEMPERATURE_KEYS ||= %i[celsius fahrenheit kelvin].freeze
53
+ TIME_KEYS ||= %i[
54
+ second seconds minute minutes hour hours day days week weeks year years decade decades century
55
+ centuries millennium millenniums
56
+ ].freeze
57
+
58
+ def add(num)
59
+ self + num
60
+ end
58
61
 
59
- def bytes_in_bytes
60
- self
61
- end
62
+ def bytes_in_bytes
63
+ self
64
+ end
62
65
 
63
- alias_method :byte_in_bytes, :bytes_in_bytes
66
+ alias_method :byte_in_bytes, :bytes_in_bytes
64
67
 
65
- def centigrams_in_grams
66
- self * CENTI
67
- end
68
+ def centigrams_in_grams
69
+ self * CENTI
70
+ end
68
71
 
69
- alias_method :centigram_in_grams, :centigrams_in_grams
72
+ alias_method :centigram_in_grams, :centigrams_in_grams
70
73
 
71
- def centimeters_in_meters
72
- self * CENTI
73
- end
74
+ def centimeters_in_meters
75
+ self * CENTI
76
+ end
74
77
 
75
- alias_method :centimeter_in_meters, :centimeters_in_meters
78
+ alias_method :centimeter_in_meters, :centimeters_in_meters
76
79
 
77
- def centuries_in_seconds
78
- self * CENTURY
79
- end
80
+ def centuries_in_seconds
81
+ self * CENTURY
82
+ end
80
83
 
81
- alias_method :century_in_seconds, :centuries_in_seconds
84
+ alias_method :century_in_seconds, :centuries_in_seconds
82
85
 
83
- # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
84
- def clamp(minimum, maximum = nil)
85
- if maximum.nil? && minimum.is_a?(Range)
86
- min_min = minimum.min
87
- min_max = minimum.max
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
88
91
 
89
- return min_min if min_min > self
92
+ return min_min if min_min > self
90
93
 
91
- min_max < self ? min_max : self
92
- else
93
- return minimum if minimum > self
94
+ min_max < self ? min_max : self
95
+ else
96
+ return minimum if minimum > self
94
97
 
95
- maximum < self ? maximum : self
98
+ maximum < self ? maximum : self
99
+ end
96
100
  end
97
- end
98
- # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
101
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
99
102
 
100
- def days_in_seconds
101
- self * DAY
102
- end
103
+ def days_in_seconds
104
+ self * DAY
105
+ end
103
106
 
104
- alias_method :day_in_seconds, :days_in_seconds
107
+ alias_method :day_in_seconds, :days_in_seconds
105
108
 
106
- def decades_in_seconds
107
- self * DECADE
108
- end
109
+ def decades_in_seconds
110
+ self * DECADE
111
+ end
109
112
 
110
- alias_method :decade_in_seconds, :decades_in_seconds
113
+ alias_method :decade_in_seconds, :decades_in_seconds
111
114
 
112
- def decagrams_in_grams
113
- self * DECA
114
- end
115
+ def decagrams_in_grams
116
+ self * DECA
117
+ end
115
118
 
116
- alias_method :decagram_in_grams, :decagrams_in_grams
119
+ alias_method :decagram_in_grams, :decagrams_in_grams
117
120
 
118
- def decameters_in_meters
119
- self * DECA
120
- end
121
+ def decameters_in_meters
122
+ self * DECA
123
+ end
121
124
 
122
- alias_method :decameter_in_meters, :decameters_in_meters
125
+ alias_method :decameter_in_meters, :decameters_in_meters
123
126
 
124
- def decigrams_in_grams
125
- self * DECI
126
- end
127
+ def decigrams_in_grams
128
+ self * DECI
129
+ end
127
130
 
128
- alias_method :decigram_in_grams, :decigrams_in_grams
131
+ alias_method :decigram_in_grams, :decigrams_in_grams
129
132
 
130
- def decimeters_in_meters
131
- self * DECI
132
- end
133
+ def decimeters_in_meters
134
+ self * DECI
135
+ end
133
136
 
134
- alias_method :decimeter_in_meters, :decimeters_in_meters
137
+ alias_method :decimeter_in_meters, :decimeters_in_meters
135
138
 
136
- def decrement(amount = 1.0)
137
- self + amount
138
- end
139
+ def decrement(amount = 1.0)
140
+ self + amount
141
+ end
139
142
 
140
- def degrees_to_radians
141
- self * Math::PI / 180.0
142
- end
143
+ def degrees_to_radians
144
+ self * ::Math::PI / 180.0
145
+ end
143
146
 
144
- alias_method :degree_to_radians, :degrees_to_radians
147
+ alias_method :degree_to_radians, :degrees_to_radians
145
148
 
146
- def distance(num)
147
- (self - num).abs
148
- end
149
+ def distance(num)
150
+ (self - num).abs
151
+ end
149
152
 
150
- def divide(num)
151
- self / num
152
- end
153
+ def divide(num)
154
+ self / num
155
+ end
153
156
 
154
- def exabytes_in_bytes
155
- self * EXABYTE
156
- end
157
+ def exabytes_in_bytes
158
+ self * EXABYTE
159
+ end
157
160
 
158
- alias_method :exabyte_in_bytes, :exabytes_in_bytes
161
+ alias_method :exabyte_in_bytes, :exabytes_in_bytes
159
162
 
160
- def feet_in_inches
161
- self * FEET
162
- end
163
+ def feet_in_inches
164
+ self * FEET
165
+ end
163
166
 
164
- alias_method :foot_in_inches, :feet_in_inches
167
+ alias_method :foot_in_inches, :feet_in_inches
165
168
 
166
- def fraction
167
- (self - truncate).abs
168
- end
169
+ def fraction
170
+ (self - truncate).abs
171
+ end
169
172
 
170
- def fraction?
171
- fraction != 0.0
172
- end
173
+ def fraction?
174
+ fraction != 0.0
175
+ end
173
176
 
174
- def gigabytes_in_bytes
175
- self * GIGABYTE
176
- end
177
+ def gigabytes_in_bytes
178
+ self * GIGABYTE
179
+ end
177
180
 
178
- alias_method :gigabyte_in_bytes, :gigabytes_in_bytes
181
+ alias_method :gigabyte_in_bytes, :gigabytes_in_bytes
179
182
 
180
- def grams_in_grams
181
- self
182
- end
183
+ def grams_in_grams
184
+ self
185
+ end
183
186
 
184
- alias_method :gram_in_grams, :grams_in_grams
187
+ alias_method :gram_in_grams, :grams_in_grams
185
188
 
186
- def greater_than?(num)
187
- num < self
188
- end
189
+ def greater_than?(num)
190
+ num < self
191
+ end
189
192
 
190
- def greater_than_or_equal_to?(num)
191
- num <= self
192
- end
193
+ def greater_than_or_equal_to?(num)
194
+ num <= self
195
+ end
193
196
 
194
- def hectograms_in_grams
195
- self * HECTO
196
- end
197
+ def hectograms_in_grams
198
+ self * HECTO
199
+ end
197
200
 
198
- alias_method :hectogram_in_grams, :hectograms_in_grams
201
+ alias_method :hectogram_in_grams, :hectograms_in_grams
199
202
 
200
- def hectometers_in_meters
201
- self * HECTO
202
- end
203
+ def hectometers_in_meters
204
+ self * HECTO
205
+ end
203
206
 
204
- alias_method :hectometer_in_meters, :hectometers_in_meters
207
+ alias_method :hectometer_in_meters, :hectometers_in_meters
205
208
 
206
- def hours_in_seconds
207
- self * HOUR
208
- end
209
+ def hours_in_seconds
210
+ self * HOUR
211
+ end
209
212
 
210
- alias_method :hour_in_seconds, :hours_in_seconds
213
+ alias_method :hour_in_seconds, :hours_in_seconds
211
214
 
212
- def inches_in_inches
213
- self
214
- end
215
+ def inches_in_inches
216
+ self
217
+ end
215
218
 
216
- alias_method :inch_in_inches, :inches_in_inches
219
+ alias_method :inch_in_inches, :inches_in_inches
217
220
 
218
- def increment(amount = 1.0)
219
- self + amount
220
- end
221
+ def increment(amount = 1.0)
222
+ self + amount
223
+ end
221
224
 
222
- def inside?(start, finish)
223
- (start < self) && (finish > self)
224
- end
225
+ def inside?(start, finish)
226
+ (start < self) && (finish > self)
227
+ end
225
228
 
226
- def kilobytes_in_bytes
227
- self * KILOBYTE
228
- end
229
+ def kilobytes_in_bytes
230
+ self * KILOBYTE
231
+ end
229
232
 
230
- alias_method :kilobyte_in_bytes, :kilobytes_in_bytes
233
+ alias_method :kilobyte_in_bytes, :kilobytes_in_bytes
231
234
 
232
- def kilometers_in_meters
233
- self * KILO
234
- end
235
+ def kilometers_in_meters
236
+ self * KILO
237
+ end
235
238
 
236
- alias_method :kilometer_in_meters, :kilometers_in_meters
239
+ alias_method :kilometer_in_meters, :kilometers_in_meters
237
240
 
238
- def kilograms_in_grams
239
- self * KILO
240
- end
241
+ def kilograms_in_grams
242
+ self * KILO
243
+ end
241
244
 
242
- alias_method :kilogram_in_grams, :kilograms_in_grams
245
+ alias_method :kilogram_in_grams, :kilograms_in_grams
243
246
 
244
- def less_than?(num)
245
- num > self
246
- end
247
+ def less_than?(num)
248
+ num > self
249
+ end
247
250
 
248
- def less_than_or_equal_to?(num)
249
- num >= self
250
- end
251
+ def less_than_or_equal_to?(num)
252
+ num >= self
253
+ end
251
254
 
252
- def metric_tons_in_grams
253
- self * METRIC_TON
254
- end
255
+ def metric_tons_in_grams
256
+ self * METRIC_TON
257
+ end
255
258
 
256
- alias_method :metric_ton_in_grams, :metric_tons_in_grams
259
+ alias_method :metric_ton_in_grams, :metric_tons_in_grams
257
260
 
258
- def megabytes_in_bytes
259
- self * MEGABYTE
260
- end
261
+ def megabytes_in_bytes
262
+ self * MEGABYTE
263
+ end
261
264
 
262
- alias_method :megabyte_in_bytes, :megabytes_in_bytes
265
+ alias_method :megabyte_in_bytes, :megabytes_in_bytes
263
266
 
264
- def meters_in_meters
265
- self
266
- end
267
+ def meters_in_meters
268
+ self
269
+ end
267
270
 
268
- alias_method :meter_in_meters, :meters_in_meters
271
+ alias_method :meter_in_meters, :meters_in_meters
269
272
 
270
- def miles_in_inches
271
- self * MILE
272
- end
273
+ def miles_in_inches
274
+ self * MILE
275
+ end
273
276
 
274
- alias_method :mile_in_inches, :miles_in_inches
277
+ alias_method :mile_in_inches, :miles_in_inches
275
278
 
276
- def millenniums_in_seconds
277
- self * MILLENNIUM
278
- end
279
+ def millenniums_in_seconds
280
+ self * MILLENNIUM
281
+ end
279
282
 
280
- alias_method :millennium_in_seconds, :millenniums_in_seconds
283
+ alias_method :millennium_in_seconds, :millenniums_in_seconds
281
284
 
282
- def milligrams_in_grams
283
- self * MILLI
284
- end
285
+ def milligrams_in_grams
286
+ self * MILLI
287
+ end
285
288
 
286
- alias_method :milligram_in_grams, :milligrams_in_grams
289
+ alias_method :milligram_in_grams, :milligrams_in_grams
287
290
 
288
- def millimeters_in_meters
289
- self * MILLI
290
- end
291
+ def millimeters_in_meters
292
+ self * MILLI
293
+ end
291
294
 
292
- alias_method :millimeter_in_meters, :millimeters_in_meters
295
+ alias_method :millimeter_in_meters, :millimeters_in_meters
293
296
 
294
- def minutes_in_seconds
295
- self * MINUTE
296
- end
297
+ def minutes_in_seconds
298
+ self * MINUTE
299
+ end
297
300
 
298
- alias_method :minute_in_seconds, :minutes_in_seconds
301
+ alias_method :minute_in_seconds, :minutes_in_seconds
299
302
 
300
- def multiply(num)
301
- self * num
302
- end
303
+ def multiply(num)
304
+ self * num
305
+ end
303
306
 
304
- def multiple_of?(number)
305
- number.zero? ? zero? : modulo(number).zero?
306
- end
307
+ def multiple_of?(number)
308
+ number.zero? ? zero? : modulo(number).zero?
309
+ end
307
310
 
308
- def nautical_miles_in_inches
309
- self * NAUTICAL_MILE
310
- end
311
+ def nautical_miles_in_inches
312
+ self * NAUTICAL_MILE
313
+ end
311
314
 
312
- alias_method :nautical_mile_in_inches, :nautical_miles_in_inches
315
+ alias_method :nautical_mile_in_inches, :nautical_miles_in_inches
313
316
 
314
- # rubocop:disable Style/NumericPredicate, Style/YodaCondition
315
- def negative?
316
- 0 > self
317
- end
318
- # rubocop:enable Style/NumericPredicate, Style/YodaCondition
319
-
320
- def ordinal
321
- if (11..13).cover?(abs % 100)
322
- 'th'
323
- else
324
- case abs % 10
325
- when 1 then 'st'
326
- when 2 then 'nd'
327
- when 3 then 'rd'
328
- else 'th'
317
+ # rubocop:disable Style/NumericPredicate, Style/YodaCondition
318
+ def negative?
319
+ 0 > self
320
+ end
321
+ # rubocop:enable Style/NumericPredicate, Style/YodaCondition
322
+
323
+ def ordinal
324
+ if (11..13).cover?(abs % 100)
325
+ 'th'
326
+ else
327
+ case abs % 10
328
+ when 1 then 'st'
329
+ when 2 then 'nd'
330
+ when 3 then 'rd'
331
+ else 'th'
332
+ end
329
333
  end
330
334
  end
331
- end
332
335
 
333
- def ordinalize
334
- "#{self}#{ordinal}"
335
- end
336
+ def ordinalize
337
+ "#{self}#{ordinal}"
338
+ end
336
339
 
337
- def ounces_in_ounces
338
- self
339
- end
340
+ def ounces_in_ounces
341
+ self
342
+ end
340
343
 
341
- alias_method :ounce_in_ounces, :ounces_in_ounces
344
+ alias_method :ounce_in_ounces, :ounces_in_ounces
342
345
 
343
- def outside?(start, finish)
344
- (start > self) || (finish < self)
345
- end
346
+ def outside?(start, finish)
347
+ (start > self) || (finish < self)
348
+ end
346
349
 
347
- def pad(options = {})
348
- pad_number = options[:pad_number] || 0
349
- precision = options[:precision] || 3
350
+ def pad(options = {})
351
+ pad_number = options[:pad_number] || 0
352
+ precision = options[:precision] || 3
350
353
 
351
- to_s.rjust(precision, pad_number.to_s)
352
- end
354
+ to_s.rjust(precision, pad_number.to_s)
355
+ end
353
356
 
354
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
355
- # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
356
- def pad_precision(options = {})
357
- pad_number = options[:pad_number] || 0
358
- precision = options[:precision] || 2
359
- separator = options[:separator] || '.'
360
- string = to_s
357
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
358
+ # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
359
+ def pad_precision(options = {})
360
+ pad_number = options[:pad_number] || 0
361
+ precision = options[:precision] || 2
362
+ separator = options[:separator] || '.'
363
+ string = to_s
361
364
 
362
- string << separator unless string.include?(separator)
363
- ljust_count = string.split(separator).first.length
364
- ljust_count += (string.count(separator) + precision) if precision.positive?
365
+ string << separator unless string.include?(separator)
366
+ ljust_count = string.split(separator).first.length
367
+ ljust_count += (string.count(separator) + precision) if precision.positive?
365
368
 
366
- if ljust_count >= string.length
367
- string.ljust(ljust_count, pad_number.to_s)
368
- else
369
- string[0..(ljust_count - 1)]
369
+ if ljust_count >= string.length
370
+ string.ljust(ljust_count, pad_number.to_s)
371
+ else
372
+ string[0..(ljust_count - 1)]
373
+ end
370
374
  end
371
- end
372
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
373
- # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
375
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
376
+ # rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity
374
377
 
375
- def percentage_of(number)
376
- return 0 if zero? || number.zero?
377
- (to_f / number.to_f) * 100.0
378
- end
378
+ def percentage_of(number)
379
+ return 0 if zero? || number.zero?
380
+ (to_f / number.to_f) * 100.0
381
+ end
379
382
 
380
- def petabytes_in_bytes
381
- self * PETABYTE
382
- end
383
+ def petabytes_in_bytes
384
+ self * PETABYTE
385
+ end
383
386
 
384
- alias_method :petabyte_in_bytes, :petabytes_in_bytes
387
+ alias_method :petabyte_in_bytes, :petabytes_in_bytes
385
388
 
386
- # rubocop:disable Style/NumericPredicate, Style/YodaCondition
387
- def positive?
388
- 0 < self
389
- end
390
- # rubocop:enable Style/NumericPredicate, Style/YodaCondition
389
+ # rubocop:disable Style/NumericPredicate, Style/YodaCondition
390
+ def positive?
391
+ 0 < self
392
+ end
393
+ # rubocop:enable Style/NumericPredicate, Style/YodaCondition
391
394
 
392
- def pounds_in_ounces
393
- self * POUND
394
- end
395
+ def pounds_in_ounces
396
+ self * POUND
397
+ end
395
398
 
396
- alias_method :pound_in_ounces, :pounds_in_ounces
399
+ alias_method :pound_in_ounces, :pounds_in_ounces
397
400
 
398
- def power(num)
399
- self**num
400
- end
401
+ def power(num)
402
+ self**num
403
+ end
401
404
 
402
- def root(num)
403
- self**(1.0 / num)
404
- end
405
+ def root(num)
406
+ self**(1.0 / num)
407
+ end
405
408
 
406
- def seconds_in_seconds
407
- self
408
- end
409
+ def seconds_in_seconds
410
+ self
411
+ end
409
412
 
410
- alias_method :second_in_seconds, :seconds_in_seconds
413
+ alias_method :second_in_seconds, :seconds_in_seconds
411
414
 
412
- def stones_in_ounces
413
- self * STONE
414
- end
415
+ def stones_in_ounces
416
+ self * STONE
417
+ end
415
418
 
416
- alias_method :stone_in_ounces, :stones_in_ounces
419
+ alias_method :stone_in_ounces, :stones_in_ounces
417
420
 
418
- def subtract(num)
419
- self - num
420
- end
421
+ def subtract(num)
422
+ self - num
423
+ end
421
424
 
422
- def terabytes_in_bytes
423
- self * TERABYTE
424
- end
425
+ def terabytes_in_bytes
426
+ self * TERABYTE
427
+ end
425
428
 
426
- alias_method :terabyte_in_bytes, :terabytes_in_bytes
429
+ alias_method :terabyte_in_bytes, :terabytes_in_bytes
427
430
 
428
- def to_byte(from, to)
429
- assert_inclusion_of_valid_keys!(BYTE_KEYS, from, to)
430
- to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f
431
- end
431
+ def to_byte(from, to)
432
+ assert_inclusion_of_valid_keys!(BYTE_KEYS, from, to)
433
+ to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f
434
+ end
432
435
 
433
- def to_currency(options = {})
434
- unit = options[:unit] || '$'
436
+ def to_currency(options = {})
437
+ unit = options[:unit] || '$'
435
438
 
436
- "#{unit}#{pad_precision(options.only(:precision))}"
437
- end
439
+ "#{unit}#{pad_precision(options.only(:precision))}"
440
+ end
438
441
 
439
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
440
- def to_length(from, to)
441
- assert_inclusion_of_valid_keys!(LENGTH_KEYS.values.flatten, from, to)
442
- metric_keys = LENGTH_KEYS.fetch(:metric)
443
- return self if from == to
444
- metrics_included_from = metric_keys.include?(from)
445
-
446
- case to
447
- when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
448
- :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
449
- if metrics_included_from
450
- to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f
451
- else
452
- to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f)
453
- end
454
- when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
455
- if metrics_included_from
456
- to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f)
457
- else
458
- to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f
442
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
443
+ def to_length(from, to)
444
+ assert_inclusion_of_valid_keys!(LENGTH_KEYS.values.flatten, from, to)
445
+ metric_keys = LENGTH_KEYS.fetch(:metric)
446
+ return self if from == to
447
+ metrics_included_from = metric_keys.include?(from)
448
+
449
+ case to
450
+ when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
451
+ :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
452
+ if metrics_included_from
453
+ to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f
454
+ else
455
+ to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f)
456
+ end
457
+ when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
458
+ if metrics_included_from
459
+ to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f)
460
+ else
461
+ to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f
462
+ end
459
463
  end
460
464
  end
461
- end
462
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
463
-
464
- # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
465
- def to_mass(from, to)
466
- assert_inclusion_of_valid_keys!(MASS_KEYS.values.flatten, from, to)
467
- metric_keys = MASS_KEYS.fetch(:metric)
468
- return self if from == to
469
- metrics_included_from = metric_keys.include?(from)
470
-
471
- case to
472
- when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
473
- :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
474
- :metric_tons
475
- if metrics_included_from
476
- to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f
477
- else
478
- to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f)
479
- end
480
- when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons
481
- if metrics_included_from
482
- to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f)
483
- else
484
- to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f
465
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
466
+
467
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
468
+ def to_mass(from, to)
469
+ assert_inclusion_of_valid_keys!(MASS_KEYS.values.flatten, from, to)
470
+ metric_keys = MASS_KEYS.fetch(:metric)
471
+ return self if from == to
472
+ metrics_included_from = metric_keys.include?(from)
473
+
474
+ case to
475
+ when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
476
+ :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
477
+ :metric_tons
478
+ if metrics_included_from
479
+ to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f
480
+ else
481
+ to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f)
482
+ end
483
+ when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons
484
+ if metrics_included_from
485
+ to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f)
486
+ else
487
+ to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f
488
+ end
485
489
  end
486
490
  end
487
- end
488
- # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
491
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
489
492
 
490
- def to_nearest_value(values = [])
491
- return self if values.length.zero?
493
+ def to_nearest_value(values = [])
494
+ return self if values.length.zero?
492
495
 
493
- value = values.first
494
- difference = (self - value).abs
496
+ value = values.first
497
+ difference = (self - value).abs
495
498
 
496
- values.each do |val|
497
- if (self - val).abs < difference
498
- difference = (self - val).abs
499
- value = val
499
+ values.each do |val|
500
+ if (self - val).abs < difference
501
+ difference = (self - val).abs
502
+ value = val
503
+ end
500
504
  end
501
- end
502
505
 
503
- value
504
- end
506
+ value
507
+ end
505
508
 
506
- def to_percentage(options = {})
507
- unit = options[:unit] || '%'
509
+ def to_percentage(options = {})
510
+ unit = options[:unit] || '%'
508
511
 
509
- "#{pad_precision(options.only(:precision))}#{unit}"
510
- end
512
+ "#{pad_precision(options.only(:precision))}#{unit}"
513
+ end
511
514
 
512
- # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
513
- def to_temperature(from, to)
514
- assert_inclusion_of_valid_keys!(TEMPERATURE_KEYS, from, to)
515
- return self if from == to
515
+ # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
516
+ def to_temperature(from, to)
517
+ assert_inclusion_of_valid_keys!(TEMPERATURE_KEYS, from, to)
518
+ return self if from == to
519
+
520
+ case to
521
+ when :celsius
522
+ from == :kelvin ? (self - 273.15) : ((self - 32.0) * 5.0 / 9.0)
523
+ when :fahrenheit
524
+ from == :kelvin ? (1.8 * (self - 273.15) + 32.0) : ((self * 9.0 / 5.0) + 32.0)
525
+ when :kelvin
526
+ from == :celsius ? (self + 273.15) : (((self - 32.0) * 5.0 / 9.0) + 273.15)
527
+ end
528
+ end
529
+ # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
516
530
 
517
- case to
518
- when :celsius
519
- from == :kelvin ? (self - 273.15) : ((self - 32.0) * 5.0 / 9.0)
520
- when :fahrenheit
521
- from == :kelvin ? (1.8 * (self - 273.15) + 32.0) : ((self * 9.0 / 5.0) + 32.0)
522
- when :kelvin
523
- from == :celsius ? (self + 273.15) : (((self - 32.0) * 5.0 / 9.0) + 273.15)
531
+ def to_time(from, to)
532
+ assert_inclusion_of_valid_keys!(TIME_KEYS, from, to)
533
+ (to_f * 1.send("#{from}_in_seconds").to_f) / 1.send("#{to}_in_seconds").to_f
524
534
  end
525
- end
526
- # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity
527
535
 
528
- def to_time(from, to)
529
- assert_inclusion_of_valid_keys!(TIME_KEYS, from, to)
530
- (to_f * 1.send("#{from}_in_seconds").to_f) / 1.send("#{to}_in_seconds").to_f
531
- end
536
+ def tons_in_ounces
537
+ self * TON
538
+ end
532
539
 
533
- def tons_in_ounces
534
- self * TON
535
- end
540
+ alias_method :ton_in_ounces, :tons_in_ounces
536
541
 
537
- alias_method :ton_in_ounces, :tons_in_ounces
542
+ def weeks_in_seconds
543
+ self * WEEK
544
+ end
538
545
 
539
- def weeks_in_seconds
540
- self * WEEK
541
- end
546
+ alias_method :week_in_seconds, :weeks_in_seconds
542
547
 
543
- alias_method :week_in_seconds, :weeks_in_seconds
548
+ def within?(number, epsilon = 0.01)
549
+ return number == self if epsilon.zero?
544
550
 
545
- def within?(number, epsilon = 0.01)
546
- return number == self if epsilon.zero?
551
+ alpha = to_f
552
+ beta = number.to_f
547
553
 
548
- alpha = to_f
549
- beta = number.to_f
554
+ alpha.zero? || beta.zero? ? (alpha - beta).abs < epsilon : (alpha / beta - 1).abs < epsilon
555
+ end
550
556
 
551
- alpha.zero? || beta.zero? ? (alpha - beta).abs < epsilon : (alpha / beta - 1).abs < epsilon
552
- end
557
+ def yards_in_inches
558
+ self * YARD
559
+ end
553
560
 
554
- def yards_in_inches
555
- self * YARD
556
- end
561
+ alias_method :yard_in_inches, :yards_in_inches
557
562
 
558
- alias_method :yard_in_inches, :yards_in_inches
563
+ def years_in_seconds
564
+ self * YEAR
565
+ end
559
566
 
560
- def years_in_seconds
561
- self * YEAR
562
- end
567
+ alias_method :year_in_seconds, :years_in_seconds
563
568
 
564
- alias_method :year_in_seconds, :years_in_seconds
569
+ private
565
570
 
566
- private
571
+ def assert_inclusion_of_valid_keys!(cns, from, to)
572
+ return if cns.include?(from) && cns.include?(to)
573
+ raise ArgumentError,
574
+ [
575
+ "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}.",
576
+ "Valid keys are: #{cns.map(&:inspect).join(', ')}"
577
+ ].join(' ')
578
+ end
567
579
 
568
- def assert_inclusion_of_valid_keys!(cns, from, to)
569
- return if cns.include?(from) && cns.include?(to)
570
- raise ArgumentError,
571
- [
572
- "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}.",
573
- "Valid keys are: #{cns.map(&:inspect).join(', ')}"
574
- ].join(' ')
575
580
  end
576
-
577
581
  end
578
582
 
579
- Numeric.include(ActiveObject::Numeric) if ActiveObject::Settings.config.autoload_numeric
583
+ Numeric.include(ActiveObject::Numeric) if ActiveObject.configuration.autoload_numeric