Class: Timer

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

Overview

Returns string with humanized time interval.

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.



29
30
31
# File 'lib/renamr/utils.rb', line 29

def initialize
  @sta = Time.now
end

Instance Method Details

#humanize(sec) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/renamr/utils.rb', line 37

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

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

    sec, n = sec.divmod(cnt)
    "#{n.to_i} #{n.to_i != 1 ? nms : nm1}"
  end.compact.reverse.join(' ')
end

#readObject



33
34
35
# File 'lib/renamr/utils.rb', line 33

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