Sha256: 511e348d6c68ee43ae6370786311edaad2c8de8f925772f5be4e54a47c7f76dd

Contents?: true

Size: 760 Bytes

Versions: 1

Compression:

Stored size: 760 Bytes

Contents

require 'delegate'

module Mongoid
  module Report

    class Collection < SimpleDelegator
      def initialize(rows, fields, columns)
        @rows    = rows
        @fields  = fields
        @columns = columns
        super(rows)
        compile_dynamic_fields(columns)
      end

      def summary
        @summary ||= reduce(Hash.new{|h, k| h[k] = 0}) do |summary, row|
          @fields.each do |field|
            summary[field] += row[field.to_s]
          end

          summary
        end.with_indifferent_access
      end

      private

      def compile_dynamic_fields(columns)
        self.each do |row|
          @columns.each do |name, function|
            row[name] = function.call(row)
          end
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-report-0.1.3 lib/mongoid/report/collection.rb