Module: Flightdeck::Duration

Defined in:
app/models/flightdeck/duration.rb

Overview

The one way a number of seconds is written anywhere in Flightdeck — list columns, overview tiles and chart axes all speak through this, so "how long" can never be spelled three subtly different ways.

Class Method Summary collapse

Class Method Details

.humanize(seconds) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'app/models/flightdeck/duration.rb', line 10

def humanize(seconds)
  value = seconds.to_f.abs
  case value
  when 0...1 then "#{(value * 1000).round}ms"
  when 1...60 then "#{trim(value.round(1))}s"
  when 60...3600 then "#{(value / 60).round}m"
  when 3600...86_400 then "#{trim((value / 3600).round(1))}h"
  else "#{trim((value / 86_400).round(1))}d"
  end
end

.trim(number) ⇒ Object



21
# File 'app/models/flightdeck/duration.rb', line 21

def trim(number) = number.to_s.sub(/\.0\z/, "")