Module: ActiveObject::Numeric

Defined in:
lib/active_object/numeric.rb

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
99
100
# 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 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



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

def days_in_seconds
  self * DAY
end

#decades_in_secondsObject Also known as: decade_in_seconds



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

def decades_in_seconds
  self * DECADE
end

#decagrams_in_gramsObject Also known as: decagram_in_grams



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

def decagrams_in_grams
  self * DECA
end

#decameters_in_metersObject Also known as: decameter_in_meters



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

def decameters_in_meters
  self * DECA
end

#decigrams_in_gramsObject Also known as: decigram_in_grams



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

def decigrams_in_grams
  self * DECI
end

#decimeters_in_metersObject Also known as: decimeter_in_meters



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

def decimeters_in_meters
  self * DECI
end

#decrement(amount = 1.0) ⇒ Object



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

def decrement(amount = 1.0)
  self + amount
end

#degrees_to_radiansObject Also known as: degree_to_radians



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

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

#distance(num) ⇒ Object



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

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

#divide(num) ⇒ Object



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

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

  self / num
end

#exabytes_in_bytesObject Also known as: exabyte_in_bytes



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

def exabytes_in_bytes
  self * EXABYTE
end

#feet_in_inchesObject Also known as: foot_in_inches



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

def feet_in_inches
  self * FEET
end

#fractionObject



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

def fraction
  (self - truncate).abs
end

#fraction?Boolean

Returns:

  • (Boolean)


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

def fraction?
  fraction != 0.0
end

#gigabytes_in_bytesObject Also known as: gigabyte_in_bytes



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

def gigabytes_in_bytes
  self * GIGABYTE
end

#grams_in_gramsObject Also known as: gram_in_grams



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

def grams_in_grams
  self
end

#greater_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than?(num)
  num < self
end

#greater_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def greater_than_or_equal_to?(num)
  num <= self
end

#hectograms_in_gramsObject Also known as: hectogram_in_grams



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

def hectograms_in_grams
  self * HECTO
end

#hectometers_in_metersObject Also known as: hectometer_in_meters



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

def hectometers_in_meters
  self * HECTO
end

#hours_in_secondsObject Also known as: hour_in_seconds



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

def hours_in_seconds
  self * HOUR
end

#inches_in_inchesObject Also known as: inch_in_inches



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

def inches_in_inches
  self
end

#increment(amount = 1.0) ⇒ Object



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

def increment(amount = 1.0)
  self + amount
end

#inside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#kilobytes_in_bytesObject Also known as: kilobyte_in_bytes



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

def kilobytes_in_bytes
  self * KILOBYTE
end

#kilograms_in_gramsObject Also known as: kilogram_in_grams



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

def kilograms_in_grams
  self * KILO
end

#kilometers_in_metersObject Also known as: kilometer_in_meters



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

def kilometers_in_meters
  self * KILO
end

#less_than?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than?(num)
  num > self
end

#less_than_or_equal_to?(num) ⇒ Boolean

Returns:

  • (Boolean)


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

def less_than_or_equal_to?(num)
  num >= self
end

#megabytes_in_bytesObject Also known as: megabyte_in_bytes



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

def megabytes_in_bytes
  self * MEGABYTE
end

#meters_in_metersObject Also known as: meter_in_meters



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

def meters_in_meters
  self
end

#metric_tons_in_gramsObject Also known as: metric_ton_in_grams



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

def metric_tons_in_grams
  self * METRIC_TON
end

#miles_in_inchesObject Also known as: mile_in_inches



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

def miles_in_inches
  self * MILE
end

#millenniums_in_secondsObject Also known as: millennium_in_seconds



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

def millenniums_in_seconds
  self * MILLENNIUM
end

#milligrams_in_gramsObject Also known as: milligram_in_grams



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

def milligrams_in_grams
  self * MILLI
end

#millimeters_in_metersObject Also known as: millimeter_in_meters



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

def millimeters_in_meters
  self * MILLI
end

#minutes_in_secondsObject Also known as: minute_in_seconds



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

def minutes_in_seconds
  self * MINUTE
end

#multiple_of?(number) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#multiply(num) ⇒ Object



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

def multiply(num)
  self * num
end

#nautical_miles_in_inchesObject Also known as: nautical_mile_in_inches



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

def nautical_miles_in_inches
  self * NAUTICAL_MILE
end

#negative?Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


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

def negative?
  0 > self
end

#ordinalObject

rubocop:enable Style/NumericPredicate, Style/YodaCondition



325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/active_object/numeric.rb', line 325

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



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

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

#ounces_in_ouncesObject Also known as: ounce_in_ounces



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

def ounces_in_ounces
  self
end

#outside?(start, finish) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#pad(options = {}) ⇒ Object



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

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



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/active_object/numeric.rb', line 361

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



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

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



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

def petabytes_in_bytes
  self * PETABYTE
end

#positive?Boolean

rubocop:disable Style/NumericPredicate, Style/YodaCondition

Returns:

  • (Boolean)


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

def positive?
  0 < self
end

#pounds_in_ouncesObject Also known as: pound_in_ounces

rubocop:enable Style/NumericPredicate, Style/YodaCondition



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

def pounds_in_ounces
  self * POUND
end

#power(num) ⇒ Object



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

def power(num)
  self**num
end

#root(num) ⇒ Object



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

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

#seconds_in_secondsObject Also known as: second_in_seconds



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

def seconds_in_seconds
  self
end

#stones_in_ouncesObject Also known as: stone_in_ounces



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

def stones_in_ounces
  self * STONE
end

#subtract(num) ⇒ Object



424
425
426
# File 'lib/active_object/numeric.rb', line 424

def subtract(num)
  self - num
end

#terabytes_in_bytesObject Also known as: terabyte_in_bytes



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

def terabytes_in_bytes
  self * TERABYTE
end

#to_byte(from, to) ⇒ Object



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

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



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

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



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

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



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/active_object/numeric.rb', line 473

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



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

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



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

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



522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/active_object/numeric.rb', line 522

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



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

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



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

def tons_in_ounces
  self * TON
end

#weeks_in_secondsObject Also known as: week_in_seconds



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

def weeks_in_seconds
  self * WEEK
end

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

Returns:

  • (Boolean)


554
555
556
557
558
559
560
561
# File 'lib/active_object/numeric.rb', line 554

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_inchesObject Also known as: yard_in_inches



563
564
565
# File 'lib/active_object/numeric.rb', line 563

def yards_in_inches
  self * YARD
end

#years_in_secondsObject Also known as: year_in_seconds



569
570
571
# File 'lib/active_object/numeric.rb', line 569

def years_in_seconds
  self * YEAR
end