mongoid_auto_inc 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongoid_auto_inc.rb +0 -2
- data/lib/mongoid_auto_inc/incrementor.rb +7 -5
- data/lib/mongoid_auto_inc/version.rb +1 -1
- data/spec/mongoid_auto_inc_spec.rb +77 -2
- metadata +44 -77
data/lib/mongoid_auto_inc.rb
CHANGED
@@ -6,10 +6,8 @@ module MongoidAutoInc
|
|
6
6
|
module ClassMethods
|
7
7
|
def auto_increment(name, options={})
|
8
8
|
field name, :type => Integer
|
9
|
-
|
10
9
|
seq_name = "#{self.name.downcase}_#{name}"
|
11
10
|
@@incrementor = MongoidAutoInc::Incrementor.new(options)
|
12
|
-
@@incrementor[seq_name].set(options[:seed]) if options[:seed].is_a? Integer
|
13
11
|
|
14
12
|
before_create { self.send("#{name}=", @@incrementor[seq_name].inc) }
|
15
13
|
end
|
@@ -3,10 +3,10 @@
|
|
3
3
|
module MongoidAutoInc
|
4
4
|
class Incrementor
|
5
5
|
class Sequence
|
6
|
-
def initialize(sequence, collection_name)
|
6
|
+
def initialize(sequence, collection_name, seed)
|
7
7
|
@sequence = sequence.to_s
|
8
8
|
@collection = collection_name.to_s
|
9
|
-
exists? || create
|
9
|
+
exists? || create(seed)
|
10
10
|
end
|
11
11
|
|
12
12
|
def inc
|
@@ -49,16 +49,18 @@ module MongoidAutoInc
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
def initialize(options=
|
52
|
+
def initialize(options=nil)
|
53
|
+
options ||= {}
|
53
54
|
@collection = options[:collection] || "sequences"
|
55
|
+
@seed = options[:seed].to_i
|
54
56
|
end
|
55
57
|
|
56
58
|
def [](sequence)
|
57
|
-
Sequence.new(sequence, @collection)
|
59
|
+
Sequence.new(sequence, @collection, @seed)
|
58
60
|
end
|
59
61
|
|
60
62
|
def []=(sequence, number)
|
61
|
-
Sequence.new(sequence, @collection).set(number)
|
63
|
+
Sequence.new(sequence, @collection, @seed).set(number)
|
62
64
|
end
|
63
65
|
end
|
64
66
|
end
|
@@ -1,5 +1,80 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# rspec spec/mongoid_auto_inc_spec.rb
|
3
|
+
require 'rspec'
|
4
|
+
require 'active_support'
|
5
|
+
require 'mongoid'
|
1
6
|
require 'mongoid_auto_inc'
|
2
7
|
|
3
|
-
describe MongoidAutoInc
|
4
|
-
|
8
|
+
describe MongoidAutoInc do
|
9
|
+
before(:all) do
|
10
|
+
Mongoid.master = Mongo::Connection.new.db("mongoid_auto_inc_test")
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "auto increment" do
|
14
|
+
before(:all) do
|
15
|
+
class DocumentA
|
16
|
+
include Mongoid::Document
|
17
|
+
auto_increment :seq
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
Mongoid.database["sequences"].remove
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should be nil" do
|
26
|
+
DocumentA.new.seq.should be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should be 1" do
|
30
|
+
DocumentA.create!.seq.should == 1
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should be 2" do
|
34
|
+
DocumentA.create!
|
35
|
+
DocumentA.create!.seq.should == 2
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "auto increment with :collection option" do
|
40
|
+
before(:all) do
|
41
|
+
class DocumentB
|
42
|
+
include Mongoid::Document
|
43
|
+
auto_increment :seq, :collection => 'other_sequences'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
before(:each) do
|
48
|
+
Mongoid.database["sequences"].remove
|
49
|
+
Mongoid.database["other_sequences"].remove
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should store sequence data to other_sequences collection" do
|
53
|
+
DocumentB.create!.seq.should == 1
|
54
|
+
DocumentB.create!.seq.should == 2
|
55
|
+
Mongoid.database["sequences"].remove
|
56
|
+
DocumentB.create!.seq.should == 3
|
57
|
+
Mongoid.database["other_sequences"].remove
|
58
|
+
DocumentB.create!.seq.should == 1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "auto increment with :seed option" do
|
63
|
+
before(:all) do
|
64
|
+
class DocumentC
|
65
|
+
include Mongoid::Document
|
66
|
+
auto_increment :seq, :seed => 10
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
before(:each) do
|
71
|
+
Mongoid.database["sequences"].remove
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should start with 11 (10 + 1)" do
|
75
|
+
DocumentC.create!.seq.should == 11
|
76
|
+
DocumentC.create!.seq.should == 12
|
77
|
+
DocumentC.create!.seq.should == 13
|
78
|
+
end
|
79
|
+
end
|
5
80
|
end
|
metadata
CHANGED
@@ -1,81 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_auto_inc
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 0
|
8
|
-
- 4
|
9
|
-
version: 0.0.4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Jeff Smith
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-01-25 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: rspec
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2172656860 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
18
|
+
requirements:
|
26
19
|
- - ~>
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
segments:
|
29
|
-
- 2
|
30
|
-
- 0
|
31
|
-
- 0
|
32
|
-
- beta
|
33
|
-
- 22
|
20
|
+
- !ruby/object:Gem::Version
|
34
21
|
version: 2.0.0.beta.22
|
35
22
|
type: :development
|
36
|
-
version_requirements: *id001
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: mongoid
|
39
23
|
prerelease: false
|
40
|
-
|
24
|
+
version_requirements: *2172656860
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongoid
|
27
|
+
requirement: &2172656360 !ruby/object:Gem::Requirement
|
41
28
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
segments:
|
46
|
-
- 2
|
47
|
-
- 0
|
48
|
-
- 0
|
49
|
-
- rc
|
50
|
-
- 6
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
51
32
|
version: 2.0.0.rc.6
|
52
33
|
type: :runtime
|
53
|
-
version_requirements: *id002
|
54
|
-
- !ruby/object:Gem::Dependency
|
55
|
-
name: activesupport
|
56
34
|
prerelease: false
|
57
|
-
|
35
|
+
version_requirements: *2172656360
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &2172655880 !ruby/object:Gem::Requirement
|
58
39
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
segments:
|
63
|
-
- 3
|
64
|
-
- 0
|
65
|
-
- 0
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
66
43
|
version: 3.0.0
|
67
44
|
type: :runtime
|
68
|
-
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2172655880
|
69
47
|
description: Adds auto increment capabilities to Mongoid::Document
|
70
|
-
email:
|
48
|
+
email:
|
71
49
|
- jffreyjs@gmail.com
|
72
50
|
executables: []
|
73
|
-
|
74
51
|
extensions: []
|
75
|
-
|
76
52
|
extra_rdoc_files: []
|
77
|
-
|
78
|
-
files:
|
53
|
+
files:
|
79
54
|
- .gitignore
|
80
55
|
- Gemfile
|
81
56
|
- LICENSE
|
@@ -86,37 +61,29 @@ files:
|
|
86
61
|
- lib/mongoid_auto_inc/version.rb
|
87
62
|
- mongoid_auto_inc.gemspec
|
88
63
|
- spec/mongoid_auto_inc_spec.rb
|
89
|
-
|
90
|
-
homepage: ""
|
64
|
+
homepage: ''
|
91
65
|
licenses: []
|
92
|
-
|
93
66
|
post_install_message:
|
94
67
|
rdoc_options: []
|
95
|
-
|
96
|
-
require_paths:
|
68
|
+
require_paths:
|
97
69
|
- lib
|
98
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
71
|
none: false
|
100
|
-
requirements:
|
101
|
-
- -
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
|
104
|
-
|
105
|
-
version: "0"
|
106
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
77
|
none: false
|
108
|
-
requirements:
|
109
|
-
- -
|
110
|
-
- !ruby/object:Gem::Version
|
111
|
-
|
112
|
-
- 0
|
113
|
-
version: "0"
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
114
82
|
requirements: []
|
115
|
-
|
116
83
|
rubyforge_project: mongoid_auto_inc
|
117
|
-
rubygems_version: 1.
|
84
|
+
rubygems_version: 1.8.14
|
118
85
|
signing_key:
|
119
86
|
specification_version: 3
|
120
87
|
summary: Adds auto increment capabilities to Mongoid::Document
|
121
|
-
test_files:
|
122
|
-
|
88
|
+
test_files:
|
89
|
+
- spec/mongoid_auto_inc_spec.rb
|