mongoid-collection-snapshot 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fdda543f7183d461cf4b7068b97c7c9bba6523b9
4
- data.tar.gz: a83c53f21498ad03d49089382dd9e519a9451d79
3
+ metadata.gz: a83bba2b3b5308ff4015fb6b1160946e9ce76acc
4
+ data.tar.gz: 74c2c24fcee8b86d53cd090cddcb3dd9b59ce117
5
5
  SHA512:
6
- metadata.gz: de91fdde1b8c8b6457cdcecb4e91eec67f4eb3fddf0f96702092ef3a8df9e4747401571d1475478e1f82e28b33133287df422fce178ac245c238c6474de2be93
7
- data.tar.gz: c9682df34fa35b1e444f6f10ccbf28a15517ca141c397d689493aca9de664c0c524cebb11c1a58f7a3abafd15fca855ac233fa3c1d538e408c3fc494392558c4
6
+ metadata.gz: b4d4d635979850c86c773192dea47a00497a48a5d1220fce28e80cae6e002960ffa3a9f7db48ac5be62c6c22043120dbf4bb117971705561f831fb617b033ad3
7
+ data.tar.gz: ac843dacc50abd245f56487ddaa49542d2aed4cc70d4a82ab9e84d359b0bc2306217471db489db6a70ff7798babd7736c6fdd042ab230958204507be1e3853fa
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2017-01-20 11:17:06 -0500 using RuboCop version 0.47.1.
3
+ # on 2017-01-20 15:27:40 -0500 using RuboCop version 0.47.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -37,13 +37,13 @@ Metrics/BlockLength:
37
37
  Metrics/CyclomaticComplexity:
38
38
  Max: 8
39
39
 
40
- # Offense count: 42
40
+ # Offense count: 43
41
41
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
42
42
  # URISchemes: http, https
43
43
  Metrics/LineLength:
44
44
  Max: 192
45
45
 
46
- # Offense count: 3
46
+ # Offense count: 4
47
47
  # Configuration parameters: CountComments.
48
48
  Metrics/MethodLength:
49
49
  Max: 31
@@ -57,11 +57,15 @@ Metrics/ModuleLength:
57
57
  Metrics/PerceivedComplexity:
58
58
  Max: 10
59
59
 
60
- # Offense count: 2
60
+ # Offense count: 6
61
61
  Style/Documentation:
62
62
  Exclude:
63
63
  - 'spec/**/*'
64
64
  - 'test/**/*'
65
+ - 'example/artist.rb'
66
+ - 'example/artist_artwork_price.rb'
67
+ - 'example/artist_artwork_price_mr.rb'
68
+ - 'example/artwork.rb'
65
69
  - 'lib/mongoid-collection-snapshot.rb'
66
70
 
67
71
  # Offense count: 1
@@ -1,3 +1,12 @@
1
+ ### 1.3.2 (Next)
2
+
3
+ * Your contribution here.
4
+
5
+ ### 1.3.1 (5/22/2017)
6
+
7
+ * [#11](https://github.com/mongoid/mongoid-collection-snapshot/pull/11): Fix: incorrect connection used with Mongoid 6 - [@dblock](https://github.com/dblock).
8
+ * [#8](https://github.com/mongoid/mongoid-collection-snapshot/pull/8): Lock mongoid-compatibility at 0.4.0 or newer - [@dblock](https://github.com/dblock).
9
+
1
10
  ### 1.3.0 (1/20/2017)
2
11
 
3
12
  * [#2](https://github.com/mongoid/mongoid-collection-snapshot/pull/2): Support for Mongoid 6 - [@dblock](https://github.com/dblock).
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011-2017 Art.sy Inc.
1
+ Copyright (c) 2011-2017 Art.sy Inc. and Contributors
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -8,30 +8,27 @@ This is a forked, renamed, maintained and supported version of [mongoid_collecti
8
8
  [![Gem Version](https://badge.fury.io/rb/mongoid-collection-snapshot.svg)](https://badge.fury.io/rb/mongoid-collection-snapshot)
9
9
  [![Build Status](https://travis-ci.org/mongoid/mongoid-collection-snapshot.svg)](https://travis-ci.org/mongoid/mongoid-collection-snapshot)
10
10
 
11
- Quick example:
12
- --------------
11
+ ### Example
13
12
 
14
13
  Suppose that you have a Mongoid model called `Artwork`, stored in a MongoDB collection called `artworks` and the underlying documents look something like:
15
14
 
16
- { name: 'Flowers', artist: 'Andy Warhol', price: 3000000 }
15
+ { name: 'Flowers', artist_id: ..., price: 3000000 }
17
16
 
18
- From time to time, your system runs a map/reduce job to compute the average price of each artist's works, resulting in a collection called `artist_average_price` that contains documents that look like:
17
+ From time to time, your system runs a map/reduce job to compute the total price of all artist's works, resulting in a collection called `artist_artwork_price` that contains documents that look like:
19
18
 
20
- { _id: { artist: 'Andy Warhol' }, value: { price: 1500000 } }
19
+ { _id: ..., artist_id: ..., sum: 1500000 }
21
20
 
22
- If your system wants to maintain and use this average price data, it has to do so at the level of raw MongoDB operations, since map/reduce result documents don't map well to models in Mongoid.
23
- Furthermore, even though map/reduce jobs can take some time to run, you probably want the entire `artist_average_price` collection populated atomically from the point of view of your system, since otherwise you don't ever know the state of the data in the collection - you could access it in the middle of a map/reduce and get partial, incorrect results.
21
+ If your system wants to maintain and use this price data, it has to do so at the level of raw MongoDB operations, since map/reduce result documents don't map well to models in Mongoid. Furthermore, even though map/reduce jobs can take some time to run, you probably want the entire `artist_artwork_price` collection populated atomically from the point of view of your system, since otherwise you don't ever know the state of the data in the collection - you could access it in the middle of a map/reduce and get partial, incorrect results.
24
22
 
25
23
  A mongoid-collection-snapshot solves this problem by providing an atomic view of collections of data like map/reduce results that live outside of Mongoid.
26
24
 
27
- In the example above, we'd set up our average artist price collection like:
25
+ In the example below, we set up our artist price sum collection by including `Mongoid::CollectionSnapshot` and implementing a `build` method.
28
26
 
29
27
  ``` ruby
30
- class AverageArtistPrice
28
+ class ArtistArtworkPrice
31
29
  include Mongoid::CollectionSnapshot
32
30
 
33
31
  def build
34
-
35
32
  map = <<-EOS
36
33
  function() {
37
34
  emit({ artist_id: this['artist_id']}, { count: 1, sum: this['price'] })
@@ -59,35 +56,27 @@ class AverageArtistPrice
59
56
  end
60
57
  end
61
58
  end
62
-
63
59
  ```
64
60
 
65
- Now, if you want to schedule a recomputation, just call `AverageArtistPrice.create`. You can define other methods on collection snapshots.
61
+ Now, if you want to schedule a recomputation, just call `ArtistArtworkPrice.create`.
66
62
 
67
- ```ruby
68
- class AverageArtistPrice
69
- ...
63
+ The latest snapshot is always available as `ArtistArtworkPrice.latest`.
70
64
 
71
- def average_price(artist_name)
72
- artist = Artist.where(name: artist_name).first
73
- doc = collection_snapshot.where(artist_id: artist.id).first
74
- doc['sum'] / doc['count']
75
- end
76
- end
65
+ ```ruby
66
+ andy_warhol = Artist.where(name: 'Andy Warhol').first
67
+ andy_warhol_price = ArtistArtworkPrice.latest.collection_snapshot.where(artist_id: andy_warhol.id).first
68
+ average_price = andy_warhol_price['sum'] / andy_warhol_price['count']
77
69
  ```
78
70
 
79
- The latest snapshot is always available as `AverageArtistPrice.latest`, so you can write code like:
71
+ ### Snapshot Cleanup
72
+
73
+ By default, mongoid-collection-snapshot maintains the most recent two snapshots computed any given time. Set `max_collection_snapshot_instances` to change this.
80
74
 
81
75
  ```ruby
82
- warhol_expected_price = AverageArtistPrice.latest.average_price('Andy Warhol')
76
+ ArtistArtworkPrice.max_collection_snapshot_instances = 3
83
77
  ```
84
78
 
85
- And always be sure that you'll never be looking at partial results. The only thing you need to do to hook into mongoid-collection-snapshot is implement the method `build`, which populates the collection snapshot and any indexes you need.
86
-
87
- By default, mongoid-collection-snapshot maintains the most recent two snapshots computed any given time.
88
-
89
- ery Snapshot Data with Mongoid
90
- --------------------------------
79
+ ### Query Snapshot Data with Mongoid
91
80
 
92
81
  You can do better than the average price example above and define first-class models for your collection snapshot data, then access them as any other Mongoid collection via collection snapshot's `.documents` method.
93
82
 
@@ -107,7 +96,7 @@ class AverageArtistPrice
107
96
  end
108
97
  ```
109
98
 
110
- Another example iterates through all latest artist price averages.
99
+ The following example iterates through all latest artist price averages.
111
100
 
112
101
  ```ruby
113
102
  AverageArtistPrice.latest.documents.each do |doc|
@@ -115,19 +104,18 @@ AverageArtistPrice.latest.documents.each do |doc|
115
104
  end
116
105
  ```
117
106
 
118
- Multi-collection snapshots
119
- --------------------------
107
+ This code can be found in the [example](example) folder.
108
+
109
+ ### Multi-Collection Snapshots
120
110
 
121
111
  You can maintain multiple collections atomically within the same snapshot by passing unique collection identifiers to `collection_snaphot` when you call it in your build or query methods:
122
112
 
123
113
  ``` ruby
124
- class ArtistStats
114
+ class ArtistArtworkPrice
125
115
  include Mongoid::CollectionSnapshot
126
116
 
127
117
  def build
128
- # ...
129
118
  # define map/reduce for average and max aggregations
130
- # ...
131
119
  Mongoid.default_session.command('mapreduce' => 'artworks', map: map_avg, reduce: reduce_avg, out: collection_snapshot('average'))
132
120
  Mongoid.default_session.command('mapreduce' => 'artworks', map: map_max, reduce: reduce_max, out: collection_snapshot('max'))
133
121
  end
@@ -147,7 +135,7 @@ end
147
135
  Specify the name of the collection to define first class Mongoid models.
148
136
 
149
137
  ```ruby
150
- class ArtistStats
138
+ class ArtistArtworkPrice
151
139
  document('average') do
152
140
  field :value, type: Hash
153
141
  end
@@ -161,27 +149,26 @@ end
161
149
  Access these by name.
162
150
 
163
151
  ```ruby
164
- ArtistStats.latest.documents('average')
165
- ArtistStats.latest.documents('max')
152
+ ArtistArtworkPrice.latest.documents('average')
153
+ ArtistArtworkPrice.latest.documents('max')
166
154
  ```
167
155
 
168
156
  If fields across multiple collection snapshots are identical, a single default `document` is sufficient.
169
157
 
170
158
  ```ruby
171
- class ArtistStats
159
+ class ArtistArtworkPrice
172
160
  document do
173
161
  field :value, type: Hash
174
162
  end
175
163
  end
176
164
  ```
177
165
 
178
- Custom database connections
179
- ---------------------------
166
+ ### Custom Database Connections
180
167
 
181
168
  Your class can specify a custom database for storage of collection snapshots by overriding the `snapshot_session` instance method. In this example, we memoize the connection at the class level to avoid creating many separate connection instances.
182
169
 
183
170
  ```ruby
184
- class ArtistStats
171
+ class ArtistArtworkPrice
185
172
  include Mongoid::CollectionSnapshot
186
173
 
187
174
  def build
@@ -189,13 +176,7 @@ class ArtistStats
189
176
  end
190
177
 
191
178
  def snapshot_session
192
- self.class.snapshot_session
193
- end
194
-
195
- def self.snapshot_session
196
- @@snapshot_session ||= Mongo::Client.new('mongodb://localhost:27017').tap do |c|
197
- c.use :alternate_db
198
- end
179
+ @snapshot_session ||= Mongo::Client.new('mongodb://localhost:27017/artists_and_artworks')
199
180
  end
200
181
  end
201
182
  ```
@@ -212,12 +193,13 @@ development:
212
193
  ```
213
194
 
214
195
  ```ruby
215
- def snapshot_session
216
- Mongoid.session('imports')
217
- end
196
+ def snapshot_session
197
+ Mongoid.session('imports')
198
+ end
218
199
  ```
219
200
 
220
- License
221
- =======
201
+ ### License
202
+
203
+ Copyright (c) 2011-2017 Art.sy Inc. and Contributors
222
204
 
223
205
  MIT License, see [LICENSE.txt](LICENSE.txt) for details.
@@ -0,0 +1,68 @@
1
+ Releasing Mongoid::CollectionSnapshot
2
+ =====================================
3
+
4
+ There're no particular rules about when to release mongoid-collection-snapshot. Release bug fixes frequently, features not so frequently and breaking API changes rarely.
5
+
6
+ ### Release
7
+
8
+ Run tests, check that all tests succeed locally.
9
+
10
+ ```
11
+ bundle install
12
+ rake
13
+ ```
14
+
15
+ Check that the last build succeeded in [Travis CI](https://travis-ci.org/mongoid/mongoid-collection-snapshot) for all supported platforms.
16
+
17
+ Increment the version, modify [lib/mongoid/collection-snapshot/version.rb](lib/mongoid/collection-snapshot/version.rb).
18
+
19
+ * Increment the third number if the release has bug fixes and/or very minor features, only (eg. change `0.5.1` to `0.5.2`).
20
+ * Increment the second number if the release contains major features or breaking API changes (eg. change `0.5.1` to `0.4.0`).
21
+
22
+ Change "Next Release" in [CHANGELOG.md](CHANGELOG.md) to the new version.
23
+
24
+ ```
25
+ ### 0.4.0 (2014-01-27)
26
+ ```
27
+
28
+ Remove the line with "Your contribution here.", since there will be no more contributions to this release.
29
+
30
+ Commit your changes.
31
+
32
+ ```
33
+ git add CHANGELOG.md lib/mongoid-collection-snapshot/version.rb
34
+ git commit -m "Preparing for release, 0.4.0."
35
+ git push origin master
36
+ ```
37
+
38
+ Release.
39
+
40
+ ```
41
+ $ rake release
42
+
43
+ mongoid-collection-snapshot 0.4.0 built to pkg/mongoid-collection-snapshot-0.4.0.gem.
44
+ Tagged v0.4.0.
45
+ Pushed git commits and tags.
46
+ Pushed mongoid-collection-snapshot 0.4.0 to rubygems.org.
47
+ ```
48
+
49
+ ### Prepare for the Next Version
50
+
51
+ Add the next release to [CHANGELOG.md](CHANGELOG.md).
52
+
53
+ ```
54
+ Next Release
55
+ ============
56
+
57
+ * Your contribution here.
58
+ ```
59
+
60
+ Increment the minor version, modify [lib/mongoid-collection-snapshot/version.rb](lib/mongoid-collection-snapshot/version.rb).
61
+
62
+ Commit your changes.
63
+
64
+ ```
65
+ git add CHANGELOG.md lib/mongoid-collection-snapshot/version.rb
66
+ git commit -m "Preparing for next release, 0.4.1."
67
+ git push origin master
68
+ ```
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'faker'
4
+ gem 'mongoid'
5
+ gem 'mongoid-collection-snapshot', path: '../'
@@ -0,0 +1,6 @@
1
+ class Artist
2
+ include Mongoid::Document
3
+
4
+ field :name, type: String
5
+ has_many :artworks
6
+ end
@@ -0,0 +1,19 @@
1
+ class ArtistArtworkPrice
2
+ include Mongoid::CollectionSnapshot
3
+
4
+ document do
5
+ field :count, type: Integer
6
+ field :sum, type: Integer
7
+ belongs_to :artist, inverse_of: nil
8
+ end
9
+
10
+ def build
11
+ Artist.all.each do |artist|
12
+ collection_snapshot.insert_one(
13
+ artist_id: artist.id,
14
+ count: artist.artworks.count,
15
+ sum: artist.artworks.sum(&:price)
16
+ )
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ class AverageArtistPriceMR
2
+ include Mongoid::CollectionSnapshot
3
+
4
+ document do
5
+ field :count, type: Integer
6
+ field :sum, type: Integer
7
+ belongs_to :artist, inverse_of: nil
8
+ end
9
+
10
+ def build
11
+ map = <<-EOS
12
+ function() {
13
+ emit({ artist_id: this['artist_id']}, { count: 1, sum: this['price'] })
14
+ }
15
+ EOS
16
+
17
+ reduce = <<-EOS
18
+ function(key, values) {
19
+ var sum = 0;
20
+ var count = 0;
21
+ values.forEach(function(value) {
22
+ sum += value['sum'];
23
+ count += value['count'];
24
+ });
25
+ return({ count: count, sum: sum });
26
+ }
27
+ EOS
28
+
29
+ Artwork.map_reduce(map, reduce).out(inline: 1).each do |doc|
30
+ collection_snapshot.insert_one(
31
+ artist_id: doc['_id']['artist_id'],
32
+ count: doc['value']['count'],
33
+ sum: doc['value']['sum']
34
+ )
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ class Artwork
2
+ include Mongoid::Document
3
+
4
+ field :title, type: String
5
+ field :price, type: Integer
6
+
7
+ belongs_to :artist
8
+ end
@@ -0,0 +1,44 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'bundler/setup'
4
+ Bundler.require
5
+
6
+ require 'artist'
7
+ require 'artwork'
8
+
9
+ require 'artist_artwork_price'
10
+ require 'artist_artwork_price_mr'
11
+
12
+ Mongoid.configure do |config|
13
+ config.connect_to('mongoid-collection-snapshot_example')
14
+ end
15
+
16
+ Mongoid.logger.level = Logger::INFO
17
+ Mongo::Logger.logger.level = Logger::INFO
18
+
19
+ Mongoid.purge!
20
+
21
+ 3.times do
22
+ Artist.create!(name: Faker::Name.name)
23
+ end
24
+
25
+ 3.times do
26
+ puts 'Creating more artworks ...'
27
+ Artist.all.each do |artist|
28
+ rand(10).times do
29
+ Artwork.create!(
30
+ artist: artist,
31
+ title: Faker::Hipster.sentence,
32
+ price: Faker::Number.between(1, 1000)
33
+ )
34
+ end
35
+ end
36
+
37
+ snapshot = ArtistArtworkPrice.create # AverageArtistPriceMR.create
38
+
39
+ snapshot.documents.each do |row|
40
+ puts " the total price of #{row.count} artwork(s) by #{row.artist.name} is $#{row.sum}"
41
+ end
42
+ end
43
+
44
+ Mongoid.purge!
@@ -44,7 +44,8 @@ module Mongoid
44
44
  store_in collection: collection_name
45
45
  end
46
46
  if Mongoid::Compatibility::Version.mongoid6?
47
- PersistenceContext.set(klass, database: snapshot_session.database.name)
47
+ ctx = PersistenceContext.set(klass, {})
48
+ ctx.instance_variable_set(:@client, snapshot_session)
48
49
  elsif Mongoid::Compatibility::Version.mongoid5?
49
50
  klass.mongo_client = snapshot_session
50
51
  else
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module CollectionSnapshot
3
- VERSION = '1.3.0'.freeze
3
+ VERSION = '1.3.1'.freeze
4
4
  end
5
5
  end
@@ -14,6 +14,6 @@ Gem::Specification.new do |s|
14
14
  s.licenses = ['MIT']
15
15
  s.summary = 'Easy maintenence of collections of processed data in MongoDB with the Mongoid ODM.'
16
16
  s.add_dependency 'mongoid', '>= 3.0'
17
- s.add_dependency 'mongoid-compatibility'
17
+ s.add_dependency 'mongoid-compatibility', '>= 0.4.0'
18
18
  s.add_dependency 'mongoid-slug'
19
19
  end
@@ -154,9 +154,7 @@ module Mongoid
154
154
 
155
155
  context '#documents' do
156
156
  it 'uses the custom session' do
157
- if Mongoid::Compatibility::Version.mongoid6?
158
- expect(CustomConnectionSnapshot.new.documents.database_name).to eq :snapshot_test
159
- elsif Mongoid::Compatibility::Version.mongoid5?
157
+ if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6?
160
158
  expect(CustomConnectionSnapshot.new.documents.mongo_client).to eq CustomConnectionSnapshot.snapshot_session
161
159
  else
162
160
  expect(CustomConnectionSnapshot.new.documents.mongo_session).to eq CustomConnectionSnapshot.snapshot_session
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-collection-snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Windsor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-20 00:00:00.000000000 Z
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 0.4.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 0.4.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mongoid-slug
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -68,8 +68,15 @@ files:
68
68
  - Gemfile
69
69
  - LICENSE.txt
70
70
  - README.md
71
+ - RELEASING.md
71
72
  - Rakefile
72
73
  - UPGRADING.md
74
+ - example/Gemfile
75
+ - example/artist.rb
76
+ - example/artist_artwork_price.rb
77
+ - example/artist_artwork_price_mr.rb
78
+ - example/artwork.rb
79
+ - example/example.rb
73
80
  - lib/mongoid-collection-snapshot.rb
74
81
  - lib/mongoid-collection-snapshot/version.rb
75
82
  - mongoid-collection-snapshot.gemspec
@@ -101,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
108
  version: 1.3.6
102
109
  requirements: []
103
110
  rubyforge_project:
104
- rubygems_version: 2.5.1
111
+ rubygems_version: 2.6.11
105
112
  signing_key:
106
113
  specification_version: 4
107
114
  summary: Easy maintenence of collections of processed data in MongoDB with the Mongoid