Class: Proforma::Compiling::Counter
- Inherits:
-
Object
- Object
- Proforma::Compiling::Counter
- Defined in:
- lib/proforma/compiling/counter.rb
Overview
A counter is the actual underlying data structure for aggregation.
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#sum ⇒ Object
readonly
Returns the value of attribute sum.
Instance Method Summary collapse
- #add(value) ⇒ Object
- #ave ⇒ Object
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
Constructor Details
#initialize ⇒ Counter
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
#count ⇒ Object (readonly)
Returns the value of attribute count.
14 15 16 |
# File 'lib/proforma/compiling/counter.rb', line 14 def count @count end |
#sum ⇒ Object (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 |
#ave ⇒ Object
30 31 32 33 34 |
# File 'lib/proforma/compiling/counter.rb', line 30 def ave return BigDecimal(0) if count.zero? sum / count end |