Class: Proforma::Compiling::Counter

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

Overview

A counter is the actual underlying data structure for aggregation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



16
17
18
19
# File 'lib/proforma/compiling/counter.rb', line 16

def initialize
  @count = 0
  @sum   = BigDecimal(0)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



14
15
16
# File 'lib/proforma/compiling/counter.rb', line 14

def count
  @count
end

#sumObject (readonly)

Returns the value of attribute sum.



14
15
16
# File 'lib/proforma/compiling/counter.rb', line 14

def sum
  @sum
end

Instance Method Details

#add(value) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/proforma/compiling/counter.rb', line 21

def add(value)
  parsed_value = value.to_s.empty? ? 0 : BigDecimal(value.to_s)

  @count += 1
  @sum   += parsed_value

  self
end

#aveObject



30
31
32
33
34
# File 'lib/proforma/compiling/counter.rb', line 30

def ave
  return BigDecimal(0) if count.zero?

  sum / count
end