Class: Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/renamr/timer.rb

Overview

Measures elapsed time and reports it in a human-readable form.

Constant Summary collapse

DIC =
[
  [60,   :seconds, :second],
  [60,   :minutes, :minute],
  [24,   :hours,   :hour],
  [1000, :days,    :day]
].freeze

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



16
17
18
# File 'lib/renamr/timer.rb', line 16

def initialize
  @sta = Time.now
end

Instance Method Details

#humanize(sec) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/renamr/timer.rb', line 24

def humanize(sec)
  return 'less than a second' if sec < 1

  DIC.filter_map do |cnt, nms, nm1|
    next if sec <= 0

    sec, n = sec.divmod(cnt)
    next if n.to_i.zero?

    "#{n.to_i} #{n.to_i == 1 ? nm1 : nms}"
  end.reverse.join(' ')
end

#readObject



20
21
22
# File 'lib/renamr/timer.rb', line 20

def read
  humanize(Time.now - @sta)
end