Module: ActiveObject::Numeric

Defined in:
lib/active_object/numeric.rb

Instance Method Summary collapse

Instance Method Details

#add(num) ⇒ Object



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

def add(num)
  self + num
end

#bytes_in_bytesObject Also known as: byte_in_bytes



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

def bytes_in_bytes
  self
end

#centigrams_in_gramsObject Also known as: centigram_in_grams



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

def centigrams_in_grams
  self * CENTI
end

#centimeters_in_metersObject Also known as: centimeter_in_meters



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

def centimeters_in_meters
  self * CENTI
end

#centuries_in_secondsObject Also known as: century_in_seconds



86
87
88
# File 'lib/active_object/numeric.rb', line 86

def centuries_in_seconds
  self * CENTURY
end

#clamp(minimum, maximum = nil) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/active_object/numeric.rb', line 93

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_secondsObject Also known as: day_in_seconds

rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



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

def days_in_seconds
  self * DAY
end

#decades_in_secondsObject Also known as: decade_in_seconds



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

def decades_in_seconds
  self * DECADE
end

#decagrams_in_gramsObject Also known as: decagram_in_grams



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

def decagrams_in_grams
  self * DECA
end

#decameters_in_metersObject Also known as: decameter_in_meters



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

def decameters_in_meters
  self * DECA
end

#decigrams_in_gramsObject Also known as: decigram_in_grams



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

def decigrams_in_grams
  self * DECI
end

#decimeters_in_metersObject Also known as: decimeter_in_meters



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

def decimeters_in_meters
  self * DECI
end

#decrement(amount = 1.0) ⇒ Object



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

def decrement(amount = 1.0)
  self + amount
end

#degrees_to_radiansObject Also known as: degree_to_radians



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

def degrees_to_radians
  self * ::Math::PI / 180.0
end

#distance(num) ⇒ Object



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

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

#divide(num) ⇒ Object



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

def divide(num)
  return 0 if num.zero?

  self / num
end

#exabytes_in_bytesObject Also known as: exabyte_in_bytes



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

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inchesObject Also known as: foot_in_inches



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

def feet_in_inches
  self * FEET
end

#fractionObject



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

def fraction
  (self - truncate).abs
end

#fraction?Boolean

Returns:

  • (Boolean)


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

def fraction?
  fraction != 0.0
end

#gigabytes_in_bytesObject Also known as: gigabyte_in_bytes



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

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_gramsObject Also known as: gram_in_grams



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

def grams_in_grams
  self
end

#greater_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/active_object/numeric.rb', line 197

def greater_than?(num)
  num < self
end

#greater_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than_or_equal_to?(num)
  num <= self
end

#hectograms_in_gramsObject Also known as: hectogram_in_grams



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

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_metersObject Also known as: hectometer_in_meters



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

def hectometers_in_meters
  self * HECTO
end

#hours_in_secondsObject Also known as: hour_in_seconds



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

def hours_in_seconds
  self * HOUR
end

#inches_in_inchesObject Also known as: inch_in_inches



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

def inches_in_inches
  self
end

#increment(amount = 1.0) ⇒ Object



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

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#kilobytes_in_bytesObject Also known as: kilobyte_in_bytes



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

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_gramsObject Also known as: kilogram_in_grams



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

def kilograms_in_grams
  self * KILO
end

#kilometers_in_metersObject Also known as: kilometer_in_meters



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

def kilometers_in_meters
  self * KILO
end

#less_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/active_object/numeric.rb', line 255

def less_than?(num)
  num > self
end

#less_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than_or_equal_to?(num)
  num >= self
end

#megabytes_in_bytesObject Also known as: megabyte_in_bytes



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

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_metersObject Also known as: meter_in_meters



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

def meters_in_meters
  self
end

#metric_tons_in_gramsObject Also known as: metric_ton_in_grams



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

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inchesObject Also known as: mile_in_inches



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

def miles_in_inches
  self * MILE
end

#millenniums_in_secondsObject Also known as: millennium_in_seconds



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

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_gramsObject Also known as: milligram_in_grams



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

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_metersObject Also known as: millimeter_in_meters



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

def millimeters_in_meters
  self * MILLI
end

#minutes_in_secondsObject Also known as: minute_in_seconds



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

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

def multiple_of?(number)
  return zero? if number.zero?

  modulo(number).zero?
end

#multiply(num) ⇒ Object



311
312
313
# File 'lib/active_object/numeric.rb', line 311

def multiply(num)
  self * num
end

#nautical_miles_in_inchesObject Also known as: nautical_mile_in_inches



321
322
323
# File 'lib/active_object/numeric.rb', line 321

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative?Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


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

def negative?
  0 > self
end

#ordinalObject

rubocop:enable Style/NumericPredicate, Style/YodaCondition



333
334
335
336
337
338
339
340
341
342
# File 'lib/active_object/numeric.rb', line 333

def ordinal
  return 'th' if (11..13).cover?(abs % 100)

  case abs % 10
  when 1 then 'st'
  when 2 then 'nd'
  when 3 then 'rd'
  else 'th'
  end
end

#ordinalizeObject



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

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

#ounces_in_ouncesObject Also known as: ounce_in_ounces



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

def ounces_in_ounces
  self
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


354
355
356
# File 'lib/active_object/numeric.rb', line 354

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

#pad(options = {}) ⇒ Object



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

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, Metrics/LineLength, Metrics/MethodLength, Metrics/PerceivedComplexity



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/active_object/numeric.rb', line 366

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, Metrics/LineLength, Metrics/MethodLength, Metrics/PerceivedComplexity



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

def percentage_of(number)
  return 0 if zero? || number.zero?

  (to_f / number.to_f) * 100.0
end

#petabytes_in_bytesObject Also known as: petabyte_in_bytes



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

def petabytes_in_bytes
  self * PETABYTE
end

#positive?Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


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

def positive?
  0 < self
end

#pounds_in_ouncesObject Also known as: pound_in_ounces

rubocop:enable Style/NumericPredicate, Style/YodaCondition



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

def pounds_in_ounces
  self * POUND
end

#power(num) ⇒ Object



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

def power(num)
  self**num
end

#root(num) ⇒ Object



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

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

#seconds_in_secondsObject Also known as: second_in_seconds



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

def seconds_in_seconds
  self
end

#stones_in_ouncesObject Also known as: stone_in_ounces



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

def stones_in_ounces
  self * STONE
end

#subtract(num) ⇒ Object



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

def subtract(num)
  self - num
end

#terabytes_in_bytesObject Also known as: terabyte_in_bytes



432
433
434
# File 'lib/active_object/numeric.rb', line 432

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



438
439
440
441
442
# File 'lib/active_object/numeric.rb', line 438

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



444
445
446
447
448
# File 'lib/active_object/numeric.rb', line 444

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



451
452
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 451

def to_length(from, to)
  assert_inclusion_of_valid_keys!(LENGTH_KEYS.values.flatten, from, to)

  return self if from == to

  metric_keys = LENGTH_KEYS.fetch(:metric)
  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



477
478
479
480
481
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 477

def to_mass(from, to)
  assert_inclusion_of_valid_keys!(MASS_KEYS.values.flatten, from, to)

  return self if from == to

  metric_keys = MASS_KEYS.fetch(:metric)
  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



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/active_object/numeric.rb', line 504

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

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

  values.each do |val|
    next unless (self - val).abs < difference

    difference = (self - val).abs
    value = val
  end

  value
end

#to_percentage(options = {}) ⇒ Object



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

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, Metrics/MethodLength



527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/active_object/numeric.rb', line 527

def to_temperature(from, to)
  assert_inclusion_of_valid_keys!(TEMPERATURE_KEYS, from, to)

  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

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



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

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_ouncesObject Also known as: ton_in_ounces



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

def tons_in_ounces
  self * TON
end

#weeks_in_secondsObject Also known as: week_in_seconds



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

def weeks_in_seconds
  self * WEEK
end

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

Returns:

  • (Boolean)


561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/active_object/numeric.rb', line 561

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

  alpha = to_f
  beta = number.to_f

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

#yards_in_inchesObject Also known as: yard_in_inches



574
575
576
# File 'lib/active_object/numeric.rb', line 574

def yards_in_inches
  self * YARD
end

#years_in_secondsObject Also known as: year_in_seconds



580
581
582
# File 'lib/active_object/numeric.rb', line 580

def years_in_seconds
  self * YEAR
end