mongoid-report 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.travis.yml +4 -0
- data/Gemfile +0 -2
- data/Gemfile.lock +1 -4
- data/README.md +23 -7
- data/Rakefile +4 -0
- data/lib/mongoid/report/queries_builder.rb +6 -6
- data/lib/mongoid/report/scope.rb +45 -0
- data/lib/mongoid/report/version.rb +1 -1
- data/lib/mongoid/report.rb +19 -23
- data/spec/mongoid/report/aggregation_spec.rb +10 -8
- data/spec/mongoid/report/summary_spec.rb +1 -0
- data/spec/spec_helper.rb +3 -18
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWFkMjE5MjllMjY1NjY3MjYzYTdlYTMyNWVhYTlmNjVjMzI4MWRjZg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
OWEzMzBkOTRlZTUxMzZhN2E1OWExMzNjYjM4ZTViZTIzODhjMzdhNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ODM5YjVmMjY5ZTEzMTgzNDNkMjMyMmI3NzExZDFkOGYwMGFhMjg4ZDAxMzc1
|
10
|
+
ZjBmNjhmMDczMjcxODEyY2ExMmEyZTk5YzlhZDhkODJlYmE2OGUxOWQ5ZmE5
|
11
|
+
MGU5MjIwZTQxMTdjZTY4ZGQ3MjFkZWRjM2EyZjg3NjY4ZDc1NDQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
ZWRmOWIwMWZkNzMwZmZjZTMxM2E5ZTg0M2IxZDcxN2U4NzYzY2Q4OWRkNDVm
|
14
|
+
MTgzMmQzNWIyN2RiNTZmZDdjMGQ0YTBkOTUxY2Q0Y2U5ZTk5ZjFjYTJkZDFl
|
15
|
+
MjFmZjkwYzY1MDhkM2EwNGU1NDE5ZTBjMDU1M2FmMDE4MGNjOGQ=
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mongoid-report (0.0.
|
4
|
+
mongoid-report (0.0.7)
|
5
5
|
mongoid (> 3.0.1)
|
6
6
|
|
7
7
|
GEM
|
@@ -16,7 +16,6 @@ GEM
|
|
16
16
|
builder (3.0.4)
|
17
17
|
coderay (1.1.0)
|
18
18
|
columnize (0.3.6)
|
19
|
-
database_cleaner (1.2.0)
|
20
19
|
debugger (1.6.6)
|
21
20
|
columnize (>= 0.3.1)
|
22
21
|
debugger-linecache (~> 1.2.0)
|
@@ -62,10 +61,8 @@ PLATFORMS
|
|
62
61
|
|
63
62
|
DEPENDENCIES
|
64
63
|
bundler (~> 1.5)
|
65
|
-
database_cleaner
|
66
64
|
mongoid-report!
|
67
65
|
pry
|
68
66
|
pry-debugger
|
69
67
|
rake
|
70
68
|
rspec
|
71
|
-
rspec-expectations
|
data/README.md
CHANGED
@@ -3,6 +3,10 @@
|
|
3
3
|
Library for easy building aggregation report using mongodb aggregation
|
4
4
|
framework.
|
5
5
|
|
6
|
+
[![Build Status](https://secure.travis-ci.org/oivoodoo/mongoid-report.png?branch=master)](https://travis-ci.org/oivoodoo/mongoid-report)
|
7
|
+
|
8
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/oivoodoo/mongoid-report)
|
9
|
+
|
6
10
|
### Example
|
7
11
|
|
8
12
|
```ruby
|
@@ -45,14 +49,30 @@ framework.
|
|
45
49
|
aggregation_field :field1
|
46
50
|
end
|
47
51
|
end
|
52
|
+
|
53
|
+
class Report5
|
54
|
+
include Mongoid::Report
|
55
|
+
|
56
|
+
attach_to Model, as: 'summary-report' do
|
57
|
+
group_by :day
|
58
|
+
|
59
|
+
aggregation_field :field1
|
60
|
+
end
|
61
|
+
end
|
48
62
|
```
|
49
63
|
|
50
64
|
```ruby
|
51
65
|
example = Report4.new
|
52
|
-
|
66
|
+
scope = example.aggregate_for(Model)
|
67
|
+
scope = scope.query('match' => { 'field1' => 1 })
|
68
|
+
result = scope.all
|
53
69
|
|
54
70
|
result.is_a?(Array) => true
|
55
71
|
result[0].is_a?(Hash) => true
|
72
|
+
|
73
|
+
example = Report5.new
|
74
|
+
scope = example.aggregate_for('summary-report')
|
75
|
+
result = scope.all
|
56
76
|
```
|
57
77
|
|
58
78
|
## Installation
|
@@ -67,15 +87,11 @@ And then execute:
|
|
67
87
|
|
68
88
|
Or install it yourself as:
|
69
89
|
|
70
|
-
$ gem install
|
71
|
-
|
72
|
-
## Usage
|
73
|
-
|
74
|
-
TODO: Write usage instructions here
|
90
|
+
$ gem install mongoid-report
|
75
91
|
|
76
92
|
## Contributing
|
77
93
|
|
78
|
-
1. Fork it ( http://github.com
|
94
|
+
1. Fork it ( http://github.com/oivoodoo/mongoid-report/fork )
|
79
95
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
80
96
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
81
97
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
@@ -4,9 +4,9 @@ module Mongoid
|
|
4
4
|
QueriesBuilder = Struct.new(:settings) do
|
5
5
|
def do
|
6
6
|
[].tap do |queries|
|
7
|
-
queries.concat([{ '$project' =>
|
8
|
-
queries.concat([{ '$group' =>
|
9
|
-
queries.concat([{ '$project' =>
|
7
|
+
queries.concat([{ '$project' => project_query }])
|
8
|
+
queries.concat([{ '$group' => group_query }])
|
9
|
+
queries.concat([{ '$project' => project_group_fields_query }])
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
@@ -33,14 +33,14 @@ module Mongoid
|
|
33
33
|
end
|
34
34
|
|
35
35
|
# Example: { '$project' => { :field1 => 1 } }
|
36
|
-
def
|
36
|
+
def project_query
|
37
37
|
all_fields.inject({}) do |hash, field|
|
38
38
|
hash.merge!(field => 1)
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
42
|
GROUP_TEMPLATE = "$%s"
|
43
|
-
def
|
43
|
+
def group_query
|
44
44
|
{}.tap do |query|
|
45
45
|
query[:_id] = {}
|
46
46
|
|
@@ -55,7 +55,7 @@ module Mongoid
|
|
55
55
|
end
|
56
56
|
|
57
57
|
PROJECT_TEMPLATE = "$_id.%s"
|
58
|
-
def
|
58
|
+
def project_group_fields_query
|
59
59
|
{}.tap do |query|
|
60
60
|
if groups == [:_id]
|
61
61
|
query[:_id] = '$_id'
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Report
|
3
|
+
|
4
|
+
Scope = Struct.new(:context, :report_name) do
|
5
|
+
def query(conditions)
|
6
|
+
queries.concat([conditions])
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
# We need to add grouping conditions when user needs it.
|
11
|
+
def yield
|
12
|
+
return self if @yielded
|
13
|
+
|
14
|
+
queries.concat(context.queries(report_name))
|
15
|
+
@yielded = true
|
16
|
+
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def all
|
21
|
+
self.yield unless yielded?
|
22
|
+
Collection.new(klass.collection.aggregate(queries), fields)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def yielded?
|
28
|
+
@yielded
|
29
|
+
end
|
30
|
+
|
31
|
+
def queries
|
32
|
+
@queries ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def klass
|
36
|
+
context.class.settings_property(report_name, :for)
|
37
|
+
end
|
38
|
+
|
39
|
+
def fields
|
40
|
+
context.class.settings_property(report_name, :fields)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
data/lib/mongoid/report.rb
CHANGED
@@ -4,6 +4,7 @@ require 'active_support/core_ext/class/attribute'
|
|
4
4
|
require_relative 'report/queries_builder'
|
5
5
|
require_relative 'report/attach_proxy'
|
6
6
|
require_relative 'report/collection'
|
7
|
+
require_relative 'report/scope'
|
7
8
|
|
8
9
|
module Mongoid
|
9
10
|
module Report
|
@@ -36,17 +37,8 @@ module Mongoid
|
|
36
37
|
end
|
37
38
|
|
38
39
|
# We should pass here mongoid document
|
39
|
-
def aggregate_for(
|
40
|
-
|
41
|
-
|
42
|
-
yield queries if block_given?
|
43
|
-
|
44
|
-
# Lets wrap aggregation by collection structure for adding common
|
45
|
-
# methods like summary for data.
|
46
|
-
Collection.new(
|
47
|
-
klass.collection.aggregate(queries),
|
48
|
-
self.class.fields(klass),
|
49
|
-
)
|
40
|
+
def aggregate_for(report_name)
|
41
|
+
Scope.new(self, report_name)
|
50
42
|
end
|
51
43
|
end
|
52
44
|
|
@@ -78,33 +70,40 @@ module Mongoid
|
|
78
70
|
settings_property(collection, :group_by)
|
79
71
|
end
|
80
72
|
|
73
|
+
def settings_property(collection, key)
|
74
|
+
settings.fetch(collection, {}).fetch(key, [])
|
75
|
+
end
|
76
|
+
|
81
77
|
private
|
82
78
|
|
83
79
|
def define_report_method(*fields)
|
84
80
|
options = fields.extract_options!
|
85
81
|
|
86
|
-
# We should always specify model to attach fields, groups
|
87
|
-
collection = options.fetch(:for)
|
88
|
-
|
89
|
-
# If user didn't pass as option to name the report we are using
|
90
|
-
# collection class as key for settings.
|
91
|
-
report_name = options.fetch(:as) { collection }
|
92
|
-
|
93
82
|
# We should always have for option
|
94
|
-
initialize_settings_by(
|
83
|
+
report_name = initialize_settings_by(options)
|
95
84
|
|
96
85
|
# Because of modifying fields(usign exract options method of
|
97
86
|
# ActiveSupport) lets pass fields to the next block with collection.
|
98
87
|
yield fields, report_name
|
99
88
|
end
|
100
89
|
|
101
|
-
def initialize_settings_by(
|
90
|
+
def initialize_settings_by(options)
|
91
|
+
# We should always specify model to attach fields, groups
|
92
|
+
collection = options.fetch(:for)
|
93
|
+
|
94
|
+
# If user didn't pass as option to name the report we are using
|
95
|
+
# collection class as key for settings.
|
96
|
+
report_name = options.fetch(:as) { collection }
|
97
|
+
|
102
98
|
settings[report_name] ||= settings.fetch(report_name) do
|
103
99
|
{
|
100
|
+
for: collection,
|
104
101
|
fields: [],
|
105
102
|
group_by: [],
|
106
103
|
}
|
107
104
|
end
|
105
|
+
|
106
|
+
report_name
|
108
107
|
end
|
109
108
|
|
110
109
|
def add_field(report_name, field)
|
@@ -117,9 +116,6 @@ module Mongoid
|
|
117
116
|
FIELD
|
118
117
|
end
|
119
118
|
|
120
|
-
def settings_property(collection, key)
|
121
|
-
settings.fetch(collection, {}).fetch(key, [])
|
122
|
-
end
|
123
119
|
end
|
124
120
|
|
125
121
|
end
|
@@ -13,6 +13,7 @@ describe Mongoid::Report do
|
|
13
13
|
|
14
14
|
example = Report2.new
|
15
15
|
rows = example.aggregate_for(klass)
|
16
|
+
rows = rows.all
|
16
17
|
|
17
18
|
expect(rows.size).to eq(3)
|
18
19
|
expect(rows[0]['field1']).to eq(1)
|
@@ -26,7 +27,9 @@ describe Mongoid::Report do
|
|
26
27
|
klass.create!(day: yesterday , field1: 1)
|
27
28
|
|
28
29
|
example = Report3.new
|
30
|
+
|
29
31
|
rows = example.aggregate_for(klass)
|
32
|
+
rows = rows.all
|
30
33
|
|
31
34
|
expect(rows.size).to eq(2)
|
32
35
|
expect(rows[0]['field1']).to eq(1)
|
@@ -45,14 +48,13 @@ describe Mongoid::Report do
|
|
45
48
|
klass.create(day: today , field1: 1 , field2: 3)
|
46
49
|
|
47
50
|
example = Report3.new
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
51
|
+
scope = example.aggregate_for(Model)
|
52
|
+
scope = scope.query('$match' => { :day => { '$gte' => yesterday.mongoize, '$lte' => today.mongoize } })
|
53
|
+
scope = scope.query('$match' => { :field2 => 2 })
|
54
|
+
scope = scope.yield
|
55
|
+
scope = scope.query('$sort' => { day: -1 })
|
56
|
+
|
57
|
+
rows = scope.all
|
56
58
|
|
57
59
|
expect(rows.size).to eq(2)
|
58
60
|
expect(rows[0]['field1']).to eq(2)
|
data/spec/spec_helper.rb
CHANGED
@@ -2,20 +2,11 @@ require 'mongoid'
|
|
2
2
|
require 'active_support/all'
|
3
3
|
|
4
4
|
require 'rspec'
|
5
|
-
require 'database_cleaner'
|
6
5
|
|
7
6
|
Mongoid.configure do |config|
|
8
7
|
config.connect_to('mongoid_report_test')
|
9
8
|
end
|
10
9
|
|
11
|
-
RSpec.configure do |config|
|
12
|
-
config.after(:each) do
|
13
|
-
Mongoid.purge!
|
14
|
-
end
|
15
|
-
|
16
|
-
config.backtrace_exclusion_patterns = [%r{lib\/rspec\/(core|expectations|matchers|mocks)}]
|
17
|
-
end
|
18
|
-
|
19
10
|
require_relative '../lib/mongoid/report.rb'
|
20
11
|
require_relative 'support/models.rb'
|
21
12
|
|
@@ -25,15 +16,9 @@ RSpec.configure do |config|
|
|
25
16
|
|
26
17
|
config.mock_with :rspec
|
27
18
|
|
28
|
-
config.before(:suite) do
|
29
|
-
DatabaseCleaner.clean_with(:truncation)
|
30
|
-
end
|
31
|
-
|
32
|
-
config.before(:each) do
|
33
|
-
DatabaseCleaner.start
|
34
|
-
end
|
35
|
-
|
36
19
|
config.after(:each) do
|
37
|
-
|
20
|
+
Mongoid.purge!
|
38
21
|
end
|
22
|
+
|
23
|
+
config.backtrace_exclusion_patterns = [%r{lib\/rspec\/(core|expectations|matchers|mocks)}]
|
39
24
|
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.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexandr Korsak
|
@@ -62,6 +62,7 @@ files:
|
|
62
62
|
- .gitignore
|
63
63
|
- .rspec
|
64
64
|
- .ruby-version
|
65
|
+
- .travis.yml
|
65
66
|
- .vimrc
|
66
67
|
- Gemfile
|
67
68
|
- Gemfile.lock
|
@@ -72,6 +73,7 @@ files:
|
|
72
73
|
- lib/mongoid/report/attach_proxy.rb
|
73
74
|
- lib/mongoid/report/collection.rb
|
74
75
|
- lib/mongoid/report/queries_builder.rb
|
76
|
+
- lib/mongoid/report/scope.rb
|
75
77
|
- lib/mongoid/report/version.rb
|
76
78
|
- mongoid-report.gemspec
|
77
79
|
- spec/mongoid/report/aggregation_spec.rb
|