class Basher::Timer

Attributes

started_at[R]
stopped_at[R]

Public Class Methods

new() click to toggle source
# File lib/basher/timer.rb, line 5
def initialize
  reset
end

Public Instance Methods

advance(milliseconds) click to toggle source
# File lib/basher/timer.rb, line 50
def advance(milliseconds)
  @total_elapsed += milliseconds
end
elapsed() click to toggle source

Milliseconds

# File lib/basher/timer.rb, line 10
def elapsed
  return @total_elapsed unless running?
  ((looking_at - started_at) * 1000).ceil
end
reset() click to toggle source
# File lib/basher/timer.rb, line 44
def reset
  @stopped_at = nil
  @started_at = nil
  @total_elapsed = 0
end
start() click to toggle source
# File lib/basher/timer.rb, line 34
def start
  @stopped_at = nil
  @started_at = now
end
stop() click to toggle source
# File lib/basher/timer.rb, line 39
def stop
  @total_elapsed += elapsed
  @stopped_at = now
end
total_elapsed() click to toggle source
# File lib/basher/timer.rb, line 15
def total_elapsed
  return @total_elapsed unless running?
  @total_elapsed + elapsed
end
total_elapsed_humanized() click to toggle source
# File lib/basher/timer.rb, line 24
def total_elapsed_humanized
  seconds = total_elapsed_in_seconds
  [[60, :seconds], [60, :minutes], [24, :hours]].map do |count, name|
    if seconds > 0
      seconds, n = seconds.divmod(count)
      "#{n.to_i} #{name}"
    end
  end.compact.reverse.join(' ')
end
total_elapsed_in_seconds() click to toggle source
# File lib/basher/timer.rb, line 20
def total_elapsed_in_seconds
  (total_elapsed.to_f / 1000).round
end

Private Instance Methods

looking_at() click to toggle source
# File lib/basher/timer.rb, line 64
def looking_at
  running? ? now : stopped_at
end
now() click to toggle source
# File lib/basher/timer.rb, line 56
def now
  Time.now
end
running?() click to toggle source
# File lib/basher/timer.rb, line 60
def running?
  !started_at.nil? && stopped_at.nil?
end