Sha256: 5bd8d6153f289a670ec4c00138acb84467264bf30a34cbda0078c4813c6a286c

Contents?: true

Size: 646 Bytes

Versions: 3

Compression:

Stored size: 646 Bytes

Contents

module Mongoid
  module Report

    # We are using this class to combine results by group by fields.
    Merger = Struct.new(:groups) do
      def do(rows)
        # Merge by groups.
        rows
          .group_by { |row| groups.map { |group| row[group] }.join('-') }
          .values
          .map { |array_row| combine(array_row) }
      end

      private

      def combine(rows)
        rows.inject(Hash.new {|h,k| h[k] = 0}) do |row, lines|
          lines.each do |key, value|
            next row[key] = value if groups.include?(key)
            row[key] += value
          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/merger.rb
mongoid-report-0.2.1 lib/mongoid/report/merger.rb
mongoid-report-0.2.0 lib/mongoid/report/merger.rb