mongo_sequence 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a559f5d439899909817699b837df01a1f1c368ee
4
+ data.tar.gz: 2dd3442ea597fc169ef47ce2138ba305aaf53b8c
5
+ SHA512:
6
+ metadata.gz: ce2adc1cf375ac0aada73a5699d04ff464152ee5ffb9aa001b2f55dee6e823ce77a204fcdd45dea2148db76fd9032e1b2f8228e7b20d1071ae37ef0c5413f8ca
7
+ data.tar.gz: 99c7239b7be59ffee5058fe1990221a829ff94f098823e8dc44bf90f734225dca8e9256d24c8a5b5556bb25687aa534fffe3bd5eddd089bf18a9401a96c414a4
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- MONGO_VERS = '1.6.1'
3
+ MONGO_VERS = '1.8.5'
4
4
 
5
5
  gem 'mongo', MONGO_VERS
6
6
  gem 'bson_ext', MONGO_VERS
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # MongoSequence
2
2
 
3
- MongoSequence provides light-weight, robust sequences in MongoDB. They are useful for auto-incrementing or counting. Compatible with any Mongo ODM.
3
+ MongoSequence provides light-weight, robust sequences in MongoDB. Such sequences are useful for auto-incrementing or counting. Compatible with any Mongo ODM.
4
4
 
5
- MongoSequence creates named sequences in a "sequences" collection in your database and atomically increments and returns the counter on them using Mongo's findAndModify command. You won't have collisions--two processes trying to increment at the same time will alway get different numbers.
5
+ MongoSequence creates named sequences in a "sequences" collection in your database and atomically increments and returns the counter on them using Mongo's [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) command. You won't have collisionstwo processes trying to increment at the same time will always get different numbers.
6
6
 
7
7
  ## Usage
8
8
 
@@ -82,6 +82,10 @@ Need something added? Please [open an issue](https://github.com/brianhempel/mong
82
82
  git push
83
83
  # then make a pull request
84
84
 
85
+ ## Inspiration
86
+
87
+ A first implimentation was written over a year ago based on Chris Shiflett's [Auto Increment with MongoDB](http://shiflett.org/blog/2010/jul/auto-increment-with-mongodb) blog post.
88
+
85
89
  ## License
86
90
 
87
91
  Authored by Brian Hempel. Public domain, no restrictions.
@@ -54,7 +54,12 @@ class MongoSequence
54
54
  :new => true, # return the modified doc
55
55
  :update => update
56
56
  }
57
- collection.find_and_modify(options)['current']
57
+ coll = collection.find_and_modify(options)
58
+ if coll
59
+ coll['current']
60
+ else
61
+ raise Mongo::OperationFailure, 'No matching object found'
62
+ end
58
63
  rescue Mongo::OperationFailure => e
59
64
  raise unless e.message =~ /No matching object found/
60
65
  init_in_database
@@ -62,6 +67,6 @@ class MongoSequence
62
67
  end
63
68
 
64
69
  def init_in_database
65
- collection.save({:_id => name, :current => 0}, :safe => true)
70
+ collection.save({:_id => name, :current => 0}, :w => 1)
66
71
  end
67
72
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "mongo_sequence"
5
- s.version = '1.0.0'
5
+ s.version = '1.0.1'
6
6
  s.authors = ["Brian Hempel"]
7
7
  s.email = ["plasticchicken@gmail.com"]
8
8
  s.homepage = "https://github.com/brianhempel/mongo_sequence"
@@ -18,5 +18,5 @@ Gem::Specification.new do |s|
18
18
 
19
19
  s.add_development_dependency "rspec"
20
20
  s.add_development_dependency "mongo_mapper"
21
- s.add_development_dependency "mongoid"
21
+ s.add_development_dependency "mongoid", "~> 2.0"
22
22
  end
@@ -14,7 +14,7 @@ describe MongoSequence do
14
14
  context "no ODM" do
15
15
  before :each do
16
16
  @db = Mongo::Connection.new.db('mongo_sequence_test')
17
- @db.collections.each(&:remove)
17
+ @db.collections.reject{|x| x.name =~ /\Asystem\./}.each(&:remove)
18
18
  MongoSequence.database = @db
19
19
  end
20
20
 
@@ -102,21 +102,37 @@ describe MongoSequence do
102
102
  MongoSequence.database = Mongo::Connection.new.db('mongo_sequence_test')
103
103
  MongoSequence.database.name.should == "mongo_sequence_test"
104
104
  end
105
+
106
+ it "starts at 1 for a new sequence and increments" do
107
+ MongoSequence[:test].current = 0
108
+ MongoSequence[:test].next.should == 1
109
+ MongoSequence[:test].next.should == 2
110
+ end
111
+
105
112
  end
106
113
 
107
114
  context "Mongoid" do
108
115
  before :each do
109
- # get around Mongoid complaining that my MongoDB is too old :P
110
116
  Mongoid.config.from_hash('database' => "mongo_sequence_test_mongoid", 'logger' => false)
117
+ # Mongoid.connect_to("mongo_sequence_test_mongoid") ## Mongoid 3
111
118
  end
112
119
 
113
120
  it "uses Mongoid's database by default" do
114
121
  MongoSequence.database.name.should == "mongo_sequence_test_mongoid"
122
+ # Mongoid.sessions['default']['database'].should == "mongo_sequence_test_mongoid" # Mongoid 3
115
123
  end
116
124
 
117
125
  it "allows overriding the database" do
126
+ ## TODO: Mongoid 3 uses Moped. Need to refactor a bunch.
118
127
  MongoSequence.database = Mongo::Connection.new.db('mongo_sequence_test')
119
128
  MongoSequence.database.name.should == "mongo_sequence_test"
120
129
  end
130
+
131
+ it "starts at 1 for a new sequence and increments" do
132
+ MongoSequence[:test].current = 0
133
+ MongoSequence[:test].next.should == 1
134
+ MongoSequence[:test].next.should == 2
135
+ end
136
+
121
137
  end
122
138
  end
metadata CHANGED
@@ -1,89 +1,79 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mongo_sequence
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 1
8
- - 0
9
- - 0
10
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Brian Hempel
14
8
  autorequire:
15
9
  bindir: bin
16
10
  cert_chain: []
17
-
18
- date: 2012-04-06 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
11
+ date: 2013-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
21
14
  name: mongo
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">"
27
- - !ruby/object:Gem::Version
28
- hash: 13
29
- segments:
30
- - 1
31
- - 1
32
- version: "1.1"
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>'
18
+ - !ruby/object:Gem::Version
19
+ version: '1.1'
33
20
  type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: rspec
37
21
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>'
25
+ - !ruby/object:Gem::Version
26
+ version: '1.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
47
34
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: mongo_mapper
51
35
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
53
- none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- hash: 3
58
- segments:
59
- - 0
60
- version: "0"
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongo_mapper
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
61
48
  type: :development
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: mongoid
65
49
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
75
62
  type: :development
76
- version_requirements: *id004
77
- description: Light-weight sequences for MongoDB, useful for auto-incrementing or counting. Works with any ODM.
78
- email:
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ description: Light-weight sequences for MongoDB, useful for auto-incrementing or counting. Works
70
+ with any ODM.
71
+ email:
79
72
  - plasticchicken@gmail.com
80
73
  executables: []
81
-
82
74
  extensions: []
83
-
84
75
  extra_rdoc_files: []
85
-
86
- files:
76
+ files:
87
77
  - .gitignore
88
78
  - Gemfile
89
79
  - README.md
@@ -93,36 +83,27 @@ files:
93
83
  - spec/mongo_sequence_spec.rb
94
84
  homepage: https://github.com/brianhempel/mongo_sequence
95
85
  licenses: []
96
-
86
+ metadata: {}
97
87
  post_install_message:
98
88
  rdoc_options: []
99
-
100
- require_paths:
89
+ require_paths:
101
90
  - lib
102
- required_ruby_version: !ruby/object:Gem::Requirement
103
- none: false
104
- requirements:
105
- - - ">="
106
- - !ruby/object:Gem::Version
107
- hash: 3
108
- segments:
109
- - 0
110
- version: "0"
111
- required_rubygems_version: !ruby/object:Gem::Requirement
112
- none: false
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- hash: 3
117
- segments:
118
- - 0
119
- version: "0"
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
120
101
  requirements: []
121
-
122
102
  rubyforge_project:
123
- rubygems_version: 1.8.21
103
+ rubygems_version: 2.0.3
124
104
  signing_key:
125
- specification_version: 3
126
- summary: Light-weight sequences for MongoDB, useful for auto-incrementing or counting. Works with any ODM.
127
- test_files:
105
+ specification_version: 4
106
+ summary: Light-weight sequences for MongoDB, useful for auto-incrementing or counting. Works
107
+ with any ODM.
108
+ test_files:
128
109
  - spec/mongo_sequence_spec.rb