lib/mongoid/report/collection.rb in mongoid-report-0.1.7 vs lib/mongoid/report/collection.rb in mongoid-report-0.1.9

- old
+ new

@@ -2,34 +2,29 @@ module Mongoid module Report class Collection < SimpleDelegator - def initialize(context, rows, fields, columns) + def initialize(context, rows, fields, columns, mapping) @context = context @rows = rows @fields = fields @columns = columns - @rows = compile_rows - - # Collection should behave like Array using delegator method. - super(@rows) + @mapping = mapping + @rows = Rows.new(compile_rows) end - def compile_rows - @rows.map do |row| - @columns.each do |name, function| - next unless @fields.include?(name) - row[name] = function.call(@context, row, { summary: false }) - end + class Rows < SimpleDelegator ; end - row - end + attr_reader :rows + + def headers + @fields end def summary - @summary ||= reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row| + @summary ||= @rows.reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row| # Find summary for aggregated rows @fields.each do |field| # Don't apply for dynamic calculated columns lets wait until we get # all summaried mongo columns and then apply dynamic columns # calculations. @@ -38,13 +33,26 @@ end # Apply dynamic columns for summarized row @columns.each do |name, function| next unless @fields.include?(name) - summary[name] = function.call(@context, row, { summary: true }) + summary[name] = function.call(@context, row, { mapping: @mapping, summary: true }) end summary + end + end + + private + + def compile_rows + @rows.map do |row| + @columns.each do |name, function| + next unless @fields.include?(name) + row[name] = function.call(@context, row, { mapping: @mapping, summary: false }) + end + + row end end end