Class: Pokerstats::StatAggregator

Inherits:
Object
  • Object
show all
Defined in:
lib/pokerstats/stat_aggregator.rb

Instance Method Summary collapse

Constructor Details

#initialize(specification = {}) ⇒ StatAggregator

Returns a new instance of StatAggregator.



4
5
6
7
8
9
10
# File 'lib/pokerstats/stat_aggregator.rb', line 4

def initialize(specification = {})
  @specification = specification
  @data = {}
  @specification.keys.each do |key|
    @data[key] = StatAggregationCount.new(0, 0)
  end
end

Instance Method Details

#apply(hash) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pokerstats/stat_aggregator.rb', line 16

def apply hash
  @specification.each do |key, value|
    # puts "applying specification #{key.inspect} with value specification #{value.inspect}"
    # determine datum value from specification
    datum = case value
    when Symbol
      hash[value]
    when Proc
      value.call(hash)
    else
      raise RuntimeError, "there is no valid specification for datum #{key.inspect}"
    end
    
    # puts "... resulting in datum #{datum.inspect}"
    # apply datum value to aggregated data
    unless datum.nil?
      @data[key].items+=1
      if datum.kind_of? Numeric
        @data[key].total+=datum
      else
        @data[key].total+=1 if datum
      end
    end
  end
end

#dataObject



12
13
14
# File 'lib/pokerstats/stat_aggregator.rb', line 12

def data
  @data
end