mongoid-report 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NTc1MDVkMjAwOGQ1MTRlM2E0NWQ2ZDk4ODE4MjllZjEzZGRmYWJhMA==
4
+ Mzk5ZjE1YTRjNTNkZjRmOWVlMzM1OTJjMjExZWQ3NjljOTk5NTY5Mw==
5
5
  data.tar.gz: !binary |-
6
- ZDc5ZDViNjhhZmFkZTQ1MzBiZWYwYzVmNTVhYzUzYTM3ZDBjNGYxNg==
6
+ ZmU3YWU2NDBiN2I4NDZlMjViZGM0MTljNGY2NGIxYmNkZWExN2ViNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NzZjMTZiYjc4NDJjOWRkM2VmYjg3NDYwZWUxNWY1MzNhNTg4NzFlODczYTkz
10
- ZTdiYmEzYjUxMzNhY2Q5OWYyNTY4NTIzMjZiOTljNjI2ZWYwZDBkMmVhM2Qw
11
- MWY1ZjBmY2YxNWI2Y2Y2ZjJlNWNjYjJiMjhmOGJmMDVkZjhlNDA=
9
+ YmY2NTA3ZGZjNjY4ZTU3NmNiZGQ1MGIzM2U4YWNhM2FkMWQ5YTA4NWZhMzll
10
+ N2JjM2VmNmNjYmFhMjEyYTI4ZGY4MDRkNTk2YzQ3NTI0Y2IxNDk4MTUyODY4
11
+ OTY2ZjAzNGMxZjkwOWRmNmIzYjA3MGY3ZTE3MjY2MTg3OWJiZWY=
12
12
  data.tar.gz: !binary |-
13
- ODMzNGM5YmRlMzhlODVhMWI0MDE4MWEwMzdjMjkwMWI4MGJlNDU5NWY2OWJi
14
- ODlkMTA2Y2MwMjk4YmJiNzkwMTRiNTBhN2E2Yjg4OWMwYWVjMzU0YTI3ZGFl
15
- N2I1OTMyNjEyMDJlMjc2NzJhNTkyNTY4Yjk4NzQzZTAyMjA0MDM=
13
+ YzM2ZTZmZWFlODA4NjI1MTIzYTY0NDk5YzkyM2EwNDQ3ZWYwNmQ0MTY4ZGI2
14
+ NjUxZjQzZDg5ZmU3NDU3ZWU4OWIxMWZjZGU5YmRjMmJhMTgyYzNiNTJjNjBj
15
+ NDQ5YWZjNGNkOTU1MDBlMTEyNjE2N2U4ODIzNDJjYmQ0YjJiZTA=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-report (0.0.8)
4
+ mongoid-report (0.0.9)
5
5
  mongoid (> 3.0.1)
6
6
 
7
7
  GEM
@@ -0,0 +1,40 @@
1
+ module Mongoid
2
+ module Report
3
+
4
+ ScopeCollection = Struct.new(:context) do
5
+ def scopes
6
+ @scopes ||= modules.map do |key|
7
+ Scope.new(context, key)
8
+ end
9
+ end
10
+
11
+ def query(conditions = {})
12
+ scopes.each do |scope|
13
+ scope.query(conditions)
14
+ end
15
+ self
16
+ end
17
+
18
+ def yield
19
+ scopes.each do |scope|
20
+ scope.yield
21
+ end
22
+ self
23
+ end
24
+
25
+ def all
26
+ scopes.inject({}) do |hash, scope|
27
+ hash[scope.report_name] = scope.all
28
+ hash
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def modules
35
+ context.settings.keys
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Report
3
- VERSION = "0.0.8"
3
+ VERSION = "0.0.9"
4
4
  end
5
5
  end
@@ -5,6 +5,7 @@ require_relative 'report/queries_builder'
5
5
  require_relative 'report/attach_proxy'
6
6
  require_relative 'report/collection'
7
7
  require_relative 'report/scope'
8
+ require_relative 'report/scope_collection'
8
9
 
9
10
  module Mongoid
10
11
  module Report
@@ -40,6 +41,10 @@ module Mongoid
40
41
  def aggregate_for(report_name)
41
42
  Scope.new(self, report_name)
42
43
  end
44
+
45
+ def aggregate
46
+ ScopeCollection.new(self)
47
+ end
43
48
  end
44
49
 
45
50
  module ClassMethods
@@ -1,11 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Mongoid::Report do
4
- describe '.aggregate_for' do
5
- let(:klass) { Model }
6
- let(:yesterday) { Date.parse("19-12-2004") }
7
- let(:today) { Date.parse("20-12-2004") }
4
+ let(:klass) { Model }
5
+ let(:yesterday) { Date.parse("19-12-2004") }
6
+ let(:today) { Date.parse("20-12-2004") }
7
+ let(:two_days_ago) { Date.parse("18-12-2004") }
8
8
 
9
+ describe '.aggregate_for' do
9
10
  it 'aggregates fields by default group _id as well' do
10
11
  instance1 = klass.create!(day: today , field1: 1)
11
12
  instance2 = klass.create!(day: today , field1: 1)
@@ -38,8 +39,6 @@ describe Mongoid::Report do
38
39
  expect(rows[1]['day']).to eq(today)
39
40
  end
40
41
 
41
- let(:two_days_ago) { Date.parse("18-12-2004") }
42
-
43
42
  it 'wraps group query by extra match queries' do
44
43
  klass.create(day: today , field1: 1 , field2: 2)
45
44
  klass.create(day: today , field1: 1 , field2: 2)
@@ -78,4 +77,50 @@ describe Mongoid::Report do
78
77
  expect(rows[0]['day']).to eq(today)
79
78
  end
80
79
  end
80
+
81
+ class Report7
82
+ include Mongoid::Report
83
+
84
+ attach_to Model, as: 'example1' do
85
+ group_by :day
86
+ aggregation_field :field1
87
+ end
88
+
89
+ attach_to Model, as: 'example2' do
90
+ group_by :day
91
+ aggregation_field :field2
92
+ end
93
+ end
94
+
95
+ describe '.aggregate' do
96
+ it 'aggregates all defined groups in the report class' do
97
+ klass.create(day: today , field1: 1 , field2: 2)
98
+ klass.create(day: today , field1: 1 , field2: 2)
99
+ klass.create(day: yesterday , field1: 1 , field2: 2)
100
+ klass.create(day: two_days_ago , field1: 1 , field2: 2)
101
+
102
+ example = Report7.new
103
+ scope = example.aggregate
104
+ scope
105
+ .query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
106
+ .yield
107
+ .query('$sort' => { day: -1 })
108
+ scope = scope.all
109
+
110
+ rows = scope['example1']
111
+ expect(rows.size).to eq(2)
112
+ expect(rows[0]['field1']).to eq(2)
113
+ expect(rows[0]['day']).to eq(today)
114
+ expect(rows[1]['field1']).to eq(1)
115
+ expect(rows[1]['day']).to eq(yesterday)
116
+
117
+ rows = scope['example2']
118
+ expect(rows.size).to eq(2)
119
+ expect(rows[0]['field2']).to eq(4)
120
+ expect(rows[0]['day']).to eq(today)
121
+ expect(rows[1]['field2']).to eq(2)
122
+ expect(rows[1]['day']).to eq(yesterday)
123
+ end
124
+ end
125
+
81
126
  end
@@ -68,5 +68,4 @@ describe Mongoid::Report do
68
68
  expect(Report6.settings).to have_key('example1')
69
69
  end
70
70
  end
71
-
72
71
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Korsak
@@ -74,6 +74,7 @@ files:
74
74
  - lib/mongoid/report/collection.rb
75
75
  - lib/mongoid/report/queries_builder.rb
76
76
  - lib/mongoid/report/scope.rb
77
+ - lib/mongoid/report/scope_collection.rb
77
78
  - lib/mongoid/report/version.rb
78
79
  - mongoid-report.gemspec
79
80
  - spec/mongoid/report/aggregation_spec.rb