Class: Infobar::Timer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTimer

Returns a new instance of Timer.



2
3
4
5
# File 'lib/infobar/timer.rb', line 2

def initialize
  @n    = 0
  @x    = 0.0
end

Instance Attribute Details

#nObject (readonly)

Returns the value of attribute n.



9
10
11
# File 'lib/infobar/timer.rb', line 9

def n
  @n
end

#xObject (readonly)

Returns the value of attribute x.



7
8
9
# File 'lib/infobar/timer.rb', line 7

def x
  @x
end

Instance Method Details

#add(time, count) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/infobar/timer.rb', line 11

def add(time, count)
  case @n
  when 0
    @n += 1
  when 1
    @n -= 1
    duration = time - @time_last
    self << (duration / @count_last)
    self << (duration / count.to_f)
  else
    duration = time - @time_last
    self << (duration / count.to_f)
  end
  @time_last, @count_last = time, count
  self
end

#average_timeObject



36
37
38
# File 'lib/infobar/timer.rb', line 36

def average_time
  @x
end

#rateObject



28
29
30
31
32
33
34
# File 'lib/infobar/timer.rb', line 28

def rate
  if @x.zero?
    0.0
  else
    1.0 / @x
  end
end