mongoid-autoinc 0.0.4 → 0.1.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 +27 -3
- data/lib/autoinc.rb +24 -10
- metadata +14 -12
data/README.rdoc
CHANGED
@@ -23,7 +23,7 @@ in class:
|
|
23
23
|
field :name
|
24
24
|
field :number, :type => Integer
|
25
25
|
|
26
|
-
|
26
|
+
increments :number
|
27
27
|
end
|
28
28
|
|
29
29
|
user = User.create(:name => 'Dr. Percival "Perry" Ulysses Cox')
|
@@ -33,7 +33,6 @@ in class:
|
|
33
33
|
another_user = User.create(:name => 'Bob Kelso')
|
34
34
|
another_user.number # 2
|
35
35
|
|
36
|
-
|
37
36
|
=== Scopes
|
38
37
|
|
39
38
|
You can scope on document fields. For example:
|
@@ -45,12 +44,37 @@ You can scope on document fields. For example:
|
|
45
44
|
field :name
|
46
45
|
field :number, :type => Integer
|
47
46
|
|
48
|
-
|
47
|
+
increments :number, :scope => :patient_id
|
49
48
|
|
50
49
|
belongs_to :patient
|
51
50
|
|
52
51
|
end
|
53
52
|
|
53
|
+
=== Custom Increment Trigger
|
54
|
+
|
55
|
+
You can trigger the assignment of an increment field manually by passing:
|
56
|
+
`:auto => false` to the increment field.
|
57
|
+
This allows for more flexible assignment of your increment number:
|
58
|
+
|
59
|
+
class Intern
|
60
|
+
include Mongoid::Document
|
61
|
+
include Mongoid::Autoinc
|
62
|
+
|
63
|
+
field :name
|
64
|
+
field :number
|
65
|
+
|
66
|
+
increments :number, :auto => false
|
67
|
+
|
68
|
+
after_save :assign_number_to_jd
|
69
|
+
|
70
|
+
protected
|
71
|
+
|
72
|
+
def assign_number_to_jd
|
73
|
+
assign!(:number) if number.blank? && name == 'J.D.'
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
54
78
|
=== Development
|
55
79
|
|
56
80
|
$ gem install bundler (if you don't have it)
|
data/lib/autoinc.rb
CHANGED
@@ -5,33 +5,47 @@ module Mongoid
|
|
5
5
|
module Autoinc
|
6
6
|
extend ActiveSupport::Concern
|
7
7
|
|
8
|
+
AlreadyAssignedError = Class.new(StandardError)
|
9
|
+
AutoIncrementsError = Class.new(StandardError)
|
10
|
+
|
8
11
|
included do
|
9
12
|
before_create :update_auto_increments
|
10
13
|
end
|
11
14
|
|
12
15
|
module ClassMethods
|
13
16
|
|
14
|
-
def
|
15
|
-
@
|
17
|
+
def incrementing_fields
|
18
|
+
@incrementing_fields ||= {}
|
16
19
|
end
|
17
20
|
|
18
|
-
def
|
19
|
-
|
21
|
+
def increments(field, options={})
|
22
|
+
incrementing_fields[field] = options.reverse_merge!(:auto => true)
|
23
|
+
attr_protected field
|
20
24
|
end
|
21
25
|
|
22
26
|
end
|
23
27
|
|
24
28
|
module InstanceMethods
|
25
29
|
|
30
|
+
def assign!(field)
|
31
|
+
options = self.class.incrementing_fields[field]
|
32
|
+
raise AutoIncrementsError if options[:auto]
|
33
|
+
raise AlreadyAssignedError if send(field).present?
|
34
|
+
increment!(field, options)
|
35
|
+
end
|
36
|
+
|
26
37
|
def update_auto_increments
|
27
|
-
self.class.
|
28
|
-
|
29
|
-
write_attribute(
|
30
|
-
autoincrementing_field.to_sym,
|
31
|
-
Mongoid::Autoinc::Incrementor.new(self.class.name, autoincrementing_field, scope_key).inc
|
32
|
-
)
|
38
|
+
self.class.incrementing_fields.each do |field, options|
|
39
|
+
increment!(field, options) if options[:auto]
|
33
40
|
end
|
41
|
+
end
|
34
42
|
|
43
|
+
def increment!(field, options)
|
44
|
+
scope_key = options[:scope] ? send(options[:scope]) : nil
|
45
|
+
write_attribute(
|
46
|
+
field.to_sym,
|
47
|
+
Mongoid::Autoinc::Incrementor.new(self.class.name, field, scope_key).inc
|
48
|
+
)
|
35
49
|
end
|
36
50
|
|
37
51
|
end
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
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.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Robert Beekman
|
9
|
+
- Steven Weller
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
+
date: 2012-07-03 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: mongoid
|
16
|
-
requirement: &
|
17
|
+
requirement: &2152268260 !ruby/object:Gem::Requirement
|
17
18
|
none: false
|
18
19
|
requirements:
|
19
20
|
- - ! '>='
|
@@ -21,10 +22,10 @@ dependencies:
|
|
21
22
|
version: '0'
|
22
23
|
type: :runtime
|
23
24
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
+
version_requirements: *2152268260
|
25
26
|
- !ruby/object:Gem::Dependency
|
26
27
|
name: bson_ext
|
27
|
-
requirement: &
|
28
|
+
requirement: &2152267720 !ruby/object:Gem::Requirement
|
28
29
|
none: false
|
29
30
|
requirements:
|
30
31
|
- - ! '>='
|
@@ -32,10 +33,10 @@ dependencies:
|
|
32
33
|
version: '0'
|
33
34
|
type: :runtime
|
34
35
|
prerelease: false
|
35
|
-
version_requirements: *
|
36
|
+
version_requirements: *2152267720
|
36
37
|
- !ruby/object:Gem::Dependency
|
37
38
|
name: rspec
|
38
|
-
requirement: &
|
39
|
+
requirement: &2152267180 !ruby/object:Gem::Requirement
|
39
40
|
none: false
|
40
41
|
requirements:
|
41
42
|
- - ! '>='
|
@@ -43,10 +44,10 @@ dependencies:
|
|
43
44
|
version: '0'
|
44
45
|
type: :runtime
|
45
46
|
prerelease: false
|
46
|
-
version_requirements: *
|
47
|
+
version_requirements: *2152267180
|
47
48
|
- !ruby/object:Gem::Dependency
|
48
49
|
name: activesupport
|
49
|
-
requirement: &
|
50
|
+
requirement: &2152266400 !ruby/object:Gem::Requirement
|
50
51
|
none: false
|
51
52
|
requirements:
|
52
53
|
- - ! '>='
|
@@ -54,10 +55,10 @@ dependencies:
|
|
54
55
|
version: '0'
|
55
56
|
type: :runtime
|
56
57
|
prerelease: false
|
57
|
-
version_requirements: *
|
58
|
+
version_requirements: *2152266400
|
58
59
|
- !ruby/object:Gem::Dependency
|
59
60
|
name: rake
|
60
|
-
requirement: &
|
61
|
+
requirement: &2152265640 !ruby/object:Gem::Requirement
|
61
62
|
none: false
|
62
63
|
requirements:
|
63
64
|
- - ! '>='
|
@@ -65,10 +66,11 @@ dependencies:
|
|
65
66
|
version: '0'
|
66
67
|
type: :runtime
|
67
68
|
prerelease: false
|
68
|
-
version_requirements: *
|
69
|
+
version_requirements: *2152265640
|
69
70
|
description: Think auto incrementing field from MySQL for mongoid.
|
70
71
|
email:
|
71
72
|
- robert@80beans.com
|
73
|
+
- steven@80beans.com
|
72
74
|
executables: []
|
73
75
|
extensions: []
|
74
76
|
extra_rdoc_files: []
|