Sha256: ff4ba2d67e2e663e74488ac14c1cf1060b10e5a629490d22147223240b562174
Contents?: true
Size: 1.56 KB
Versions: 3
Compression:
Stored size: 1.56 KB
Contents
require 'delegate' module Mongoid module Report class Collection def initialize(context, rows, fields, columns, mapping) @context = context @rows = rows @fields = fields @columns = columns @mapping = mapping @rows = Rows.new(compile_rows) end class Rows < SimpleDelegator ; end attr_reader :context, :rows def headers @fields end def summary @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. next if @columns.has_key?(field) next unless row[field].is_a?(Fixnum) || row[field].is_a?(Float) summary[field] += row[field] end # Apply dynamic columns for summarized row @columns.each do |name, function| next unless @fields.include?(name) summary[name] = function.call(@context, summary, { 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 end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
mongoid-report-0.2.2 | lib/mongoid/report/collection.rb |
mongoid-report-0.2.1 | lib/mongoid/report/collection.rb |
mongoid-report-0.2.0 | lib/mongoid/report/collection.rb |