mongoid-report 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- N2Y0NzI2YzMwYTk3NTljY2VkODkzZjcxYjYyNmM2YzZmYmVlMDdmNw==
4
+ NjBjODE4ODliYjQ0ZDQzZDQ3MzRjNjZmMWUyM2FmOTU5ZDVlNzRjZg==
5
5
  data.tar.gz: !binary |-
6
- NWZkNWZlYWMxNzJkYWRkZDkzMWY4MmYxYzZkYmNmMzk5MTIwNWJjZg==
6
+ Y2U2ZGQxYjVhZjAyZDcxYzY4ZTdmMTJhNWFiMmY5YWE5OTY4M2RjNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZWZjNjg4NWFlOWFjZWQ0ZmNhODVlYmViYzA1Njg3ZDAwYjhlMDI4YWEwMDM1
10
- YTg5NTQ1MDAxNjk5MmI2ZTRhNzkwOTkwNTdmNGU5ZTdkZGRhZTE0OGUwNGQw
11
- N2Y4NjMxYjY4ZmYyNTdjZWY1NzYxZGYwMGEyNDc4NjNiNzE1ZDY=
9
+ MDQ3MDBmNmY2YWNlMDFmOGJjY2JiYTJhNjZiYzkxYTQ0Yzg0ZjZmYWUyMDFj
10
+ MzE1YmUxOTBkMDNkZjQ3MGVmN2I0MzVjOWMyMzNjMWQ2NGYwZTJjZjE5Mjdh
11
+ OWZmODY0MGRkZGZiZWJiYTkxZmZkNzk4ZGQ0ZDFjMjlhZWU3MjM=
12
12
  data.tar.gz: !binary |-
13
- MjcxNzZkZTBhNzEzZDQ1MmNkNDJiMWU1Yjg5NGM0MTA2OWNhMmQ0ZTlhYWEx
14
- YjhiNTdhN2ViNDRjYTY1M2E0YjI3Y2EzMTA5OTBiZGU3ZjI2M2IzYTgwZjcw
15
- YjIwOWI1ZWM4ZWY2YjA1NDQ2YmMwN2NjMTAyZmMxYWYwMmZmNmM=
13
+ Zjk0OTJmOTk1NjQyNDEyMTdkMDViZWI3MjQxNTgwMjJiNDA5NDc5ZDI4YTBl
14
+ ZjI1ODU0ZjgwOThmM2ZkOGMxYzU5ZjRjNDUzYTFiNzg3NjYyOGVmNWEyY2Vk
15
+ MzMxZDhkNjI1YzJiZjZiNzQzZTQ0NDNlYmRkYzI4YjM5ZDFjOWE=
data/.drone.yml ADDED
@@ -0,0 +1,16 @@
1
+ image: bradrydzewski/ruby:1.9.3
2
+ script:
3
+ - gem install bundler --no-ri --no-rdoc
4
+ - bundle install
5
+ - bundle exec rake default
6
+ services:
7
+ - mongodb
8
+ notify:
9
+ email:
10
+ recipients:
11
+ - alex.korsak@gmail.com
12
+ flowdock:
13
+ flow_token: $FLOW_TOKEN
14
+ on_started: true
15
+ on_success: true
16
+ on_failure: true
data/Gemfile CHANGED
@@ -9,4 +9,7 @@ end
9
9
  group :development, :test do
10
10
  gem 'pry'
11
11
  gem 'pry-debugger'
12
+ gem 'guard'
13
+ gem 'guard-rspec'
14
+ gem 'guard-bundler'
12
15
  end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid-report (0.1.1)
4
+ mongoid-report (0.1.2)
5
5
  mongoid (> 3.0.1)
6
6
 
7
7
  GEM
@@ -14,6 +14,11 @@ GEM
14
14
  i18n (~> 0.6, >= 0.6.4)
15
15
  multi_json (~> 1.0)
16
16
  builder (3.0.4)
17
+ celluloid (0.15.2)
18
+ timers (~> 1.1.0)
19
+ celluloid-io (0.15.0)
20
+ celluloid (>= 0.15.0)
21
+ nio4r (>= 0.5.0)
17
22
  coderay (1.1.0)
18
23
  columnize (0.3.6)
19
24
  debugger (1.6.6)
@@ -23,7 +28,27 @@ GEM
23
28
  debugger-linecache (1.2.0)
24
29
  debugger-ruby_core_source (1.3.2)
25
30
  diff-lcs (1.2.5)
31
+ ffi (1.9.3)
32
+ formatador (0.2.4)
33
+ guard (2.6.0)
34
+ formatador (>= 0.2.4)
35
+ listen (~> 2.7)
36
+ lumberjack (~> 1.0)
37
+ pry (>= 0.9.12)
38
+ thor (>= 0.18.1)
39
+ guard-bundler (2.0.0)
40
+ bundler (~> 1.0)
41
+ guard (~> 2.2)
42
+ guard-rspec (4.2.8)
43
+ guard (~> 2.1)
44
+ rspec (>= 2.14, < 4.0)
26
45
  i18n (0.6.9)
46
+ listen (2.7.1)
47
+ celluloid (>= 0.15.2)
48
+ celluloid-io (>= 0.15.0)
49
+ rb-fsevent (>= 0.9.3)
50
+ rb-inotify (>= 0.9)
51
+ lumberjack (1.0.5)
27
52
  method_source (0.8.2)
28
53
  mongoid (3.1.6)
29
54
  activemodel (~> 3.2)
@@ -32,6 +57,7 @@ GEM
32
57
  tzinfo (~> 0.3.29)
33
58
  moped (1.5.2)
34
59
  multi_json (1.10.1)
60
+ nio4r (1.0.0)
35
61
  origin (1.1.0)
36
62
  pry (0.9.12.6)
37
63
  coderay (~> 1.0)
@@ -41,6 +67,9 @@ GEM
41
67
  debugger (~> 1.3)
42
68
  pry (~> 0.9.10)
43
69
  rake (10.3.2)
70
+ rb-fsevent (0.9.4)
71
+ rb-inotify (0.9.3)
72
+ ffi (>= 0.5.0)
44
73
  rspec (3.0.0)
45
74
  rspec-core (~> 3.0.0)
46
75
  rspec-expectations (~> 3.0.0)
@@ -54,6 +83,8 @@ GEM
54
83
  rspec-support (~> 3.0.0)
55
84
  rspec-support (3.0.0)
56
85
  slop (3.5.0)
86
+ thor (0.19.1)
87
+ timers (1.1.0)
57
88
  tzinfo (0.3.39)
58
89
 
59
90
  PLATFORMS
@@ -61,6 +92,9 @@ PLATFORMS
61
92
 
62
93
  DEPENDENCIES
63
94
  bundler (~> 1.5)
95
+ guard
96
+ guard-bundler
97
+ guard-rspec
64
98
  mongoid-report!
65
99
  pry
66
100
  pry-debugger
data/Guardfile ADDED
@@ -0,0 +1,14 @@
1
+ guard 'rspec', :cli => '--format documentation', :version => 2, :all_after_pass => false, :keep_failed => false do
2
+ watch(%r{^spec/.+_spec\.rb$})
3
+ watch(%r{^spec/.+\.rb$})
4
+ watch(%r{^spec/support/.+\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { "spec" }
6
+ watch(%r{^lib/mongoid/(.+)\.rb$}) { "spec" }
7
+ watch(%r{^lib/mongoid/report/(.+)\.rb$}) { "spec" }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ end
10
+
11
+ guard 'bundler' do
12
+ watch('Gemfile')
13
+ watch(/^.+\.gemspec/)
14
+ end
@@ -12,24 +12,27 @@ module Mongoid
12
12
  end
13
13
 
14
14
  def aggregation_field(*fields)
15
- field_options = fields.extract_options!
16
- field_options.merge!(options)
17
-
18
- context.aggregation_field(*fields, field_options)
15
+ proxy_options = fields.extract_options!
16
+ proxy_options.merge!(options)
17
+ context.aggregation_field(*fields, proxy_options)
19
18
  end
20
19
 
21
20
  def group_by(*fields)
22
- group_options = fields.extract_options!
23
- group_options.merge!(options)
24
-
25
- context.group_by(*fields, group_options)
21
+ proxy_options = fields.extract_options!
22
+ proxy_options.merge!(options)
23
+ context.group_by(*fields, proxy_options)
26
24
  end
27
25
 
28
26
  def filter(*fields)
29
- filter_options = fields.extract_options!
30
- filter_options.merge!(options)
27
+ proxy_options = fields.extract_options!
28
+ proxy_options.merge!(options)
29
+ context.filter(*fields, proxy_options)
30
+ end
31
31
 
32
- context.filter(*fields, filter_options)
32
+ def column(*fields)
33
+ proxy_options = fields.extract_options!
34
+ proxy_options.merge!(options)
35
+ context.column(*fields, proxy_options)
33
36
  end
34
37
  end
35
38
 
@@ -4,10 +4,12 @@ module Mongoid
4
4
  module Report
5
5
 
6
6
  class Collection < SimpleDelegator
7
- def initialize(rows, fields)
8
- @rows = rows
9
- @fields = fields
7
+ def initialize(rows, fields, columns)
8
+ @rows = rows
9
+ @fields = fields
10
+ @columns = columns
10
11
  super(rows)
12
+ compile_dynamic_fields(columns)
11
13
  end
12
14
 
13
15
  def summary
@@ -19,6 +21,16 @@ module Mongoid
19
21
  summary
20
22
  end
21
23
  end
24
+
25
+ private
26
+
27
+ def compile_dynamic_fields(columns)
28
+ self.each do |row|
29
+ @columns.each do |name, function|
30
+ row[name] = function.call(row)
31
+ end
32
+ end
33
+ end
22
34
  end
23
35
 
24
36
  end
@@ -5,7 +5,7 @@ module Mongoid
5
5
  def attach_to(model, options = {}, &block)
6
6
  as = options.fetch(:as) { model.collection.name }
7
7
 
8
- options.merge!(as: "#{name}-#{as}") if as
8
+ options.merge!(as: "#{name}-#{as}")
9
9
 
10
10
  context.attach_to(model, options, &block)
11
11
  end
@@ -20,7 +20,7 @@ module Mongoid
20
20
  def all
21
21
  self.yield unless yielded?
22
22
  queries = compile_queries
23
- Collection.new(klass.collection.aggregate(queries), fields)
23
+ Collection.new(klass.collection.aggregate(queries), fields, columns)
24
24
  end
25
25
 
26
26
  private
@@ -55,6 +55,10 @@ module Mongoid
55
55
  # than defined colunms, Example: field1: 'report-field-name'
56
56
  context.class.settings_property(report_name, :fields).values
57
57
  end
58
+
59
+ def columns
60
+ context.class.settings_property(report_name, :columns)
61
+ end
58
62
  end
59
63
 
60
64
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Report
3
- VERSION = "0.1.1"
3
+ VERSION = "0.1.2"
4
4
  end
5
5
  end
@@ -87,6 +87,16 @@ module Mongoid
87
87
  end
88
88
  end
89
89
 
90
+ def column(*fields)
91
+ # Because of representing fields as hash instead of columns we should
92
+ # have the last variable as columns.
93
+ define_report_method(*fields) do |_, report_name, columns|
94
+ columns.each do |name, function|
95
+ add_column(report_name, name, function)
96
+ end
97
+ end
98
+ end
99
+
90
100
  def fields(collection)
91
101
  settings_property(collection, :fields, {})
92
102
  end
@@ -127,9 +137,10 @@ module Mongoid
127
137
  settings[attach_name] ||= settings.fetch(attach_name) do
128
138
  {
129
139
  for: collection,
130
- fields: {},
140
+ fields: ActiveSupport::OrderedHash.new,
131
141
  group_by: [],
132
142
  queries: [],
143
+ columns: ActiveSupport::OrderedHash.new,
133
144
  }
134
145
  end
135
146
  end
@@ -138,6 +149,10 @@ module Mongoid
138
149
  settings[attach_name][:fields][field] = name
139
150
  end
140
151
 
152
+ def add_column(attach_name, name, function)
153
+ settings[attach_name][:columns][name] = function
154
+ end
155
+
141
156
  end
142
157
 
143
158
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Report do
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
+ let(:report_klass) do
9
+ Class.new do
10
+ include Mongoid::Report
11
+
12
+ report 'example' do
13
+ attach_to Model do
14
+ group_by :day
15
+ aggregation_field :field1
16
+ column 'dynamic-field1' => ->(row) { row['field1'] * 10 }
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ it 'calculates dynamic field for each row in the report' do
23
+ klass.create(day: today , field1: 1)
24
+ klass.create(day: yesterday , field1: 1)
25
+ klass.create(day: today , field1: 1)
26
+
27
+ example = report_klass.new
28
+ scope = example.aggregate
29
+ scope = scope.all
30
+
31
+ rows = scope['example-models']
32
+ expect(rows.size).to eq(2)
33
+ expect(rows[0]['field1']).to eq(1)
34
+ expect(rows[0]['dynamic-field1']).to eq(10)
35
+ expect(rows[1]['field1']).to eq(2)
36
+ expect(rows[1]['dynamic-field1']).to eq(20)
37
+ end
38
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Korsak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2014-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -59,6 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - .drone.yml
62
63
  - .gitignore
63
64
  - .rspec
64
65
  - .ruby-version
@@ -66,6 +67,7 @@ files:
66
67
  - .vimrc
67
68
  - Gemfile
68
69
  - Gemfile.lock
70
+ - Guardfile
69
71
  - LICENSE.txt
70
72
  - README.md
71
73
  - Rakefile
@@ -79,6 +81,7 @@ files:
79
81
  - lib/mongoid/report/version.rb
80
82
  - mongoid-report.gemspec
81
83
  - spec/mongoid/report/aggregation_spec.rb
84
+ - spec/mongoid/report/column_spec.rb
82
85
  - spec/mongoid/report/queries_builder_spec.rb
83
86
  - spec/mongoid/report/summary_spec.rb
84
87
  - spec/mongoid/report_spec.rb
@@ -110,6 +113,7 @@ specification_version: 4
110
113
  summary: Easily build mongoid reports using aggregation framework
111
114
  test_files:
112
115
  - spec/mongoid/report/aggregation_spec.rb
116
+ - spec/mongoid/report/column_spec.rb
113
117
  - spec/mongoid/report/queries_builder_spec.rb
114
118
  - spec/mongoid/report/summary_spec.rb
115
119
  - spec/mongoid/report_spec.rb