Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/buildpack_support/format_duration.rb

Overview

A mixin that adds the ability to format a Numeric as a user-readable duration

Instance Method Summary collapse

Instance Method Details

#durationString

Formats a number as a user-readable duration

Returns:

  • (String)

    a user-readable duration. Follows the following algorithm

    1. If more than an hour, print hours and minutes

    2. If less than an hour and more than a minute, print minutes and seconds

    3. If less than a minute and more than a second, print seconds.tenths



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/buildpack_support/format_duration.rb', line 25

def duration
  remainder = self

  hours     = (remainder / HOUR).to_int
  remainder -= HOUR * hours

  minutes   = (remainder / MINUTE).to_int
  remainder -= MINUTE * minutes

  return "#{hours}h #{minutes}m" if hours > 0

  seconds   = (remainder / SECOND).to_int
  remainder -= SECOND * seconds

  return "#{minutes}m #{seconds}s" if minutes > 0

  tenths = (remainder / TENTH).to_int
  "#{seconds}.#{tenths}s"
end