Class: Basher::Timer
- Inherits:
-
Object
- Object
- Basher::Timer
- Defined in:
- lib/basher/timer.rb
Instance Attribute Summary collapse
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#stopped_at ⇒ Object
readonly
Returns the value of attribute stopped_at.
Instance Method Summary collapse
- #advance(milliseconds) ⇒ Object
-
#elapsed ⇒ Object
Milliseconds.
-
#initialize ⇒ Timer
constructor
A new instance of Timer.
- #reset ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #total_elapsed ⇒ Object
- #total_elapsed_humanized ⇒ Object
- #total_elapsed_in_seconds ⇒ Object
Constructor Details
#initialize ⇒ Timer
Returns a new instance of Timer.
5 6 7 |
# File 'lib/basher/timer.rb', line 5 def initialize reset end |
Instance Attribute Details
#started_at ⇒ Object (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_at ⇒ Object (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 |
#elapsed ⇒ Object
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 |
#reset ⇒ Object
44 45 46 47 48 |
# File 'lib/basher/timer.rb', line 44 def reset @stopped_at = nil @started_at = nil @total_elapsed = 0 end |
#start ⇒ Object
34 35 36 37 |
# File 'lib/basher/timer.rb', line 34 def start @stopped_at = nil @started_at = now end |
#stop ⇒ Object
39 40 41 42 |
# File 'lib/basher/timer.rb', line 39 def stop @total_elapsed += elapsed @stopped_at = now end |
#total_elapsed ⇒ Object
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_humanized ⇒ Object
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_seconds ⇒ Object
20 21 22 |
# File 'lib/basher/timer.rb', line 20 def total_elapsed_in_seconds (total_elapsed.to_f / 1000).round end |