mongoid-autoinc 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +23 -6
- data/lib/autoinc.rb +6 -9
- data/lib/autoinc/version.rb +1 -1
- data/lib/incrementor.rb +7 -3
- metadata +12 -12
data/README.rdoc
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
A mongoid plugin to add auto incrementing fields to your documents.
|
4
4
|
|
5
|
+
{<img src="https://secure.travis-ci.org/80beans/mongoid-autoinc.png" />}[http://travis-ci.org/#!/80beans/mongoid-autoinc]
|
5
6
|
|
6
7
|
== Installation
|
7
8
|
|
@@ -11,22 +12,20 @@ in gemfile:
|
|
11
12
|
|
12
13
|
in class:
|
13
14
|
|
14
|
-
require
|
15
|
-
|
15
|
+
require 'autoinc'
|
16
16
|
|
17
17
|
=== Usage
|
18
18
|
|
19
|
-
# app/models/
|
19
|
+
# app/models/user.rb
|
20
20
|
class User
|
21
|
-
include
|
21
|
+
include Mongoid::Document
|
22
|
+
include Mongoid::Autoinc
|
22
23
|
field :name
|
23
24
|
field :number, :type => Integer
|
24
25
|
|
25
26
|
auto_increment :number
|
26
27
|
end
|
27
28
|
|
28
|
-
== Usage
|
29
|
-
|
30
29
|
user = User.create(:name => 'Dr. Percival "Perry" Ulysses Cox')
|
31
30
|
user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
|
32
31
|
user.number # 1
|
@@ -34,6 +33,24 @@ in class:
|
|
34
33
|
another_user = User.create(:name => 'Bob Kelso')
|
35
34
|
another_user.number # 2
|
36
35
|
|
36
|
+
|
37
|
+
=== Scopes
|
38
|
+
|
39
|
+
You can scope on document fields. For example:
|
40
|
+
|
41
|
+
class PatientFile
|
42
|
+
include Mongoid::Document
|
43
|
+
include Mongoid::Autoinc
|
44
|
+
|
45
|
+
field :name
|
46
|
+
field :number, :type => Integer
|
47
|
+
|
48
|
+
auto_increment :number, :scope => :patient_id
|
49
|
+
|
50
|
+
belongs_to :patient
|
51
|
+
|
52
|
+
end
|
53
|
+
|
37
54
|
=== Development
|
38
55
|
|
39
56
|
$ gem install bundler (if you don't have it)
|
data/lib/autoinc.rb
CHANGED
@@ -12,15 +12,11 @@ module Mongoid
|
|
12
12
|
module ClassMethods
|
13
13
|
|
14
14
|
def autoincrementing_fields
|
15
|
-
@autoincrementing_fields ||=
|
15
|
+
@autoincrementing_fields ||= {}
|
16
16
|
end
|
17
17
|
|
18
|
-
def auto_increment(field)
|
19
|
-
|
20
|
-
autoincrementing_fields<< field
|
21
|
-
else
|
22
|
-
autoincrementing_fields = [field]
|
23
|
-
end
|
18
|
+
def auto_increment(field, options={})
|
19
|
+
autoincrementing_fields[field] = options
|
24
20
|
end
|
25
21
|
|
26
22
|
end
|
@@ -28,10 +24,11 @@ module Mongoid
|
|
28
24
|
module InstanceMethods
|
29
25
|
|
30
26
|
def update_auto_increments
|
31
|
-
self.class.autoincrementing_fields.each do |autoincrementing_field|
|
27
|
+
self.class.autoincrementing_fields.each do |autoincrementing_field, options|
|
28
|
+
scope_key = options[:scope] ? send(options[:scope]) : nil
|
32
29
|
write_attribute(
|
33
30
|
autoincrementing_field.to_sym,
|
34
|
-
Mongoid::Autoinc::Incrementor.new(self.class.name, autoincrementing_field).inc
|
31
|
+
Mongoid::Autoinc::Incrementor.new(self.class.name, autoincrementing_field, scope_key).inc
|
35
32
|
)
|
36
33
|
end
|
37
34
|
|
data/lib/autoinc/version.rb
CHANGED
data/lib/incrementor.rb
CHANGED
@@ -4,16 +4,20 @@ module Mongoid
|
|
4
4
|
module Autoinc
|
5
5
|
|
6
6
|
class Incrementor
|
7
|
-
attr_accessor :model_name, :field_name, :collection
|
7
|
+
attr_accessor :model_name, :field_name, :scope_key, :collection
|
8
8
|
|
9
|
-
def initialize(model_name, field_name)
|
9
|
+
def initialize(model_name, field_name, scope_key=nil)
|
10
10
|
self.model_name = model_name
|
11
11
|
self.field_name = field_name.to_s
|
12
|
+
self.scope_key = scope_key
|
12
13
|
self.collection = Mongoid.database['auto_increment_counters']
|
13
14
|
end
|
14
15
|
|
15
16
|
def key
|
16
|
-
"
|
17
|
+
"".tap do |str|
|
18
|
+
str << "#{model_name.underscore}_#{field_name}"
|
19
|
+
str << "_#{scope_key}" unless scope_key.blank?
|
20
|
+
end
|
17
21
|
end
|
18
22
|
|
19
23
|
def ensuring_document(&block)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-autoinc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongoid
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152146500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152146500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bson_ext
|
27
|
-
requirement: &
|
27
|
+
requirement: &2152139260 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2152139260
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &2152138200 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2152138200
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: activesupport
|
49
|
-
requirement: &
|
49
|
+
requirement: &2152137260 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2152137260
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &2152136580 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2152136580
|
69
69
|
description: Think auto incrementing field from MySQL for mongoid.
|
70
70
|
email:
|
71
71
|
- robert@80beans.com
|