Class: Basher::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



5
6
7
# File 'lib/basher/timer.rb', line 5

def initialize
  reset
end

Instance Attribute Details

#started_atObject (readonly)

Returns the value of attribute started_at.



3
4
5
# File 'lib/basher/timer.rb', line 3

def started_at
  @started_at
end

#stopped_atObject (readonly)

Returns the value of attribute stopped_at.



3
4
5
# File 'lib/basher/timer.rb', line 3

def stopped_at
  @stopped_at
end

Instance Method Details

#advance(milliseconds) ⇒ Object



50
51
52
# File 'lib/basher/timer.rb', line 50

def advance(milliseconds)
  @total_elapsed += milliseconds
end

#elapsedObject

Milliseconds



10
11
12
13
# File 'lib/basher/timer.rb', line 10

def elapsed
  return @total_elapsed unless running?
  ((looking_at - started_at) * 1000).ceil
end

#resetObject



44
45
46
47
48
# File 'lib/basher/timer.rb', line 44

def reset
  @stopped_at = nil
  @started_at = nil
  @total_elapsed = 0
end

#startObject



34
35
36
37
# File 'lib/basher/timer.rb', line 34

def start
  @stopped_at = nil
  @started_at = now
end

#stopObject



39
40
41
42
# File 'lib/basher/timer.rb', line 39

def stop
  @total_elapsed += elapsed
  @stopped_at = now
end

#total_elapsedObject



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

def total_elapsed
  return @total_elapsed unless running?
  @total_elapsed + elapsed
end

#total_elapsed_humanizedObject



24
25
26
27
28
29
30
31
32
# 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_secondsObject



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

def total_elapsed_in_seconds
  (total_elapsed.to_f / 1000).round
end