mongoid-autoinc 0.4.0 → 0.5.0
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.
- data/README.rdoc +20 -1
- data/lib/autoinc.rb +2 -1
- data/lib/autoinc/incrementor.rb +14 -2
- data/lib/autoinc/version.rb +1 -1
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -87,6 +87,25 @@ This allows for more flexible assignment of your increment number:
|
|
87
87
|
|
88
88
|
end
|
89
89
|
|
90
|
+
=== Seeds
|
91
|
+
|
92
|
+
You can use a seed to start the incrementing field at a given value. The first
|
93
|
+
document created will start at `seed + 1`.
|
94
|
+
|
95
|
+
class Vehicle
|
96
|
+
include Mongoid::Document
|
97
|
+
include Mongoid::Autoinc
|
98
|
+
|
99
|
+
field :model
|
100
|
+
field :vin
|
101
|
+
|
102
|
+
increments :vin, seed: 1000
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
car = Vehicle.new(model: "Coupe")
|
107
|
+
car.vin # 1001
|
108
|
+
|
90
109
|
=== Development
|
91
110
|
|
92
111
|
$ gem install bundler (if you don't have it)
|
@@ -96,7 +115,7 @@ This allows for more flexible assignment of your increment number:
|
|
96
115
|
== Contributions
|
97
116
|
|
98
117
|
Thanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes
|
99
|
-
|
118
|
+
And to Marcus Gartner (@mgartner) for implementing the seed functionality
|
100
119
|
|
101
120
|
== Copyright
|
102
121
|
|
data/lib/autoinc.rb
CHANGED
@@ -44,9 +44,10 @@ module Mongoid
|
|
44
44
|
|
45
45
|
def increment!(field, options)
|
46
46
|
scope_key = options[:scope] ? evaluate_scope(options[:scope]) : nil
|
47
|
+
seed = options[:seed]
|
47
48
|
write_attribute(
|
48
49
|
field.to_sym, Mongoid::Autoinc::Incrementor.new(
|
49
|
-
self.class.model_name, field, scope_key).inc
|
50
|
+
self.class.model_name, field, scope_key, seed).inc
|
50
51
|
)
|
51
52
|
end
|
52
53
|
|
data/lib/autoinc/incrementor.rb
CHANGED
@@ -3,13 +3,15 @@ module Mongoid
|
|
3
3
|
module Autoinc
|
4
4
|
|
5
5
|
class Incrementor
|
6
|
-
attr_accessor :model_name, :field_name, :scope_key, :collection
|
6
|
+
attr_accessor :model_name, :field_name, :scope_key, :collection, :seed
|
7
7
|
|
8
|
-
def initialize(model_name, field_name, scope_key=nil)
|
8
|
+
def initialize(model_name, field_name, scope_key=nil, seed=nil)
|
9
9
|
self.model_name = model_name
|
10
10
|
self.field_name = field_name.to_s
|
11
11
|
self.scope_key = scope_key
|
12
12
|
self.collection = ::Mongoid.default_session['auto_increment_counters']
|
13
|
+
self.seed = seed
|
14
|
+
create if seed && !exists?
|
13
15
|
end
|
14
16
|
|
15
17
|
def key
|
@@ -28,6 +30,16 @@ module Mongoid
|
|
28
30
|
)['c']
|
29
31
|
end
|
30
32
|
|
33
|
+
private
|
34
|
+
|
35
|
+
def exists?
|
36
|
+
collection.find('_id' => key).count > 0
|
37
|
+
end
|
38
|
+
|
39
|
+
def create
|
40
|
+
collection.insert({'_id' => key, 'c' => seed})
|
41
|
+
end
|
42
|
+
|
31
43
|
end
|
32
44
|
|
33
45
|
end
|
data/lib/autoinc/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid-autoinc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.5.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Robert Beekman
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-
|
14
|
+
date: 2013-05-15 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -119,7 +119,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 332714116938446976
|
123
123
|
none: false
|
124
124
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
125
|
requirements:
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
segments:
|
130
130
|
- 0
|
131
|
-
hash:
|
131
|
+
hash: 332714116938446976
|
132
132
|
none: false
|
133
133
|
requirements: []
|
134
134
|
rubyforge_project:
|