Class: Mongoid::Report::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/report/collection.rb

Defined Under Namespace

Classes: Rows

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, rows, fields, columns, mapping) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
11
12
13
14
# File 'lib/mongoid/report/collection.rb', line 7

def initialize(context, rows, fields, columns, mapping)
  @context = context
  @rows    = rows
  @fields  = fields
  @columns = columns
  @mapping = mapping
  @rows    = Rows.new(compile_rows)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



18
19
20
# File 'lib/mongoid/report/collection.rb', line 18

def context
  @context
end

#rowsObject (readonly)

Returns the value of attribute rows.



18
19
20
# File 'lib/mongoid/report/collection.rb', line 18

def rows
  @rows
end

Instance Method Details

#headersObject



20
21
22
# File 'lib/mongoid/report/collection.rb', line 20

def headers
  @fields
end

#summaryObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mongoid/report/collection.rb', line 24

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