Class: Comrade::Timer
- Inherits:
-
Object
- Object
- Comrade::Timer
- Defined in:
- lib/comrade/timer.rb
Instance Method Summary collapse
-
#elapsed? ⇒ Boolean
(also: #finished?)
Return if the time has elapsed.
-
#elapsed_percentage ⇒ Object
Elapsed time percentage Starts at 0 and goes up to 100 [return] Float.
-
#elapsed_ratio ⇒ Object
Elapsed time ratio Starts at 0.0 and goes up to 1.0 [return] Float.
-
#initialize(string) ⇒ Timer
constructor
Initializes the variables started_at and stops_at [param] time_period_string string to be parsed by treetop.
-
#loop? ⇒ Boolean
Timer should loop.
-
#remaining_percentage ⇒ Object
Remaining time percentage Starts at 100 and goes down to 0 [return] Float.
-
#remaining_ratio ⇒ Object
Remaining time ratio Starts at 1.0 and goes down to 0.0 [return] Float.
Constructor Details
#initialize(string) ⇒ Timer
Initializes the variables started_at and stops_at
- param
-
time_period_string string to be parsed by treetop
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/comrade/timer.rb', line 19 def initialize string @started_at = Time.now @parsed = parse string if @parsed. date = DateTime.parse "#{@parsed.input}#{@started_at.zone}" @stops_at = date > @started_at ? date : date + 1 else @stops_at = @started_at + @parsed.to_seconds end end |
Instance Method Details
#elapsed? ⇒ Boolean Also known as: finished?
Return if the time has elapsed
55 |
# File 'lib/comrade/timer.rb', line 55 def elapsed?; elapsed_ratio >= 1.0 end |
#elapsed_percentage ⇒ Object
Elapsed time percentage Starts at 0 and goes up to 100
- return
-
Float
42 |
# File 'lib/comrade/timer.rb', line 42 def elapsed_percentage; elapsed_ratio * 100 end |
#elapsed_ratio ⇒ Object
Elapsed time ratio Starts at 0.0 and goes up to 1.0
- return
-
Float
34 35 36 37 |
# File 'lib/comrade/timer.rb', line 34 def elapsed_ratio ratio = elapsed_seconds.to_f / total_seconds.to_f ratio > 1.0 ? 1.0 : ratio end |
#loop? ⇒ Boolean
Timer should loop
59 60 61 |
# File 'lib/comrade/timer.rb', line 59 def loop? @parsed.loop? end |
#remaining_percentage ⇒ Object
Remaining time percentage Starts at 100 and goes down to 0
- return
-
Float
52 |
# File 'lib/comrade/timer.rb', line 52 def remaining_percentage; remaining_ratio * 100 end |
#remaining_ratio ⇒ Object
Remaining time ratio Starts at 1.0 and goes down to 0.0
- return
-
Float
47 |
# File 'lib/comrade/timer.rb', line 47 def remaining_ratio;1 - elapsed_ratio end |