class Allotment::Stopwatch

Stopwatch Class Strangely enough works just like a stopwatch @!attribute [r] name

@return [String] the current name

@!attribute [r] status

@return [String] the current status (running|stopped)

Attributes

name[R]
status[R]

Public Class Methods

new(name = self.class.uniqe_name) click to toggle source

If the stopwatch is not given a name the uniqe_name class method is called @param name [String] choosen name

# File lib/allotment/stopwatch.rb, line 21
def initialize name = self.class.uniqe_name
  @name    = name
  @status  = 'stopped'
  @sw_time = 0
end

Private Class Methods

uniqe_name() click to toggle source

ensures that if a stopwatch is unnamed it has a uniqe name @return [String] stopwatch uniqe name

# File lib/allotment/stopwatch.rb, line 95
def self.uniqe_name
  "stopwatch_" + (@id ? @id += 1 : @id = 0).to_s
end

Public Instance Methods

inspect() click to toggle source

Overwite the obj inspect

# File lib/allotment/stopwatch.rb, line 14
def inspect
  "#<Stopwatch:%s>" % @status
end
lap() click to toggle source

A lap is the amount of time from the very start or from the end of the last lap Accumilated lap time is retained accross any stopages of the stopwatch @return [Float] the lap time

# File lib/allotment/stopwatch.rb, line 70
def lap
  new_lap  = time - (@lp_start || 0)
  @lp_start = time
  new_lap
end
reset() click to toggle source

sets all times to 0 if the timer is running then the start time is just overwritten if the timer is not then the start time will be over written on start @return [Allotment::Stopwatch] self

# File lib/allotment/stopwatch.rb, line 60
def reset
  @sw_start = Time.now
  @sw_time  = 0
  self
end
split() click to toggle source

A split is the amount of time from the last start of the stopwatch it uses the sw time so it will keep accumelated time accross stoppages @return [Float] the split time

# File lib/allotment/stopwatch.rb, line 80
def split
  @status == 'running' ? Time.now - @last_start : @last_stop - @last_start
end
start() click to toggle source

if the stopwatch was previously stopped then the current time is removed from the current time this means that it is in effect added to the total time @return [Allotment::Stopwatch] self

# File lib/allotment/stopwatch.rb, line 32
def start
  if status == 'stopped'
    @last_start = Time.now
    @sw_start   = Time.now - @sw_time
    @status     = 'running'
  end
  self
end
stop() click to toggle source

sets the current_time this is where the start time could be Time.now - current_time @return [Float] the stopwatch time

# File lib/allotment/stopwatch.rb, line 45
def stop
  if status == 'running'
    @status    = 'stopped'
    @last_stop = Time.now
    @sw_time   = Time.now - @sw_start
  else
    time
  end
end
time() click to toggle source

The accumelated current time on the stopwatch @return [Float] the current time

# File lib/allotment/stopwatch.rb, line 87
def time
  @status == 'running' ? Time.now - @sw_start : @sw_time
end