Sha256: 0b426b32052944301fae716ad92669ba15468e2fe27439490fca8a9e2016e448

Contents?: true

Size: 736 Bytes

Versions: 1

Compression:

Stored size: 736 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
      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.2 lib/mongoid/report/collection.rb