Class: Numeric

Inherits:
Object show all
Defined in:
lib/active_object/numeric.rb

Constant Summary collapse

MILLI =
0.001
CENTI =
MILLI * 10
DECI =
CENTI * 10
DECA =
10
HECTO =
DECA * 10
KILO =
HECTO * 10
KILOBYTE =
1024
MEGABYTE =
KILOBYTE * 1024
GIGABYTE =
MEGABYTE * 1024
TERABYTE =
GIGABYTE * 1024
PETABYTE =
TERABYTE * 1024
EXABYTE =
PETABYTE * 1024
FEET =
12
YARD =
FEET * 3
MILE =
YARD * 1760
NAUTICAL_MILE =
MILE * 1.15078
METRIC_TON =
KILO * 1000
POUND =
16
STONE =
POUND * 14
TON =
POUND * 2000
MINUTE =
60
HOUR =
MINUTE * 60
DAY =
HOUR * 24
WEEK =
DAY * 7
YEAR =
DAY * 365.25
DECADE =
YEAR * 10
CENTURY =
DECADE * 10
MILLENNIUM =
CENTURY * 10

Instance Method Summary collapse

Instance Method Details

#add(n) ⇒ Object



38
39
40
# File 'lib/active_object/numeric.rb', line 38

def add(n)
  self + n
end

#bytes_in_bytesObject Also known as: byte_in_bytes



42
43
44
# File 'lib/active_object/numeric.rb', line 42

def bytes_in_bytes
  self
end

#centigrams_in_gramsObject Also known as: centigram_in_grams



48
49
50
# File 'lib/active_object/numeric.rb', line 48

def centigrams_in_grams
  self * CENTI
end

#centimeters_in_metersObject Also known as: centimeter_in_meters



54
55
56
# File 'lib/active_object/numeric.rb', line 54

def centimeters_in_meters
  self * CENTI
end

#centuries_in_secondsObject Also known as: century_in_seconds



60
61
62
# File 'lib/active_object/numeric.rb', line 60

def centuries_in_seconds
  self * CENTURY
end

#days_in_secondsObject Also known as: day_in_seconds



66
67
68
# File 'lib/active_object/numeric.rb', line 66

def days_in_seconds
  self * DAY
end

#decades_in_secondsObject Also known as: decade_in_seconds



72
73
74
# File 'lib/active_object/numeric.rb', line 72

def decades_in_seconds
  self * DECADE
end

#decagrams_in_gramsObject Also known as: decagram_in_grams



78
79
80
# File 'lib/active_object/numeric.rb', line 78

def decagrams_in_grams
  self * DECA
end

#decameters_in_metersObject Also known as: decameter_in_meters



84
85
86
# File 'lib/active_object/numeric.rb', line 84

def decameters_in_meters
  self * DECA
end

#decigrams_in_gramsObject Also known as: decigram_in_grams



90
91
92
# File 'lib/active_object/numeric.rb', line 90

def decigrams_in_grams
  self * DECI
end

#decimeters_in_metersObject Also known as: decimeter_in_meters



96
97
98
# File 'lib/active_object/numeric.rb', line 96

def decimeters_in_meters
  self * DECI
end

#distance(n) ⇒ Object



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

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

#divide(n) ⇒ Object



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

def divide(n)
  self / n
end

#exabytes_in_bytesObject Also known as: exabyte_in_bytes



110
111
112
# File 'lib/active_object/numeric.rb', line 110

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inchesObject Also known as: foot_in_inches



116
117
118
# File 'lib/active_object/numeric.rb', line 116

def feet_in_inches
  self * FEET
end

#gigabytes_in_bytesObject Also known as: gigabyte_in_bytes



122
123
124
# File 'lib/active_object/numeric.rb', line 122

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_gramsObject Also known as: gram_in_grams



128
129
130
# File 'lib/active_object/numeric.rb', line 128

def grams_in_grams
  self
end

#hectograms_in_gramsObject Also known as: hectogram_in_grams



134
135
136
# File 'lib/active_object/numeric.rb', line 134

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_metersObject Also known as: hectometer_in_meters



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

def hectometers_in_meters
  self * HECTO
end

#hours_in_secondsObject Also known as: hour_in_seconds



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

def hours_in_seconds
  self * HOUR
end

#inches_in_inchesObject Also known as: inch_in_inches



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

def inches_in_inches
  self
end

#kilobytes_in_bytesObject Also known as: kilobyte_in_bytes



158
159
160
# File 'lib/active_object/numeric.rb', line 158

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_gramsObject Also known as: kilogram_in_grams



170
171
172
# File 'lib/active_object/numeric.rb', line 170

def kilograms_in_grams
  self * KILO
end

#kilometers_in_metersObject Also known as: kilometer_in_meters



164
165
166
# File 'lib/active_object/numeric.rb', line 164

def kilometers_in_meters
  self * KILO
end

#megabytes_in_bytesObject Also known as: megabyte_in_bytes



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

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_metersObject Also known as: meter_in_meters



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

def meters_in_meters
  self
end

#metric_tons_in_gramsObject Also known as: metric_ton_in_grams



176
177
178
# File 'lib/active_object/numeric.rb', line 176

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inchesObject Also known as: mile_in_inches



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

def miles_in_inches
  self * MILE
end

#millenniums_in_secondsObject Also known as: millennium_in_seconds



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

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_gramsObject Also known as: milligram_in_grams



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

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_metersObject Also known as: millimeter_in_meters



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

def millimeters_in_meters
  self * MILLI
end

#minutes_in_secondsObject Also known as: minute_in_seconds



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

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/active_object/numeric.rb', line 229

def multiple_of?(number)
  number != 0 ? modulo(number).zero? : zero?
end

#multiply(n) ⇒ Object



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

def multiply(n)
  self * n
end

#nautical_miles_in_inchesObject Also known as: nautical_mile_in_inches



234
235
236
# File 'lib/active_object/numeric.rb', line 234

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  self < 0
end

#ordinalObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_object/numeric.rb', line 245

def ordinal
  abs_number = abs

  if (11..13).include?(abs_number % 100)
    "th"
  else
    case abs_number % 10
      when 1; "st"
      when 2; "nd"
      when 3; "rd"
      else    "th"
    end
  end
end

#ordinalizeObject



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

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

#ounces_in_ouncesObject Also known as: ounce_in_ounces



267
268
269
# File 'lib/active_object/numeric.rb', line 267

def ounces_in_ounces
  self
end

#pad(options = {}) ⇒ Object



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

def pad(options={})
  pad_number = options.fetch(:pad_number, 0)
  precision  = options.fetch(:precision, 3)

  to_s.rjust(precision, pad_number.to_s)
end

#pad_precision(options = {}) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/active_object/numeric.rb', line 280

def pad_precision(options={})
  pad_number = options.fetch(:pad_number, 0)
  precision  = options.fetch(:precision, 2)
  separator  = options.fetch(:separator, ".")
  string     = to_s

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

#petabytes_in_bytesObject Also known as: petabyte_in_bytes



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

def petabytes_in_bytes
  self * PETABYTE
end

#positive?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/active_object/numeric.rb', line 299

def positive?
  self > 0
end

#pounds_in_ouncesObject Also known as: pound_in_ounces



303
304
305
# File 'lib/active_object/numeric.rb', line 303

def pounds_in_ounces
  self * POUND
end

#power(n) ⇒ Object



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

def power(n)
  self ** n
end

#root(n) ⇒ Object



313
314
315
# File 'lib/active_object/numeric.rb', line 313

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

#seconds_in_secondsObject Also known as: second_in_seconds



317
318
319
# File 'lib/active_object/numeric.rb', line 317

def seconds_in_seconds
  self
end

#stones_in_ouncesObject Also known as: stone_in_ounces



323
324
325
# File 'lib/active_object/numeric.rb', line 323

def stones_in_ounces
  self * STONE
end

#subtract(n) ⇒ Object



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

def subtract(n)
  self - n
end

#terabytes_in_bytesObject Also known as: terabyte_in_bytes



333
334
335
# File 'lib/active_object/numeric.rb', line 333

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/active_object/numeric.rb', line 339

def to_byte(from, to)
  valid_keys = [
    :byte,     :bytes,
    :kilobyte, :kilobytes,
    :megabyte, :megabytes,
    :gigabyte, :gigabytes,
    :terabyte, :terabytes,
    :petabyte, :petabytes,
    :exabyte,  :exabytes
  ]

  unless valid_keys.include?(from) && valid_keys.include?(to)
    raise ArgumentError,
      "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end

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

#to_currency(options = {}) ⇒ Object



358
359
360
361
362
# File 'lib/active_object/numeric.rb', line 358

def to_currency(options={})
  unit = options.fetch(:unit, "$")

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

#to_length(from, to) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/active_object/numeric.rb', line 364

def to_length(from, to)
  valid_keys = [
    :meter,         :meters,
    :millimeter,    :millimeters,
    :centimeter,    :centimeters,
    :decimeter,     :decimeters,
    :decameter,     :decameters,
    :hectometer,    :hectometers,
    :kilometer,     :kilometers,
    :inch,          :inches,
    :foot,          :feet,
    :yard,          :yards,
    :mile,          :miles,
    :nautical_mile, :nautical_miles
  ]

  unless valid_keys.include?(from) && valid_keys.include?(to)
    raise ArgumentError,
      "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end

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

#to_mass(from, to) ⇒ Object



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/active_object/numeric.rb', line 403

def to_mass(from, to)
  valid_keys = [
    :gram,       :grams,
    :milligram,  :milligrams,
    :centigram,  :centigrams,
    :decigram,   :decigrams,
    :decagram,   :decagrams,
    :hectogram,  :hectograms,
    :kilogram,   :kilograms,
    :metric_ton, :metric_tons,
    :ounce,      :ounces,
    :pound,      :pounds,
    :stone,      :stones,
    :ton,        :tons
  ]

  unless valid_keys.include?(from) && valid_keys.include?(to)
    raise ArgumentError,
      "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end

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

#to_nearest_value(values = []) ⇒ Object



442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/active_object/numeric.rb', line 442

def to_nearest_value(values=[])
  return(self) if values.size.zero?
  value      = values.first
  difference = (self - value).abs

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

#to_percentage(options = {}) ⇒ Object



456
457
458
459
460
# File 'lib/active_object/numeric.rb', line 456

def to_percentage(options={})
  unit = options.fetch(:unit, "%")

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

#to_temperature(from, to) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/active_object/numeric.rb', line 462

def to_temperature(from, to)
  valid_keys = [:celsius, :fahrenheit, :kelvin]

  unless valid_keys.include?(from) && valid_keys.include?(to)
    raise ArgumentError,
      "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end

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

#to_time(from, to) ⇒ Object



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/active_object/numeric.rb', line 482

def to_time(from, to)
  valid_keys = [
    :second,     :seconds,
    :minute,     :minutes,
    :hour,       :hours,
    :day,        :days,
    :week,       :weeks,
    :year,       :years,
    :decade,     :decades,
    :century,    :centuries,
    :millennium, :millenniums
  ]

  unless valid_keys.include?(from) && valid_keys.include?(to)
    raise ArgumentError,
      "Unknown key(s): from: #{from.inspect} and to: #{to.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end

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

#tons_in_ouncesObject Also known as: ton_in_ounces



503
504
505
# File 'lib/active_object/numeric.rb', line 503

def tons_in_ounces
  self * TON
end

#weeks_in_secondsObject Also known as: week_in_seconds



509
510
511
# File 'lib/active_object/numeric.rb', line 509

def weeks_in_seconds
  self * WEEK
end

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

Returns:

  • (Boolean)


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

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

  a, b = to_f, number.to_f

  (a.zero? || b.zero?) ? (a - b).abs < epsilon : (a / b - 1).abs < epsilon
end

#yards_in_inchesObject Also known as: yard_in_inches



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

def yards_in_inches
  self * YARD
end

#years_in_secondsObject Also known as: year_in_seconds



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

def years_in_seconds
  self * YEAR
end