Class: Tailstrom::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/tailstrom/counter.rb

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



3
4
5
# File 'lib/tailstrom/counter.rb', line 3

def initialize
  clear
end

Instance Method Details

#<<(value) ⇒ Object



7
8
9
10
# File 'lib/tailstrom/counter.rb', line 7

def <<(value)
  purge_cache
  @values << value
end

#avgObject



21
22
23
24
# File 'lib/tailstrom/counter.rb', line 21

def avg
  return nil if @values.empty?
  sum / @values.length
end

#clearObject



12
13
14
15
# File 'lib/tailstrom/counter.rb', line 12

def clear
  @values = []
  purge_cache
end

#countObject



42
43
44
# File 'lib/tailstrom/counter.rb', line 42

def count
  @cache[:count] ||= @values.count
end

#maxObject



34
35
36
# File 'lib/tailstrom/counter.rb', line 34

def max
  @cache[:max] ||= @values.max
end

#medObject



38
39
40
# File 'lib/tailstrom/counter.rb', line 38

def med
  @values[@values.length / 2]
end

#minObject



30
31
32
# File 'lib/tailstrom/counter.rb', line 30

def min
  @cache[:min] ||= @values.min
end

#purge_cacheObject



17
18
19
# File 'lib/tailstrom/counter.rb', line 17

def purge_cache
  @cache = {}
end

#sumObject



26
27
28
# File 'lib/tailstrom/counter.rb', line 26

def sum
  @cache[:sum] ||= @values.inject(0, :+)
end