mongoid_collection_snapshot 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 918809b176c7a1430e76767c00aa8e3f59114a68
4
+ data.tar.gz: 7cfdc2500575d070f4f525ff84b17f37065477c9
5
+ SHA512:
6
+ metadata.gz: f13bc196f4e0e6544cd9dab28715cd3d1fd3fa8a4017daf82488212653a6827869784dbbefd3df4c5307bdabce4349e2fe9b99c9a71df105517e06a73220052b
7
+ data.tar.gz: abfbdd42a184eb2e2bc8482b3efd9b8aeff462bfe487b1e5fbc1a797d37b2c9ac5f6f8b809467431828eb4684a84bebeec0a633a6b2d5beaeda2710ffc344fcb
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ services:
2
+ - mongodb
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+
9
+ env:
10
+ - MONGOID_VERSION=3
11
+ - MONGOID_VERSION=4
12
+
13
+ language: ruby
14
+
15
+ cache: bundler
data/CHANGELOG.md CHANGED
@@ -1,11 +1,24 @@
1
- Next Release (0.2.0)
2
- ====================
1
+ Next Release
2
+ ------------
3
+
4
+ * Expose `snapshot_session` for custom snapshot storage - [@joeyAghion](https://github.com/joeyAghion)
5
+ * Compatibility with Mongoid 4.x - [@dblock](https://github.com/dblock).
6
+
7
+ 0.2.0
8
+ -----
9
+
10
+ Important note for those upgrading from 0.1.0 (pre-Mongoid 3.0) to 0.2.0 (Mongoid 3.x): you'll need to upgrade any
11
+ existing snapshots created by mongoid_collection_snapshot 0.1.0 in your database before they're usable. You can
12
+ do this by renaming the 'workspace_slug' attribute to 'slug' in MongoDB after upgrading. For example,
13
+ to upgrade the snapshot class "MySnapshot", you'd enter the following at the mongo shell:
14
+
15
+ db.my_snapshot.rename({'workspace_slug': {'$exists': true}}, {'$rename': {'workspace_slug': 'slug'}})
3
16
 
4
17
  * Added ability to maintain a snapshot of multiple collections atomically - [@aaw](https://github.com/aaw).
5
18
  * Added support for [Mongoid 3.0](https://github.com/mongoid/mongoid) - [@dblock](https://github.com/dblock).
6
19
  * Relaxed version limitations of [mongoid_slug](https://github.com/digitalplaywright/mongoid-slug) - [@dblock](https://github.com/dblock).
7
20
 
8
- Version 0.1.0
9
- =============
21
+ 0.1.0
22
+ -----
10
23
 
11
24
  * Initial public release - [@aaw](https://github.com/aaw).
data/Gemfile CHANGED
@@ -1,10 +1,18 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "mongoid", "~> 3.0"
3
+ case version = ENV['MONGOID_VERSION'] || '~> 3.1'
4
+ when /4/
5
+ gem 'mongoid', github: 'mongoid/mongoid'
6
+ when /3/
7
+ gem 'mongoid', '~> 3.1'
8
+ else
9
+ gem 'mongoid', version
10
+ end
11
+
4
12
  gem "mongoid_slug"
5
13
 
6
14
  group :development do
7
- gem "mongoid", "~> 3.0"
8
15
  gem "rspec", "~> 2.11.0"
9
16
  gem "jeweler", "~> 1.8.4"
10
17
  end
18
+
data/README.md CHANGED
@@ -3,11 +3,13 @@ Mongoid Collection Snapshot
3
3
 
4
4
  Easy maintenance of collections of processed data in MongoDB with the Mongoid 3.x ODM.
5
5
 
6
+ [![Build Status](https://travis-ci.org/aaw/mongoid_collection_snapshot.svg)](https://travis-ci.org/aaw/mongoid_collection_snapshot)
7
+
6
8
  Quick example:
7
9
  --------------
8
10
 
9
11
  Suppose that you have a Mongoid model called `Artwork`, stored
10
- in a MongoDB collection called `artworks` and the underlying documents
12
+ in a MongoDB collection called `artworks` and the underlying documents
11
13
  look something like:
12
14
 
13
15
  { name: 'Flowers', artist: 'Andy Warhol', price: 3000000 }
@@ -18,10 +20,10 @@ average price of each artist's works, resulting in a collection called
18
20
 
19
21
  { _id: { artist: 'Andy Warhol'}, value: { price: 1500000 } }
20
22
 
21
- If your system wants to maintain and use this average price data, it has
23
+ If your system wants to maintain and use this average price data, it has
22
24
  to do so at the level of raw MongoDB operations, since
23
25
  map/reduce result documents don't map well to models in Mongoid.
24
- Furthermore, even though map/reduce jobs can take some time to run, you probably
26
+ Furthermore, even though map/reduce jobs can take some time to run, you probably
25
27
  want the entire `artist_average_price` collection populated atomically
26
28
  from the point of view of your system, since otherwise you don't ever
27
29
  know the state of the data in the collection - you could access it in
@@ -29,7 +31,7 @@ the middle of a map/reduce and get partial, incorrect results.
29
31
 
30
32
  mongoid_collection_snapshot solves this problem by providing an atomic
31
33
  view of collections of data like map/reduce results that live outside
32
- of Mongoid.
34
+ of Mongoid.
33
35
 
34
36
  In the example above, we'd set up our average artist price collection like:
35
37
 
@@ -83,14 +85,14 @@ And always be sure that you'll never be looking at partial results. The only
83
85
  thing you need to do to hook into mongoid_collection_snapshot is implement the
84
86
  method `build`, which populates the collection snapshot and any indexes you need.
85
87
 
86
- By default, mongoid_collection_snapshot maintains the most recent two snapshots
88
+ By default, mongoid_collection_snapshot maintains the most recent two snapshots
87
89
  computed any given time.
88
90
 
89
- Other features
90
- --------------
91
+ Multi-collection snapshots
92
+ --------------------------
91
93
 
92
94
  You can maintain multiple collections atomically within the same snapshot by
93
- passing unique collection identifiers to ``collection_snaphot`` when you call it
95
+ passing unique collection identifiers to ``collection_snaphot`` when you call it
94
96
  in your build or query methods:
95
97
 
96
98
  ``` ruby
@@ -117,6 +119,48 @@ class ArtistStats
117
119
  end
118
120
  ```
119
121
 
122
+ Custom database connections
123
+ ---------------------------
124
+
125
+ 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.
126
+
127
+ ```ruby
128
+ class ArtistStats
129
+ include Mongoid::CollectionSnapshot
130
+
131
+ def build
132
+ # ...
133
+ end
134
+
135
+ def snapshot_session
136
+ self.class.snapshot_session
137
+ end
138
+
139
+ def self.snapshot_session
140
+ @@snapshot_session ||= Moped::Session.new(['127.0.0.1:27017']).tap do |s|
141
+ s.use :alternate_db
142
+ end
143
+ end
144
+ end
145
+ ```
146
+
147
+ Another common way of configuring this is through mongoid.yml.
148
+
149
+ ```yaml
150
+ development:
151
+ sessions:
152
+ default:
153
+ database: dev_data
154
+ imports:
155
+ database: dev_imports
156
+ ```
157
+
158
+ ```ruby
159
+ def snapshot_session
160
+ Mongoid.session('imports')
161
+ end
162
+ ```
163
+
120
164
  License
121
165
  =======
122
166
 
data/Rakefile CHANGED
@@ -25,11 +25,11 @@ Jeweler::Tasks.new do |gem|
25
25
  end
26
26
  Jeweler::RubygemsDotOrgTasks.new
27
27
 
28
- require 'rake/testtask'
29
- Rake::TestTask.new(:test) do |test|
30
- test.libs << 'lib' << 'test'
31
- test.pattern = 'test/**/test_*.rb'
32
- test.verbose = true
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ spec.rspec_opts = "--color --format progress"
33
33
  end
34
34
 
35
- task :default => :test
35
+ task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 1.0.0
@@ -13,8 +13,8 @@ module Mongoid::CollectionSnapshot
13
13
 
14
14
  field :max_collection_snapshot_instances, default: 2
15
15
 
16
- before_save :build
17
- after_save :ensure_at_most_two_instances_exist
16
+ before_create :build
17
+ after_create :ensure_at_most_two_instances_exist
18
18
  before_destroy :drop_snapshot_collections
19
19
  end
20
20
 
@@ -26,14 +26,14 @@ module Mongoid::CollectionSnapshot
26
26
 
27
27
  def collection_snapshot(name=nil)
28
28
  if name
29
- Mongoid.default_session["#{self.collection.name}.#{name}.#{slug}"]
29
+ snapshot_session["#{self.collection.name}.#{name}.#{slug}"]
30
30
  else
31
- Mongoid.default_session["#{self.collection.name}.#{slug}"]
31
+ snapshot_session["#{self.collection.name}.#{slug}"]
32
32
  end
33
33
  end
34
34
 
35
35
  def drop_snapshot_collections
36
- Mongoid.default_session.collections.each do |collection|
36
+ snapshot_session.collections.each do |collection|
37
37
  collection.drop if collection.name =~ /^#{self.collection.name}\.([^\.]+\.)?#{slug}$/
38
38
  end
39
39
  end
@@ -48,5 +48,10 @@ module Mongoid::CollectionSnapshot
48
48
  all_instances[self.max_collection_snapshot_instances..-1].each { |instance| instance.destroy }
49
49
  end
50
50
  end
51
+
52
+ # Override to supply custom database connection for snapshots
53
+ def snapshot_session
54
+ Mongoid.default_session
55
+ end
51
56
 
52
57
  end
@@ -2,14 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
+ # stub: mongoid_collection_snapshot 1.0.0 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
8
  s.name = "mongoid_collection_snapshot"
8
- s.version = "0.2.0"
9
+ s.version = "1.0.0"
9
10
 
10
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
11
13
  s.authors = ["Aaron Windsor"]
12
- s.date = "2012-11-04"
14
+ s.date = "2014-06-07"
13
15
  s.description = "Easy maintenence of collections of processed data in MongoDB with the Mongoid ODM"
14
16
  s.email = "aaron.windsor@gmail.com"
15
17
  s.extra_rdoc_files = [
@@ -17,6 +19,7 @@ Gem::Specification.new do |s|
17
19
  "README.md"
18
20
  ]
19
21
  s.files = [
22
+ ".travis.yml",
20
23
  "CHANGELOG.md",
21
24
  "Gemfile",
22
25
  "LICENSE.txt",
@@ -27,36 +30,33 @@ Gem::Specification.new do |s|
27
30
  "mongoid_collection_snapshot.gemspec",
28
31
  "spec/models/artwork.rb",
29
32
  "spec/models/average_artist_price.rb",
33
+ "spec/models/custom_connection_snapshot.rb",
30
34
  "spec/models/multi_collection_snapshot.rb",
31
35
  "spec/mongoid/collection_snapshot_spec.rb",
32
36
  "spec/spec_helper.rb"
33
37
  ]
34
38
  s.homepage = "http://github.com/aaw/mongoid_collection_snapshot"
35
39
  s.licenses = ["MIT"]
36
- s.require_paths = ["lib"]
37
- s.rubygems_version = "1.8.24"
40
+ s.rubygems_version = "2.2.2"
38
41
  s.summary = "Easy maintenence of collections of processed data in MongoDB with the Mongoid ODM"
39
42
 
40
43
  if s.respond_to? :specification_version then
41
- s.specification_version = 3
44
+ s.specification_version = 4
42
45
 
43
46
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
- s.add_runtime_dependency(%q<mongoid>, ["~> 3.0"])
47
+ s.add_runtime_dependency(%q<mongoid>, ["~> 3.1"])
45
48
  s.add_runtime_dependency(%q<mongoid_slug>, [">= 0"])
46
- s.add_development_dependency(%q<mongoid>, ["~> 3.0"])
47
49
  s.add_development_dependency(%q<rspec>, ["~> 2.11.0"])
48
50
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
49
51
  else
50
- s.add_dependency(%q<mongoid>, ["~> 3.0"])
52
+ s.add_dependency(%q<mongoid>, ["~> 3.1"])
51
53
  s.add_dependency(%q<mongoid_slug>, [">= 0"])
52
- s.add_dependency(%q<mongoid>, ["~> 3.0"])
53
54
  s.add_dependency(%q<rspec>, ["~> 2.11.0"])
54
55
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
55
56
  end
56
57
  else
57
- s.add_dependency(%q<mongoid>, ["~> 3.0"])
58
+ s.add_dependency(%q<mongoid>, ["~> 3.1"])
58
59
  s.add_dependency(%q<mongoid_slug>, [">= 0"])
59
- s.add_dependency(%q<mongoid>, ["~> 3.0"])
60
60
  s.add_dependency(%q<rspec>, ["~> 2.11.0"])
61
61
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
62
62
  end
@@ -0,0 +1,18 @@
1
+ class CustomConnectionSnapshot
2
+ include Mongoid::CollectionSnapshot
3
+
4
+ def self.snapshot_session
5
+ @@snapshot_session ||= Moped::Session.new(['127.0.0.1:27017']).tap do |session|
6
+ session.use :snapshot_test
7
+ end
8
+ end
9
+
10
+ def snapshot_session
11
+ self.class.snapshot_session
12
+ end
13
+
14
+ def build
15
+ collection_snapshot.insert('name' => 'foo')
16
+ collection_snapshot('foo').insert({'name' => 'bar'})
17
+ end
18
+ end
@@ -4,7 +4,7 @@ module Mongoid
4
4
  describe CollectionSnapshot do
5
5
 
6
6
  context "creating a basic snapshot" do
7
-
7
+
8
8
  let!(:flowers) { Artwork.create(:name => 'Flowers', :artist => 'Andy Warhol', :price => 3000000) }
9
9
  let!(:guns) { Artwork.create(:name => 'Guns', :artist => 'Andy Warhol', :price => 1000000) }
10
10
  let!(:vinblastine) { Artwork.create(:name => 'Vinblastine', :artist => 'Damien Hirst', :price => 1500000) }
@@ -31,7 +31,7 @@ module Mongoid
31
31
  AverageArtistPrice.latest.should == second
32
32
  sleep(1)
33
33
  third = AverageArtistPrice.create
34
- AverageArtistPrice.latest.should == third
34
+ AverageArtistPrice.latest.should == third
35
35
  end
36
36
 
37
37
  it "should only maintain at most two of the latest snapshots to support its calculations" do
@@ -45,7 +45,7 @@ module Mongoid
45
45
  end
46
46
 
47
47
  context "creating a snapshot containing multiple collections" do
48
-
48
+
49
49
  it "populates several collections and allows them to be queried" do
50
50
  MultiCollectionSnapshot.latest.should be_nil
51
51
  10.times { MultiCollectionSnapshot.create }
@@ -76,5 +76,26 @@ module Mongoid
76
76
 
77
77
  end
78
78
 
79
+ context "with a custom snapshot connection" do
80
+
81
+ around(:each) do |example|
82
+ CustomConnectionSnapshot.snapshot_session.drop
83
+ example.run
84
+ CustomConnectionSnapshot.snapshot_session.drop
85
+ end
86
+
87
+ it "builds snapshot in custom database" do
88
+ snapshot = CustomConnectionSnapshot.create
89
+ [
90
+ "#{CustomConnectionSnapshot.collection.name}.foo.#{snapshot.slug}",
91
+ "#{CustomConnectionSnapshot.collection.name}.#{snapshot.slug}"
92
+ ].each do |collection_name|
93
+ Mongoid.default_session[collection_name].find.count.should == 0
94
+ CustomConnectionSnapshot.snapshot_session[collection_name].find.count.should == 1
95
+ end
96
+ end
97
+
98
+ end
99
+
79
100
  end
80
101
  end
metadata CHANGED
@@ -1,94 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_collection_snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Aaron Windsor
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-04 00:00:00.000000000 Z
11
+ date: 2014-06-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: mongoid
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
- version: '3.0'
19
+ version: '3.1'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
- version: '3.0'
26
+ version: '3.1'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: mongoid_slug
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
- - !ruby/object:Gem::Dependency
47
- name: mongoid
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: '3.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '3.0'
62
41
  - !ruby/object:Gem::Dependency
63
42
  name: rspec
64
43
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
44
  requirements:
67
- - - ~>
45
+ - - "~>"
68
46
  - !ruby/object:Gem::Version
69
47
  version: 2.11.0
70
48
  type: :development
71
49
  prerelease: false
72
50
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
51
  requirements:
75
- - - ~>
52
+ - - "~>"
76
53
  - !ruby/object:Gem::Version
77
54
  version: 2.11.0
78
55
  - !ruby/object:Gem::Dependency
79
56
  name: jeweler
80
57
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
58
  requirements:
83
- - - ~>
59
+ - - "~>"
84
60
  - !ruby/object:Gem::Version
85
61
  version: 1.8.4
86
62
  type: :development
87
63
  prerelease: false
88
64
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
65
  requirements:
91
- - - ~>
66
+ - - "~>"
92
67
  - !ruby/object:Gem::Version
93
68
  version: 1.8.4
94
69
  description: Easy maintenence of collections of processed data in MongoDB with the
@@ -100,6 +75,7 @@ extra_rdoc_files:
100
75
  - LICENSE.txt
101
76
  - README.md
102
77
  files:
78
+ - ".travis.yml"
103
79
  - CHANGELOG.md
104
80
  - Gemfile
105
81
  - LICENSE.txt
@@ -110,36 +86,33 @@ files:
110
86
  - mongoid_collection_snapshot.gemspec
111
87
  - spec/models/artwork.rb
112
88
  - spec/models/average_artist_price.rb
89
+ - spec/models/custom_connection_snapshot.rb
113
90
  - spec/models/multi_collection_snapshot.rb
114
91
  - spec/mongoid/collection_snapshot_spec.rb
115
92
  - spec/spec_helper.rb
116
93
  homepage: http://github.com/aaw/mongoid_collection_snapshot
117
94
  licenses:
118
95
  - MIT
96
+ metadata: {}
119
97
  post_install_message:
120
98
  rdoc_options: []
121
99
  require_paths:
122
100
  - lib
123
101
  required_ruby_version: !ruby/object:Gem::Requirement
124
- none: false
125
102
  requirements:
126
- - - ! '>='
103
+ - - ">="
127
104
  - !ruby/object:Gem::Version
128
105
  version: '0'
129
- segments:
130
- - 0
131
- hash: -1957011938527181136
132
106
  required_rubygems_version: !ruby/object:Gem::Requirement
133
- none: false
134
107
  requirements:
135
- - - ! '>='
108
+ - - ">="
136
109
  - !ruby/object:Gem::Version
137
110
  version: '0'
138
111
  requirements: []
139
112
  rubyforge_project:
140
- rubygems_version: 1.8.24
113
+ rubygems_version: 2.2.2
141
114
  signing_key:
142
- specification_version: 3
115
+ specification_version: 4
143
116
  summary: Easy maintenence of collections of processed data in MongoDB with the Mongoid
144
117
  ODM
145
118
  test_files: []