Module: Cubicle::Data

Defined in:
lib/cubicle/data.rb,
lib/cubicle/data/level.rb,
lib/cubicle/data/table.rb,
lib/cubicle/data/member.rb,
lib/cubicle/data/hierarchy.rb

Defined Under Namespace

Modules: Member Classes: Hierarchy, Level, Table

Class Method Summary collapse

Class Method Details

.aggregate(data, measures) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cubicle/data.rb', line 4

def self.aggregate(data,measures)
  aggregated = OrderedHashWithIndifferentAccess.new {|hash,key|hash[key]=[]}
  #in step one, we will gather our values into columns to give to the measure
  #definitions to aggregation.
  data.each do |row|
    measures.each do |measure|
      if (row.include?(measure.name))
        val = row[measure.name]
        (aggregated[measure.name] ||= []) << val if val.kind_of?(Numeric)
      end
    end
  end
  #in step two, we will let the measures reduce the columns of values to a single number, preferably using
  #black magic or human sacrifice
  measures.each do |measure|
    aggregated[measure.name] = measure.aggregate(aggregated[measure.name])
  end

  #give each measure a final shot to operate on the results. This is useful for measures that
  #act on the results of other aggregations, like Ratio does.
  measures.each {|measure|measure.finalize_aggregation(aggregated)}
  aggregated
end