Class: Mongoid::Report::Collection
- Inherits:
-
Object
- Object
- Mongoid::Report::Collection
- Defined in:
- lib/mongoid/report/collection.rb
Defined Under Namespace
Classes: Rows
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(context, rows, fields, columns, mapping) ⇒ Collection
constructor
A new instance of Collection.
- #summary ⇒ Object
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
18 19 20 |
# File 'lib/mongoid/report/collection.rb', line 18 def context @context end |
#rows ⇒ Object (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
#headers ⇒ Object
20 21 22 |
# File 'lib/mongoid/report/collection.rb', line 20 def headers @fields end |
#summary ⇒ Object
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 |