lib/mongoid/report/scope_collection.rb in mongoid-report-0.1.3 vs lib/mongoid/report/scope_collection.rb in mongoid-report-0.1.5

- old
+ new

@@ -1,9 +1,16 @@ +require 'thread' + module Mongoid module Report ScopeCollection = Struct.new(:context) do + def initialize(context) + @mutex = Mutex.new + super + end + def scopes @scopes ||= modules.map do |key| Scope.new(context, key) end end @@ -21,12 +28,25 @@ end self end def all - scopes.inject({}) do |hash, scope| - hash[scope.report_name] = scope.all - hash + {}.tap do |hash| + if Mongoid::Report::Config.use_threads_on_aggregate + scopes.map do |scope| + Thread.new do + rows = scope.all + + @mutex.synchronize do + hash[scope.report_name] = rows + end + end + end.map(&:join) + else + scopes.each do |scope| + hash[scope.report_name] = scope.all + end + end end end private