Module: ActiveObject::Numeric

Defined in:
lib/active_object/numeric.rb

Constant Summary collapse

MILLI =
0.001
CENTI =
MILLI * 10.0
DECI =
CENTI * 10.0
DECA =
10.0
HECTO =
DECA * 10.0
KILO =
HECTO * 10.0
KILOBYTE =
1024.0
MEGABYTE =
KILOBYTE * 1024.0
GIGABYTE =
MEGABYTE * 1024.0
TERABYTE =
GIGABYTE * 1024.0
PETABYTE =
TERABYTE * 1024.0
EXABYTE =
PETABYTE * 1024.0
FEET =
12.0
YARD =
FEET * 3.0
MILE =
YARD * 1760.0
NAUTICAL_MILE =
MILE * 1.15078
METRIC_TON =
KILO * 1000.0
POUND =
16.0
STONE =
POUND * 14.0
TON =
POUND * 2000.0
MINUTE =
60.0
HOUR =
MINUTE * 60.0
DAY =
HOUR * 24.0
WEEK =
DAY * 7.0
YEAR =
DAY * 365.25
DECADE =
YEAR * 10.0
CENTURY =
DECADE * 10.0
MILLENNIUM =
CENTURY * 10.0
BYTE_KEYS =
[
  :byte, :bytes, :kilobyte, :kilobytes, :megabyte, :megabytes, :gigabyte, :gigabytes, :terabyte,
  :terabytes, :petabyte, :petabytes, :exabyte, :exabytes
].freeze
LENGTH_KEYS =
{
  metric: [
    :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
    :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
  ],
  imperical: [
    :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
  ]
}.freeze
MASS_KEYS =
{
  metric: [
    :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
    :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
    :metric_tons
  ],
  imperical: [:ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons]
}.freeze
TEMPERATURE_KEYS =
[:celsius, :fahrenheit, :kelvin].freeze
TIME_KEYS =
[
  :second, :seconds, :minute, :minutes, :hour, :hours, :day, :days, :week, :weeks, :year, :years,
  :decade, :decades, :century, :centuries, :millennium, :millenniums
].freeze

Instance Method Summary collapse

Instance Method Details

#add(num) ⇒ Object



58
59
60
# File 'lib/active_object/numeric.rb', line 58

def add(num)
  self + num
end

#bytes_in_bytesObject Also known as: byte_in_bytes



62
63
64
# File 'lib/active_object/numeric.rb', line 62

def bytes_in_bytes
  self
end

#centigrams_in_gramsObject Also known as: centigram_in_grams



68
69
70
# File 'lib/active_object/numeric.rb', line 68

def centigrams_in_grams
  self * CENTI
end

#centimeters_in_metersObject Also known as: centimeter_in_meters



74
75
76
# File 'lib/active_object/numeric.rb', line 74

def centimeters_in_meters
  self * CENTI
end

#centuries_in_secondsObject Also known as: century_in_seconds



80
81
82
# File 'lib/active_object/numeric.rb', line 80

def centuries_in_seconds
  self * CENTURY
end

#clamp(minimum, maximum = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_object/numeric.rb', line 87

def clamp(minimum, maximum = nil)
  if maximum.nil? && minimum.is_a?(Range)
    min_min = minimum.min
    min_max = minimum.max

    return(min_min) if self < min_min
    self > min_max ? min_max : self
  else
    return(minimum) if self < minimum
    self > maximum ? maximum : self
  end
end

#days_in_secondsObject Also known as: day_in_seconds

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



101
102
103
# File 'lib/active_object/numeric.rb', line 101

def days_in_seconds
  self * DAY
end

#decades_in_secondsObject Also known as: decade_in_seconds



107
108
109
# File 'lib/active_object/numeric.rb', line 107

def decades_in_seconds
  self * DECADE
end

#decagrams_in_gramsObject Also known as: decagram_in_grams



113
114
115
# File 'lib/active_object/numeric.rb', line 113

def decagrams_in_grams
  self * DECA
end

#decameters_in_metersObject Also known as: decameter_in_meters



119
120
121
# File 'lib/active_object/numeric.rb', line 119

def decameters_in_meters
  self * DECA
end

#decigrams_in_gramsObject Also known as: decigram_in_grams



125
126
127
# File 'lib/active_object/numeric.rb', line 125

def decigrams_in_grams
  self * DECI
end

#decimeters_in_metersObject Also known as: decimeter_in_meters



131
132
133
# File 'lib/active_object/numeric.rb', line 131

def decimeters_in_meters
  self * DECI
end

#decrement(amount = 1.0) ⇒ Object



137
138
139
# File 'lib/active_object/numeric.rb', line 137

def decrement(amount = 1.0)
  self + amount
end

#degrees_to_radiansObject Also known as: degree_to_radians



141
142
143
# File 'lib/active_object/numeric.rb', line 141

def degrees_to_radians
  self * Math::PI / 180.0
end

#distance(num) ⇒ Object



147
148
149
# File 'lib/active_object/numeric.rb', line 147

def distance(num)
  (self - num).abs
end

#divide(num) ⇒ Object



151
152
153
# File 'lib/active_object/numeric.rb', line 151

def divide(num)
  self / num
end

#exabytes_in_bytesObject Also known as: exabyte_in_bytes



155
156
157
# File 'lib/active_object/numeric.rb', line 155

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inchesObject Also known as: foot_in_inches



161
162
163
# File 'lib/active_object/numeric.rb', line 161

def feet_in_inches
  self * FEET
end

#gigabytes_in_bytesObject Also known as: gigabyte_in_bytes



167
168
169
# File 'lib/active_object/numeric.rb', line 167

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_gramsObject Also known as: gram_in_grams



173
174
175
# File 'lib/active_object/numeric.rb', line 173

def grams_in_grams
  self
end

#greater_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/active_object/numeric.rb', line 179

def greater_than?(num)
  self > num
end

#greater_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/active_object/numeric.rb', line 183

def greater_than_or_equal_to?(num)
  self >= num
end

#hectograms_in_gramsObject Also known as: hectogram_in_grams



187
188
189
# File 'lib/active_object/numeric.rb', line 187

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_metersObject Also known as: hectometer_in_meters



193
194
195
# File 'lib/active_object/numeric.rb', line 193

def hectometers_in_meters
  self * HECTO
end

#hours_in_secondsObject Also known as: hour_in_seconds



199
200
201
# File 'lib/active_object/numeric.rb', line 199

def hours_in_seconds
  self * HOUR
end

#inches_in_inchesObject Also known as: inch_in_inches



205
206
207
# File 'lib/active_object/numeric.rb', line 205

def inches_in_inches
  self
end

#increment(amount = 1.0) ⇒ Object



211
212
213
# File 'lib/active_object/numeric.rb', line 211

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/active_object/numeric.rb', line 215

def inside?(start, finish)
  (start < self) && (self < finish)
end

#kilobytes_in_bytesObject Also known as: kilobyte_in_bytes



219
220
221
# File 'lib/active_object/numeric.rb', line 219

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_gramsObject Also known as: kilogram_in_grams



231
232
233
# File 'lib/active_object/numeric.rb', line 231

def kilograms_in_grams
  self * KILO
end

#kilometers_in_metersObject Also known as: kilometer_in_meters



225
226
227
# File 'lib/active_object/numeric.rb', line 225

def kilometers_in_meters
  self * KILO
end

#less_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/active_object/numeric.rb', line 237

def less_than?(num)
  self < num
end

#less_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/active_object/numeric.rb', line 241

def less_than_or_equal_to?(num)
  self <= num
end

#megabytes_in_bytesObject Also known as: megabyte_in_bytes



251
252
253
# File 'lib/active_object/numeric.rb', line 251

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_metersObject Also known as: meter_in_meters



257
258
259
# File 'lib/active_object/numeric.rb', line 257

def meters_in_meters
  self
end

#metric_tons_in_gramsObject Also known as: metric_ton_in_grams



245
246
247
# File 'lib/active_object/numeric.rb', line 245

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inchesObject Also known as: mile_in_inches



263
264
265
# File 'lib/active_object/numeric.rb', line 263

def miles_in_inches
  self * MILE
end

#millenniums_in_secondsObject Also known as: millennium_in_seconds



269
270
271
# File 'lib/active_object/numeric.rb', line 269

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_gramsObject Also known as: milligram_in_grams



275
276
277
# File 'lib/active_object/numeric.rb', line 275

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_metersObject Also known as: millimeter_in_meters



281
282
283
# File 'lib/active_object/numeric.rb', line 281

def millimeters_in_meters
  self * MILLI
end

#minutes_in_secondsObject Also known as: minute_in_seconds



287
288
289
# File 'lib/active_object/numeric.rb', line 287

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/active_object/numeric.rb', line 297

def multiple_of?(number)
  number.zero? ? zero? : modulo(number).zero?
end

#multiply(num) ⇒ Object



293
294
295
# File 'lib/active_object/numeric.rb', line 293

def multiply(num)
  self * num
end

#nautical_miles_in_inchesObject Also known as: nautical_mile_in_inches



301
302
303
# File 'lib/active_object/numeric.rb', line 301

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative?Boolean

rubocop:disable Style/NumericPredicate

Returns:

  • (Boolean)


308
309
310
# File 'lib/active_object/numeric.rb', line 308

def negative?
  self < 0
end

#ordinalObject

rubocop:enable Style/NumericPredicate



313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/active_object/numeric.rb', line 313

def ordinal
  if (11..13).cover?(abs % 100)
    'th'
  else
    case abs % 10
    when 1 then 'st'
    when 2 then 'nd'
    when 3 then 'rd'
    else 'th'
    end
  end
end

#ordinalizeObject



326
327
328
# File 'lib/active_object/numeric.rb', line 326

def ordinalize
  "#{self}#{ordinal}"
end

#ounces_in_ouncesObject Also known as: ounce_in_ounces



330
331
332
# File 'lib/active_object/numeric.rb', line 330

def ounces_in_ounces
  self
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


336
337
338
# File 'lib/active_object/numeric.rb', line 336

def outside?(start, finish)
  (self < start) || (finish < self)
end

#pad(options = {}) ⇒ Object



340
341
342
343
344
345
# File 'lib/active_object/numeric.rb', line 340

def pad(options = {})
  pad_number = options[:pad_number] || 0
  precision = options[:precision] || 3

  to_s.rjust(precision, pad_number.to_s)
end

#pad_precision(options = {}) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/active_object/numeric.rb', line 349

def pad_precision(options = {})
  pad_number = options[:pad_number] || 0
  precision = options[:precision] || 2
  separator = options[:separator] || '.'
  string = to_s

  string << separator unless string.include?(separator)
  ljust_count = string.split(separator).first.length
  ljust_count += (string.count(separator) + precision) if precision.positive?
  if ljust_count >= string.length
    string.ljust(ljust_count, pad_number.to_s)
  else
    string[0..(ljust_count - 1)]
  end
end

#petabytes_in_bytesObject Also known as: petabyte_in_bytes

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength, Metrics/PerceivedComplexity



367
368
369
# File 'lib/active_object/numeric.rb', line 367

def petabytes_in_bytes
  self * PETABYTE
end

#positive?Boolean

rubocop:disable Style/NumericPredicate

Returns:

  • (Boolean)


374
375
376
# File 'lib/active_object/numeric.rb', line 374

def positive?
  self > 0
end

#pounds_in_ouncesObject Also known as: pound_in_ounces

rubocop:enable Style/NumericPredicate



379
380
381
# File 'lib/active_object/numeric.rb', line 379

def pounds_in_ounces
  self * POUND
end

#power(num) ⇒ Object



385
386
387
# File 'lib/active_object/numeric.rb', line 385

def power(num)
  self**num
end

#root(num) ⇒ Object



389
390
391
# File 'lib/active_object/numeric.rb', line 389

def root(num)
  self**(1.0 / num)
end

#seconds_in_secondsObject Also known as: second_in_seconds



393
394
395
# File 'lib/active_object/numeric.rb', line 393

def seconds_in_seconds
  self
end

#stones_in_ouncesObject Also known as: stone_in_ounces



399
400
401
# File 'lib/active_object/numeric.rb', line 399

def stones_in_ounces
  self * STONE
end

#subtract(num) ⇒ Object



405
406
407
# File 'lib/active_object/numeric.rb', line 405

def subtract(num)
  self - num
end

#terabytes_in_bytesObject Also known as: terabyte_in_bytes



409
410
411
# File 'lib/active_object/numeric.rb', line 409

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



415
416
417
418
419
# File 'lib/active_object/numeric.rb', line 415

def to_byte(from, to)
  assert_valid_keys!(BYTE_KEYS, from, to)

  to_f * 1.send("#{from}_in_bytes").to_f / 1.send("#{to}_in_bytes").to_f
end

#to_currency(options = {}) ⇒ Object



421
422
423
424
425
# File 'lib/active_object/numeric.rb', line 421

def to_currency(options = {})
  unit = options[:unit] || '$'

  "#{unit}#{pad_precision(options.only(:precision))}"
end

#to_length(from, to) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/active_object/numeric.rb', line 428

def to_length(from, to)
  assert_valid_keys!(LENGTH_KEYS.values.flatten, from, to)
  metric_keys = LENGTH_KEYS.fetch(:metric)
  return(self) if from == to
  metrics_included_from = metric_keys.include?(from)

  case to
  when :meter, :meters, :millimeter, :millimeters, :centimeter, :centimeters, :decimeter,
       :decimeters, :decameter, :decameters, :hectometer, :hectometers, :kilometer, :kilometers
    if metrics_included_from
      to_f * 1.send("#{from}_in_meters").to_f / 1.send("#{to}_in_meters").to_f
    else
      to_f * ((1.send("#{from}_in_inches").to_f * 0.0254) / 1.send("#{to}_in_meters").to_f)
    end
  when :inch, :inches, :foot, :feet, :yard, :yards, :mile, :miles, :nautical_mile, :nautical_miles
    if metrics_included_from
      to_f * ((1.send("#{from}_in_meters").to_f * 39.3701) / 1.send("#{to}_in_inches").to_f)
    else
      to_f * 1.send("#{from}_in_inches").to_f / 1.send("#{to}_in_inches").to_f
    end
  end
end

#to_mass(from, to) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'lib/active_object/numeric.rb', line 453

def to_mass(from, to)
  assert_valid_keys!(MASS_KEYS.values.flatten, from, to)
  metric_keys = MASS_KEYS.fetch(:metric)
  return(self) if from == to
  metrics_included_from = metric_keys.include?(from)

  case to
  when :gram, :grams, :milligram, :milligrams, :centigram, :centigrams, :decigram, :decigrams,
       :decagram, :decagrams, :hectogram, :hectograms, :kilogram, :kilograms, :metric_ton,
       :metric_tons
    if metrics_included_from
      to_f * 1.send("#{from}_in_grams").to_f / 1.send("#{to}_in_grams").to_f
    else
      to_f * ((1.send("#{from}_in_ounces") * 28.3495).to_f / 1.send("#{to}_in_grams").to_f)
    end
  when :ounce, :ounces, :pound, :pounds, :stone, :stones, :ton, :tons
    if metrics_included_from
      to_f * ((1.send("#{from}_in_grams") * 0.035274).to_f / 1.send("#{to}_in_ounces").to_f)
    else
      to_f * 1.send("#{from}_in_ounces").to_f / 1.send("#{to}_in_ounces").to_f
    end
  end
end

#to_nearest_value(values = []) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/MethodLength



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/active_object/numeric.rb', line 478

def to_nearest_value(values = [])
  return(self) if values.length.zero?

  value = values.first
  difference = (self - value).abs

  values.each do |val|
    if (self - val).abs < difference
      difference = (self - val).abs
      value = val
    end
  end

  value
end

#to_percentage(options = {}) ⇒ Object



494
495
496
497
498
# File 'lib/active_object/numeric.rb', line 494

def to_percentage(options = {})
  unit = options[:unit] || '%'

  "#{pad_precision(options.only(:precision))}#{unit}"
end

#to_temperature(from, to) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity



501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/active_object/numeric.rb', line 501

def to_temperature(from, to)
  assert_valid_keys!(TEMPERATURE_KEYS, from, to)
  return(self) if from == to

  case to
  when :celsius
    from == :kelvin ? (self - 273.15) : ((self - 32.0) * 5.0 / 9.0)
  when :fahrenheit
    from == :kelvin ? (1.8 * (self - 273.15) + 32.0) : ((self * 9.0 / 5.0) + 32.0)
  when :kelvin
    from == :celsius ? (self + 273.15) : (((self - 32.0) * 5.0 / 9.0) + 273.15)
  end
end

#to_time(from, to) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity



516
517
518
519
520
# File 'lib/active_object/numeric.rb', line 516

def to_time(from, to)
  assert_valid_keys!(TIME_KEYS, from, to)

  (to_f * 1.send("#{from}_in_seconds").to_f) / 1.send("#{to}_in_seconds").to_f
end

#tons_in_ouncesObject Also known as: ton_in_ounces



522
523
524
# File 'lib/active_object/numeric.rb', line 522

def tons_in_ounces
  self * TON
end

#weeks_in_secondsObject Also known as: week_in_seconds



528
529
530
# File 'lib/active_object/numeric.rb', line 528

def weeks_in_seconds
  self * WEEK
end

#within?(number, epsilon = 0.01) ⇒ Boolean

Returns:

  • (Boolean)


534
535
536
537
538
539
540
541
# File 'lib/active_object/numeric.rb', line 534

def within?(number, epsilon = 0.01)
  return(self == number) if epsilon.zero?

  alpha = to_f
  beta = number.to_f

  alpha.zero? || beta.zero? ? (alpha - beta).abs < epsilon : (alpha / beta - 1).abs < epsilon
end

#yards_in_inchesObject Also known as: yard_in_inches



543
544
545
# File 'lib/active_object/numeric.rb', line 543

def yards_in_inches
  self * YARD
end

#years_in_secondsObject Also known as: year_in_seconds



549
550
551
# File 'lib/active_object/numeric.rb', line 549

def years_in_seconds
  self * YEAR
end