class Comrade::Timer

Public Class Methods

new(string) click to toggle source

Initializes the variables started_at and stops_at

param

time_period_string string to be parsed by treetop

# File lib/comrade/timer.rb, line 18
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

Public Instance Methods

elapsed?() click to toggle source

Return if the time has elapsed

# File lib/comrade/timer.rb, line 54
def elapsed?; elapsed_ratio >= 1.0 end
Also aliased as: finished?
elapsed_percentage() click to toggle source

Elapsed time percentage Starts at 0 and goes up to 100

return

Float

# File lib/comrade/timer.rb, line 41
def elapsed_percentage; elapsed_ratio * 100 end
elapsed_ratio() click to toggle source

Elapsed time ratio Starts at 0.0 and goes up to 1.0

return

Float

# File lib/comrade/timer.rb, line 33
def elapsed_ratio
  ratio = elapsed_seconds.to_f / total_seconds.to_f
  ratio > 1.0 ? 1.0 : ratio
end
finished?()
Alias for: elapsed?
loop?() click to toggle source

Timer should loop

# File lib/comrade/timer.rb, line 58
def loop?
  @parsed.loop?
end
remaining_percentage() click to toggle source

Remaining time percentage Starts at 100 and goes down to 0

return

Float

# File lib/comrade/timer.rb, line 51
def remaining_percentage; remaining_ratio * 100 end
remaining_ratio() click to toggle source

Remaining time ratio Starts at 1.0 and goes down to 0.0

return

Float

# File lib/comrade/timer.rb, line 46
def remaining_ratio;1 - elapsed_ratio end

Private Instance Methods

elapsed_seconds() click to toggle source

Elapsed seconds since timer started

return

Fixnum

# File lib/comrade/timer.rb, line 80
def elapsed_seconds
  Time.now.unix_time - @started_at.unix_time
end
parse(string) click to toggle source

Parses the input using the PeriodParser raises ‘Parsing Error’ if parsing unsuccessful

# File lib/comrade/timer.rb, line 66
def parse string
  parsed = PeriodParser.new.parse string
  raise 'Parsing Error' if parsed.nil?
  parsed
end
total_seconds() click to toggle source

Seconds between timer start and expected end

return

Fixnum

# File lib/comrade/timer.rb, line 74
def total_seconds
  @stops_at.unix_time - @started_at.unix_time
end