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
]
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
  ]
}
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]
}
TEMPERATURE_KEYS =
[:celsius, :fahrenheit, :kelvin]
TIME_KEYS =
[
  :second, :seconds, :minute, :minutes, :hour, :hours, :day, :days,
  :week, :weeks, :year, :years, :decade, :decades, :century, :centuries,
  :millennium, :millenniums
]

Instance Method Summary collapse

Instance Method Details

#add(n) ⇒ Object



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

def add(n)
  self + n
end

#bytes_in_bytesObject Also known as: byte_in_bytes



70
71
72
# File 'lib/active_object/numeric.rb', line 70

def bytes_in_bytes
  self
end

#centigrams_in_gramsObject Also known as: centigram_in_grams



76
77
78
# File 'lib/active_object/numeric.rb', line 76

def centigrams_in_grams
  self * CENTI
end

#centimeters_in_metersObject Also known as: centimeter_in_meters



82
83
84
# File 'lib/active_object/numeric.rb', line 82

def centimeters_in_meters
  self * CENTI
end

#centuries_in_secondsObject Also known as: century_in_seconds



88
89
90
# File 'lib/active_object/numeric.rb', line 88

def centuries_in_seconds
  self * CENTURY
end

#clamp(min, max = nil) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/active_object/numeric.rb', line 94

def clamp(min, max=nil)
  if max.nil? && min.is_a?(Range)
    self < min.min ? min.min : self > min.max ? min.max : self
  else
    self < min ? min : self > max ? max : self
  end
end

#days_in_secondsObject Also known as: day_in_seconds



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

def days_in_seconds
  self * DAY
end

#decades_in_secondsObject Also known as: decade_in_seconds



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

def decades_in_seconds
  self * DECADE
end

#decagrams_in_gramsObject Also known as: decagram_in_grams



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

def decagrams_in_grams
  self * DECA
end

#decameters_in_metersObject Also known as: decameter_in_meters



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

def decameters_in_meters
  self * DECA
end

#decigrams_in_gramsObject Also known as: decigram_in_grams



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

def decigrams_in_grams
  self * DECI
end

#decimeters_in_metersObject Also known as: decimeter_in_meters



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

def decimeters_in_meters
  self * DECI
end

#decrement(amount = 1.0) ⇒ Object



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

def decrement(amount=1.0)
  self + amount
end

#degrees_to_radiansObject Also known as: degree_to_radians



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

def degrees_to_radians
  self * Math::PI / 180.0
end

#distance(n) ⇒ Object



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

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

#divide(n) ⇒ Object



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

def divide(n)
  self / n
end

#exabytes_in_bytesObject Also known as: exabyte_in_bytes



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

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inchesObject Also known as: foot_in_inches



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

def feet_in_inches
  self * FEET
end

#gigabytes_in_bytesObject Also known as: gigabyte_in_bytes



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

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_gramsObject Also known as: gram_in_grams



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

def grams_in_grams
  self
end

#greater_than?(n) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than?(n)
  self > n
end

#greater_than_or_equal_to?(n) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than_or_equal_to?(n)
  self >= n
end

#hectograms_in_gramsObject Also known as: hectogram_in_grams



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

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_metersObject Also known as: hectometer_in_meters



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

def hectometers_in_meters
  self * HECTO
end

#hours_in_secondsObject Also known as: hour_in_seconds



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

def hours_in_seconds
  self * HOUR
end

#inches_in_inchesObject Also known as: inch_in_inches



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

def inches_in_inches
  self
end

#increment(amount = 1.0) ⇒ Object



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

def increment(amount=1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#kilobytes_in_bytesObject Also known as: kilobyte_in_bytes



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

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_gramsObject Also known as: kilogram_in_grams



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

def kilograms_in_grams
  self * KILO
end

#kilometers_in_metersObject Also known as: kilometer_in_meters



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

def kilometers_in_meters
  self * KILO
end

#less_than?(n) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than?(n)
  self < n
end

#less_than_or_equal_to?(n) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than_or_equal_to?(n)
  self <= n
end

#megabytes_in_bytesObject Also known as: megabyte_in_bytes



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

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_metersObject Also known as: meter_in_meters



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

def meters_in_meters
  self
end

#metric_tons_in_gramsObject Also known as: metric_ton_in_grams



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

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inchesObject Also known as: mile_in_inches



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

def miles_in_inches
  self * MILE
end

#millenniums_in_secondsObject Also known as: millennium_in_seconds



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

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_gramsObject Also known as: milligram_in_grams



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

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_metersObject Also known as: millimeter_in_meters



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

def millimeters_in_meters
  self * MILLI
end

#minutes_in_secondsObject Also known as: minute_in_seconds



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

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#multiply(n) ⇒ Object



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

def multiply(n)
  self * n
end

#nautical_miles_in_inchesObject Also known as: nautical_mile_in_inches



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

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative?Boolean

Returns:

  • (Boolean)


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

def negative?
  self < 0
end

#ordinalObject



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

def ordinal
  abs_number = abs

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

#ordinalizeObject



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

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

#ounces_in_ouncesObject Also known as: ounce_in_ounces



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

def ounces_in_ounces
  self
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pad(options = {}) ⇒ Object



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

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



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

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.length
  ljust_count += (string.count(separator) + precision) if precision > 0
  num_count =  string.length
  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



361
362
363
# File 'lib/active_object/numeric.rb', line 361

def petabytes_in_bytes
  self * PETABYTE
end

#positive?Boolean

Returns:

  • (Boolean)


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

def positive?
  self > 0
end

#pounds_in_ouncesObject Also known as: pound_in_ounces



371
372
373
# File 'lib/active_object/numeric.rb', line 371

def pounds_in_ounces
  self * POUND
end

#power(n) ⇒ Object



377
378
379
# File 'lib/active_object/numeric.rb', line 377

def power(n)
  self ** n
end

#root(n) ⇒ Object



381
382
383
# File 'lib/active_object/numeric.rb', line 381

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

#seconds_in_secondsObject Also known as: second_in_seconds



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

def seconds_in_seconds
  self
end

#stones_in_ouncesObject Also known as: stone_in_ounces



391
392
393
# File 'lib/active_object/numeric.rb', line 391

def stones_in_ounces
  self * STONE
end

#subtract(n) ⇒ Object



397
398
399
# File 'lib/active_object/numeric.rb', line 397

def subtract(n)
  self - n
end

#terabytes_in_bytesObject Also known as: terabyte_in_bytes



401
402
403
# File 'lib/active_object/numeric.rb', line 401

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



407
408
409
410
411
412
413
414
# File 'lib/active_object/numeric.rb', line 407

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

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

#to_currency(options = {}) ⇒ Object



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

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

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

#to_length(from, to) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/active_object/numeric.rb', line 422

def to_length(from, to)
  metric_keys = LENGTH_KEYS.fetch(:metric)
  valid_keys = LENGTH_KEYS.collect { |k, v| v }.flatten

  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 metric_keys.include?(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 metric_keys.include?(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



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

def to_mass(from, to)
  metric_keys = MASS_KEYS.fetch(:metric)
  valid_keys = MASS_KEYS.collect { |k, v| v }.flatten

  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 metric_keys.include?(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 metric_keys.include?(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



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

def to_nearest_value(values=[])
  return(self) if values.length.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



492
493
494
495
496
# File 'lib/active_object/numeric.rb', line 492

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

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

#to_temperature(from, to) ⇒ Object



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

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

  case to
  when from
    self
  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



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

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

  (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



525
526
527
# File 'lib/active_object/numeric.rb', line 525

def tons_in_ounces
  self * TON
end

#weeks_in_secondsObject Also known as: week_in_seconds



531
532
533
# File 'lib/active_object/numeric.rb', line 531

def weeks_in_seconds
  self * WEEK
end

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

Returns:

  • (Boolean)


537
538
539
540
541
542
543
# File 'lib/active_object/numeric.rb', line 537

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



545
546
547
# File 'lib/active_object/numeric.rb', line 545

def yards_in_inches
  self * YARD
end

#years_in_secondsObject Also known as: year_in_seconds



551
552
553
# File 'lib/active_object/numeric.rb', line 551

def years_in_seconds
  self * YEAR
end