Module: ActiveObject::Numeric

Defined in:
lib/active_object/numeric.rb

Instance Method Summary collapse

Instance Method Details

#add(num) ⇒ Object



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

def add(num)
  self + num
end

#bytes_in_bytes ⇒ Object Also known as: byte_in_bytes



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

def bytes_in_bytes
  self
end

#centigrams_in_grams ⇒ Object Also known as: centigram_in_grams



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

def centigrams_in_grams
  self * CENTI
end

#centimeters_in_meters ⇒ Object Also known as: centimeter_in_meters



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

def centimeters_in_meters
  self * CENTI
end

#centuries_in_seconds ⇒ Object Also known as: century_in_seconds



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

def centuries_in_seconds
  self * CENTURY
end

#clamp(minimum, maximum = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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

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

    return min_min if min_min > self

    min_max < self ? min_max : self
  else
    return minimum if minimum > self

    maximum < self ? maximum : self
  end
end

#days_in_seconds ⇒ Object Also known as: day_in_seconds

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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

def days_in_seconds
  self * DAY
end

#decades_in_seconds ⇒ Object Also known as: decade_in_seconds



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

def decades_in_seconds
  self * DECADE
end

#decagrams_in_grams ⇒ Object Also known as: decagram_in_grams



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

def decagrams_in_grams
  self * DECA
end

#decameters_in_meters ⇒ Object Also known as: decameter_in_meters



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

def decameters_in_meters
  self * DECA
end

#decigrams_in_grams ⇒ Object Also known as: decigram_in_grams



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

def decigrams_in_grams
  self * DECI
end

#decimeters_in_meters ⇒ Object Also known as: decimeter_in_meters



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

def decimeters_in_meters
  self * DECI
end

#decrement(amount = 1.0) ⇒ Object



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

def decrement(amount = 1.0)
  self + amount
end

#degrees_to_radians ⇒ Object Also known as: degree_to_radians



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

def degrees_to_radians
  self * Math::PI / 180.0
end

#distance(num) ⇒ Object



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

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

#divide(num) ⇒ Object



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

def divide(num)
  self / num
end

#exabytes_in_bytes ⇒ Object Also known as: exabyte_in_bytes



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

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inches ⇒ Object Also known as: foot_in_inches



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

def feet_in_inches
  self * FEET
end

#fraction ⇒ Object



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

def fraction
  (self - truncate).abs
end

#fraction? ⇒ Boolean

Returns:

  • (Boolean)


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

def fraction?
  fraction != 0.0
end

#gigabytes_in_bytes ⇒ Object Also known as: gigabyte_in_bytes



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

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_grams ⇒ Object Also known as: gram_in_grams



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

def grams_in_grams
  self
end

#greater_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than?(num)
  num < self
end

#greater_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/active_object/numeric.rb', line 190

def greater_than_or_equal_to?(num)
  num <= self
end

#hectograms_in_grams ⇒ Object Also known as: hectogram_in_grams



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

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_meters ⇒ Object Also known as: hectometer_in_meters



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

def hectometers_in_meters
  self * HECTO
end

#hours_in_seconds ⇒ Object Also known as: hour_in_seconds



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

def hours_in_seconds
  self * HOUR
end

#inches_in_inches ⇒ Object Also known as: inch_in_inches



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

def inches_in_inches
  self
end

#increment(amount = 1.0) ⇒ Object



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

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/active_object/numeric.rb', line 222

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

#kilobytes_in_bytes ⇒ Object Also known as: kilobyte_in_bytes



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

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_grams ⇒ Object Also known as: kilogram_in_grams



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

def kilograms_in_grams
  self * KILO
end

#kilometers_in_meters ⇒ Object Also known as: kilometer_in_meters



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

def kilometers_in_meters
  self * KILO
end

#less_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than?(num)
  num > self
end

#less_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/active_object/numeric.rb', line 248

def less_than_or_equal_to?(num)
  num >= self
end

#megabytes_in_bytes ⇒ Object Also known as: megabyte_in_bytes



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

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_meters ⇒ Object Also known as: meter_in_meters



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

def meters_in_meters
  self
end

#metric_tons_in_grams ⇒ Object Also known as: metric_ton_in_grams



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

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inches ⇒ Object Also known as: mile_in_inches



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

def miles_in_inches
  self * MILE
end

#millenniums_in_seconds ⇒ Object Also known as: millennium_in_seconds



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

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_grams ⇒ Object Also known as: milligram_in_grams



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

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_meters ⇒ Object Also known as: millimeter_in_meters



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

def millimeters_in_meters
  self * MILLI
end

#minutes_in_seconds ⇒ Object Also known as: minute_in_seconds



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

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#multiply(num) ⇒ Object



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

def multiply(num)
  self * num
end

#nautical_miles_in_inches ⇒ Object Also known as: nautical_mile_in_inches



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

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative? ⇒ Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


315
316
317
# File 'lib/active_object/numeric.rb', line 315

def negative?
  0 > self
end

#ordinal ⇒ Object

rubocop:enable Style/NumericPredicate, Style/YodaCondition



320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/active_object/numeric.rb', line 320

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

#ordinalize ⇒ Object



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

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

#ounces_in_ounces ⇒ Object Also known as: ounce_in_ounces



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

def ounces_in_ounces
  self
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pad(options = {}) ⇒ Object



347
348
349
350
351
352
# File 'lib/active_object/numeric.rb', line 347

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



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/active_object/numeric.rb', line 356

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

#percentage_of(number) ⇒ Object

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



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

def percentage_of(number)
  return 0 if zero? || number.zero?
  (to_f / number.to_f) * 100.0
end

#petabytes_in_bytes ⇒ Object Also known as: petabyte_in_bytes



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

def petabytes_in_bytes
  self * PETABYTE
end

#positive? ⇒ Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


387
388
389
# File 'lib/active_object/numeric.rb', line 387

def positive?
  0 < self
end

#pounds_in_ounces ⇒ Object Also known as: pound_in_ounces

rubocop:enable Style/NumericPredicate, Style/YodaCondition



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

def pounds_in_ounces
  self * POUND
end

#power(num) ⇒ Object



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

def power(num)
  self**num
end

#root(num) ⇒ Object



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

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

#seconds_in_seconds ⇒ Object Also known as: second_in_seconds



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

def seconds_in_seconds
  self
end

#stones_in_ounces ⇒ Object Also known as: stone_in_ounces



412
413
414
# File 'lib/active_object/numeric.rb', line 412

def stones_in_ounces
  self * STONE
end

#subtract(num) ⇒ Object



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

def subtract(num)
  self - num
end

#terabytes_in_bytes ⇒ Object Also known as: terabyte_in_bytes



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

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



428
429
430
431
# File 'lib/active_object/numeric.rb', line 428

def to_byte(from, to)
  assert_inclusion_of_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



433
434
435
436
437
# File 'lib/active_object/numeric.rb', line 433

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



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/active_object/numeric.rb', line 440

def to_length(from, to)
  assert_inclusion_of_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



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/active_object/numeric.rb', line 465

def to_mass(from, to)
  assert_inclusion_of_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



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/active_object/numeric.rb', line 490

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



506
507
508
509
510
# File 'lib/active_object/numeric.rb', line 506

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



513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/active_object/numeric.rb', line 513

def to_temperature(from, to)
  assert_inclusion_of_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



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

def to_time(from, to)
  assert_inclusion_of_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_ounces ⇒ Object Also known as: ton_in_ounces



533
534
535
# File 'lib/active_object/numeric.rb', line 533

def tons_in_ounces
  self * TON
end

#weeks_in_seconds ⇒ Object Also known as: week_in_seconds



539
540
541
# File 'lib/active_object/numeric.rb', line 539

def weeks_in_seconds
  self * WEEK
end

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

Returns:

  • (Boolean)


545
546
547
548
549
550
551
552
# File 'lib/active_object/numeric.rb', line 545

def within?(number, epsilon = 0.01)
  return number == self 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_inches ⇒ Object Also known as: yard_in_inches



554
555
556
# File 'lib/active_object/numeric.rb', line 554

def yards_in_inches
  self * YARD
end

#years_in_seconds ⇒ Object Also known as: year_in_seconds



560
561
562
# File 'lib/active_object/numeric.rb', line 560

def years_in_seconds
  self * YEAR
end