lib/mongoid/report/collection.rb in mongoid-report-0.1.3 vs lib/mongoid/report/collection.rb in mongoid-report-0.1.5
- old
+ new
@@ -2,16 +2,22 @@
module Mongoid
module Report
class Collection < SimpleDelegator
- def initialize(rows, fields, columns)
+ def initialize(context, rows, fields, columns)
+ @context = context
@rows = rows
@fields = fields
@columns = columns
+
+ # Apply dyncamic columns in context of row and apply indifferent access
+ # for the rows.
+ rows = compile_dynamic_fields(rows, columns)
+
+ # Collection should behave like Array using delegator method.
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|
@@ -22,14 +28,16 @@
end.with_indifferent_access
end
private
- def compile_dynamic_fields(columns)
- self.each do |row|
+ def compile_dynamic_fields(rows, columns)
+ rows.map do |row|
@columns.each do |name, function|
- row[name] = function.call(row)
+ row[name] = function.call(@context, row)
end
+
+ row.with_indifferent_access
end
end
end
end