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.



4
5
6
7
8
# File 'lib/infobar/timer.rb', line 4

def initialize
  @n    = 0
  @x    = 0.0
  @fifo = Infobar::FIFO.new(30)
end

Instance Attribute Details

#fifoObject (readonly)

Returns the value of attribute fifo.



14
15
16
# File 'lib/infobar/timer.rb', line 14

def fifo
  @fifo
end

#nObject (readonly)

Returns the value of attribute n.



12
13
14
# File 'lib/infobar/timer.rb', line 12

def n
  @n
end

#xObject (readonly)

Returns the value of attribute x.



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

def x
  @x
end

Instance Method Details

#add(time, count) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/infobar/timer.rb', line 16

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



41
42
43
# File 'lib/infobar/timer.rb', line 41

def average_time
  @x
end

#rateObject



33
34
35
36
37
38
39
# File 'lib/infobar/timer.rb', line 33

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