Class: Mongoid::Report::Scope
- Inherits:
-
Struct
- Object
- Struct
- Mongoid::Report::Scope
- Defined in:
- lib/mongoid/report/scope.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#report_module ⇒ Object
Returns the value of attribute report_module.
-
#report_name ⇒ Object
Returns the value of attribute report_name.
Instance Method Summary collapse
- #all ⇒ Object
- #all_in_batches(aggregation_queries) ⇒ Object
- #all_inline(aggregation_queries) ⇒ Object
- #in(collection_name) ⇒ Object
- #in_batches(conditions) ⇒ Object
- #out(collection_name, options = {}) ⇒ Object
- #query(conditions = {}) ⇒ Object
-
#yield ⇒ Object
We need to add grouping conditions when user needs it.
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context
6 7 8 |
# File 'lib/mongoid/report/scope.rb', line 6 def context @context end |
#report_module ⇒ Object
Returns the value of attribute report_module
6 7 8 |
# File 'lib/mongoid/report/scope.rb', line 6 def report_module @report_module end |
#report_name ⇒ Object
Returns the value of attribute report_name
6 7 8 |
# File 'lib/mongoid/report/scope.rb', line 6 def report_name @report_name end |
Instance Method Details
#all ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/mongoid/report/scope.rb', line 62 def all self.yield unless yielded? aggregation_queries = compile_queries rows = if batches.present? all_in_batches(aggregation_queries) else all_inline(aggregation_queries) end # in case if we want to store rows to collection if output.present? output.do(rows) end Collection.new(context, rows, fields, columns, mapping) end |
#all_in_batches(aggregation_queries) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mongoid/report/scope.rb', line 33 def all_in_batches(aggregation_queries) # Lets assume we have only one field for making splits for the # aggregation queries. rows = [] threads = batches.map do |r| # For now we are supporting only data fields for splitting up the # queries. range_match = r.map { |time| time.to_date.mongoize } Thread.new do q = ['$match' => { batches.field => { '$gte' => range_match.first, '$lte' => range_match.last } }] + aggregation_queries # if groups == [batch.field] rows.concat(Array(collection.aggregate(q))) end end threads.map(&:join) merger = Mongoid::Report::Merger.new(groups) merger.do(rows) end |
#all_inline(aggregation_queries) ⇒ Object
58 59 60 |
# File 'lib/mongoid/report/scope.rb', line 58 def all_inline(aggregation_queries) Array(collection.aggregate(aggregation_queries)) end |
#in(collection_name) ⇒ Object
28 29 30 31 |
# File 'lib/mongoid/report/scope.rb', line 28 def in(collection_name) input.collection_name = collection_name self end |
#in_batches(conditions) ⇒ Object
81 82 83 84 |
# File 'lib/mongoid/report/scope.rb', line 81 def in_batches(conditions) batches.conditions = conditions self end |
#out(collection_name, options = {}) ⇒ Object
22 23 24 25 26 |
# File 'lib/mongoid/report/scope.rb', line 22 def out(collection_name, = {}) output.collection_name = collection_name output. = self end |
#query(conditions = {}) ⇒ Object
7 8 9 10 |
# File 'lib/mongoid/report/scope.rb', line 7 def query(conditions = {}) queries.concat([conditions]) unless conditions.empty? self end |
#yield ⇒ Object
We need to add grouping conditions when user needs it.
13 14 15 16 17 18 19 20 |
# File 'lib/mongoid/report/scope.rb', line 13 def yield return self if @yielded queries.concat(context.queries(report_module, report_name)) @yielded = true self end |