Class: Comrade::Timer

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

Instance Method Summary collapse

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.timestamp?
    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

Returns:

  • (Boolean)


55
# File 'lib/comrade/timer.rb', line 55

def elapsed?; elapsed_ratio >= 1.0 end

#elapsed_percentageObject

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_ratioObject

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

Returns:

  • (Boolean)


59
60
61
# File 'lib/comrade/timer.rb', line 59

def loop?
  @parsed.loop?
end

#remaining_percentageObject

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_ratioObject

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