mongoid-autoinc 0.5.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- MzE2NjY5ZmUwNzcyMjI1YjU4NTNjMDU1OTYzMGRkZjk3ZGE1MzRhOA==
5
- data.tar.gz: !binary |-
6
- NmJjYzFmMTUyNGIzNDM4NzM3M2Q4YTA3Y2M4NDMyZjhiOWE5NWZlYw==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- MmQ4MDc1ODYzMmU5YzdiYzBhMDc3OWVlMzExNjliZmU4MjRhZWMzMmQ2MzA3
10
- MmZmMjQ1ODM5NWFjOGNiZTgxMTExMDAzMDA3Mzk4ZjIyODY4ZTc1NWEzMmU1
11
- NTUxN2I4ODA4YjM3MmZkOWVhMzBkN2I2NDllMDQ5MDIwNGVmMDQ=
12
- data.tar.gz: !binary |-
13
- ZWQ2YWFkYjQ5YmM4ZTM3OWQzOTFlNmE4YzUzMTdkOTA2MjBjNDA2ZTg1ZWYw
14
- NzVjNzUxMDY1NDRhZDg5NTRhN2Q3ODNhY2FjZTNlYjY1ZTE0N2MxMzdmYTU5
15
- ODY5ZmE5MjUzZDFlOGRhOGUwMWM1YjMyODUxMDM5YTViNDRjZjk=
2
+ SHA1:
3
+ metadata.gz: 96d00a61057fb2bc8e9fb2132a53f0621c0d3e90
4
+ data.tar.gz: 7ab2a16af4c5e473cf4c37ffc44982af4cd7dd3e
5
+ SHA512:
6
+ metadata.gz: 99e700cf5996f0e5002e2f7dd40281a35041511170500052a44537f4b03c24deef3a38fb4b2d53c6f75341e607d91d000247717a36361001edcdc02921685250
7
+ data.tar.gz: 70e77ba82f5dd884b7dd6f23dd9622bacb43586a85ba3e25a5bccf95882240a20ce7a8971542fb34e4f60a895fe6c87cb575d7c729a783dcee64031d9430ae9e
data/README.md ADDED
@@ -0,0 +1,199 @@
1
+ # mongoid-autoinc
2
+
3
+ A mongoid plugin to add auto incrementing fields to your documents.
4
+
5
+ [![Build Status](https://travis-ci.org/suweller/mongoid-autoinc.png?branch=master)](https://travis-ci.org/suweller/mongoid-autoinc)
6
+ [![Code Climate](https://codeclimate.com/github/suweller/mongoid-autoinc.png)](https://codeclimate.com/github/suweller/mongoid-autoinc)
7
+
8
+ # Versions
9
+
10
+ Use 0.1.3 for mongoid 2.0
11
+
12
+ Use 0.3.0 for mongoid 3.0
13
+
14
+ Use the `mongoid-4` branch for mongoid 4.0.
15
+ We'll ship a new gem version when the mongoid 4.x gem is released.
16
+
17
+ ## Installation
18
+
19
+ in gemfile:
20
+
21
+ ``` ruby
22
+ gem 'mongoid-autoinc'
23
+ ```
24
+
25
+ in class:
26
+
27
+ ``` ruby
28
+ require 'autoinc'
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ``` ruby
34
+ # app/models/user.rb
35
+ class User
36
+ include Mongoid::Document
37
+ include Mongoid::Autoinc
38
+ field :name
39
+ field :number, :type => Integer
40
+
41
+ increments :number
42
+ end
43
+
44
+ user = User.create(:name => 'Dr. Percival "Perry" Ulysses Cox')
45
+ user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
46
+ user.number # 1
47
+
48
+ another_user = User.create(:name => 'Bob Kelso')
49
+ another_user.number # 2
50
+ ```
51
+
52
+ ### Scopes
53
+
54
+ You can scope on document fields. For example:
55
+
56
+ ``` ruby
57
+ class PatientFile
58
+ include Mongoid::Document
59
+ include Mongoid::Autoinc
60
+
61
+ field :name
62
+ field :number, :type => Integer
63
+
64
+ increments :number, :scope => :patient_id
65
+
66
+ belongs_to :patient
67
+
68
+ end
69
+ ```
70
+
71
+ Scope can also be a Proc:
72
+
73
+ ``` ruby
74
+ increments :number, :scope => lambda { patient.name }
75
+ ```
76
+
77
+ ### Custom Increment Trigger
78
+
79
+ You can trigger the assignment of an increment field manually by passing:
80
+ `:auto => false` to the increment field.
81
+ This allows for more flexible assignment of your increment number:
82
+
83
+ ``` ruby
84
+ class Intern
85
+ include Mongoid::Document
86
+ include Mongoid::Autoinc
87
+
88
+ field :name
89
+ field :number
90
+
91
+ increments :number, :auto => false
92
+
93
+ after_save :assign_number_to_jd
94
+
95
+ protected
96
+
97
+ def assign_number_to_jd
98
+ assign!(:number) if number.blank? && name == 'J.D.'
99
+ end
100
+
101
+ end
102
+ ```
103
+
104
+ ### Custom Model Name
105
+
106
+ You can override the model name used to generate the autoincrement keys. This can be useful
107
+ when working with subclasses or namespaces.
108
+
109
+ ``` ruby
110
+ class Intern
111
+ include Mongoid::Document
112
+ include Mongoid::Autoinc
113
+
114
+ field :name
115
+ field :number
116
+
117
+ increments :number, model_name => :foo
118
+ end
119
+ ```
120
+
121
+ ### Seeds
122
+
123
+ You can use a seed to start the incrementing field at a given value. The first
124
+ document created will start at 'seed + 1'.
125
+
126
+ ``` ruby
127
+ class Vehicle
128
+ include Mongoid::Document
129
+ include Mongoid::Autoinc
130
+
131
+ field :model
132
+ field :vin
133
+
134
+ increments :vin, seed: 1000
135
+
136
+ end
137
+
138
+ car = Vehicle.new(model: "Coupe")
139
+ car.vin # 1001
140
+ ```
141
+
142
+ ### Step
143
+
144
+ The step option can be used to specify the amount to increment the field every
145
+ time a new document is created. If no step is specified, it will increment by
146
+ 1.
147
+
148
+ ``` ruby
149
+ class Ticket
150
+ include Mongoid::Document
151
+ include Mongoid::Autoinc
152
+
153
+ field :number
154
+
155
+ increments :number, step: 5
156
+
157
+ end
158
+ ```
159
+ ``` ruby
160
+ first_ticket = Ticket.new
161
+ first_ticket.number # 5
162
+ second_ticket = Ticket.new
163
+ second_ticket.number # 10
164
+ ```
165
+
166
+ The step option can also be a Proc:
167
+
168
+ ``` ruby
169
+ increments :number, step: lambda { 1 + rand(10) }
170
+ ```
171
+
172
+ ### Development
173
+
174
+ ```
175
+ $ gem install bundler (if you don't have it)
176
+ $ bundle install
177
+ $ bundle exec spec
178
+ ```
179
+
180
+ ## Contributing
181
+
182
+ * Fork and create a topic branch.
183
+ * Follow the
184
+ [80beans styleguide](https://gist.github.com/b896eb9e66fc6ab3640d).
185
+ Basically the [rubystyleguide](https://github.com/bbatsov/ruby-style-guide/)
186
+ with some minor changes.
187
+ * Submit a pull request
188
+
189
+ ## Contributions
190
+
191
+ Thanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes
192
+ And to Marcus Gartner (@mgartner) for implementing the seed functionality
193
+
194
+ Kris Martin (@krismartin) and Johnny Shields (@johnnyshields) for adding the
195
+ overwritten model name feature
196
+
197
+ ## Copyright
198
+
199
+ See LICENSE for details
data/lib/autoinc.rb CHANGED
@@ -24,7 +24,7 @@ module Mongoid
24
24
 
25
25
  def increments(field, options={})
26
26
  incrementing_fields[field] = options.reverse_merge!(:auto => true)
27
- attr_protected field
27
+ attr_protected(field) if respond_to?(:attr_protected)
28
28
  end
29
29
 
30
30
  end
@@ -43,24 +43,37 @@ module Mongoid
43
43
  end
44
44
 
45
45
  def increment!(field, options)
46
- scope_key = options[:scope] ? evaluate_scope(options[:scope]) : nil
47
- seed = options[:seed]
46
+ options = options.dup
47
+ model_name = (options.delete(:model_name) || self.class.model_name).to_s
48
+ options[:scope] = evaluate_scope(options[:scope]) if options[:scope]
49
+ options[:step] = evaluate_step(options[:step]) if options[:step]
48
50
  write_attribute(
49
- field.to_sym, Mongoid::Autoinc::Incrementor.new(
50
- self.class.model_name, field, scope_key, seed).inc
51
+ field.to_sym, Mongoid::Autoinc::Incrementor.new(model_name, field, options).inc
51
52
  )
52
53
  end
53
54
 
54
55
  def evaluate_scope(scope)
55
- if scope.is_a? Symbol
56
- send(scope)
57
- elsif scope.is_a? Proc
58
- instance_exec &scope
59
- else
60
- raise 'scope is not a Symbol or a Proc'
56
+ case scope
57
+ when Symbol then send(scope)
58
+ when Proc then instance_exec &scope
59
+ else raise 'scope is not a Symbol or a Proc'
61
60
  end
62
61
  end
63
62
 
63
+ def evaluate_step(step)
64
+ case step
65
+ when Integer then step
66
+ when Proc then evaluate_step_proc(step)
67
+ else raise 'step is not an Integer or a Proc'
68
+ end
69
+ end
70
+
71
+ def evaluate_step_proc(step_proc)
72
+ result = instance_exec &step_proc
73
+ return result if result.is_a? Integer
74
+ raise 'step Proc does not evaluate to an Integer'
75
+ end
76
+
64
77
  end
65
78
 
66
79
  end
@@ -3,14 +3,15 @@ module Mongoid
3
3
  module Autoinc
4
4
 
5
5
  class Incrementor
6
- attr_accessor :model_name, :field_name, :scope_key, :collection, :seed
6
+ attr_accessor :model_name, :field_name, :scope_key, :collection, :seed, :step
7
7
 
8
- def initialize(model_name, field_name, scope_key=nil, seed=nil)
9
- self.model_name = model_name
8
+ def initialize(model_name, field_name, options={})
9
+ self.model_name = model_name.to_s
10
10
  self.field_name = field_name.to_s
11
- self.scope_key = scope_key
11
+ self.scope_key = options[:scope]
12
+ self.step = options[:step] || 1
13
+ self.seed = options[:seed]
12
14
  self.collection = ::Mongoid.default_session['auto_increment_counters']
13
- self.seed = seed
14
15
  create if seed && !exists?
15
16
  end
16
17
 
@@ -25,7 +26,7 @@ module Mongoid
25
26
  collection.find(
26
27
  '_id' => key
27
28
  ).modify(
28
- {'$inc' => { 'c' => 1 }},
29
+ {'$inc' => { 'c' => step }},
29
30
  :new => true, :upsert => true
30
31
  )['c']
31
32
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Autoinc
3
- VERSION = "0.5.1"
3
+ VERSION = "4.0.0"
4
4
  end
5
5
  end
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.5.1
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Beekman
@@ -10,79 +10,79 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-06-06 00:00:00.000000000 Z
13
+ date: 2014-07-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongoid
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ~>
19
+ - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: '3.0'
21
+ version: '4.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - ~>
26
+ - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: '3.0'
28
+ version: '4.0'
29
29
  - !ruby/object:Gem::Dependency
30
- name: activesupport
30
+ name: rake
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - ! '>='
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: '0'
36
- type: :runtime
36
+ type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - ! '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  - !ruby/object:Gem::Dependency
44
- name: rake
44
+ name: foreman
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - ! '>='
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
- type: :runtime
50
+ type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - ! '>='
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  - !ruby/object:Gem::Dependency
58
- name: foreman
58
+ name: rspec
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - ! '>='
61
+ - - ">="
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - ! '>='
68
+ - - ">="
69
69
  - !ruby/object:Gem::Version
70
70
  version: '0'
71
71
  - !ruby/object:Gem::Dependency
72
- name: rspec
72
+ name: pry
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - ! '>='
75
+ - - ">="
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - ! '>='
82
+ - - ">="
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
- description: Think auto incrementing field from MySQL for mongoid.
85
+ description: Think auto incrementing field from SQL for mongoid.
86
86
  email:
87
87
  - robert@80beans.com
88
88
  - steven@80beans.com
@@ -91,12 +91,13 @@ executables: []
91
91
  extensions: []
92
92
  extra_rdoc_files: []
93
93
  files:
94
+ - README.md
95
+ - lib/autoinc.rb
94
96
  - lib/autoinc/incrementor.rb
95
97
  - lib/autoinc/version.rb
96
- - lib/autoinc.rb
97
- - README.rdoc
98
- homepage: https://github.com/80beans/mongoid-autoinc
99
- licenses: []
98
+ homepage: https://github.com/suweller/mongoid-autoinc
99
+ licenses:
100
+ - MIT
100
101
  metadata: {}
101
102
  post_install_message:
102
103
  rdoc_options: []
@@ -104,17 +105,17 @@ require_paths:
104
105
  - lib
105
106
  required_ruby_version: !ruby/object:Gem::Requirement
106
107
  requirements:
107
- - - ! '>='
108
+ - - ">="
108
109
  - !ruby/object:Gem::Version
109
110
  version: '0'
110
111
  required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  requirements:
112
- - - ! '>='
113
+ - - ">="
113
114
  - !ruby/object:Gem::Version
114
115
  version: '0'
115
116
  requirements: []
116
117
  rubyforge_project:
117
- rubygems_version: 2.0.3
118
+ rubygems_version: 2.2.2
118
119
  signing_key:
119
120
  specification_version: 4
120
121
  summary: Add auto incrementing fields to mongoid documents
data/README.rdoc DELETED
@@ -1,122 +0,0 @@
1
- = mongoid-autoinc
2
-
3
- = Versions
4
-
5
- Use 0.1.3 for mongoid 2.0
6
-
7
- use 0.3.0 for mongoid 3.0
8
-
9
-
10
- A mongoid plugin to add auto incrementing fields to your documents.
11
-
12
- {<img src="https://secure.travis-ci.org/80beans/mongoid-autoinc.png" />}[http://travis-ci.org/#!/80beans/mongoid-autoinc]
13
- {<img src="https://codeclimate.com/badge.png" />}[https://codeclimate.com/github/80beans/mongoid-autoinc]
14
-
15
- == Installation
16
-
17
- in gemfile:
18
-
19
- gem 'mongoid-autoinc'
20
-
21
- in class:
22
-
23
- require 'autoinc'
24
-
25
- === Usage
26
-
27
- # app/models/user.rb
28
- class User
29
- include Mongoid::Document
30
- include Mongoid::Autoinc
31
- field :name
32
- field :number, :type => Integer
33
-
34
- increments :number
35
- end
36
-
37
- user = User.create(:name => 'Dr. Percival "Perry" Ulysses Cox')
38
- user.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
39
- user.number # 1
40
-
41
- another_user = User.create(:name => 'Bob Kelso')
42
- another_user.number # 2
43
-
44
- === Scopes
45
-
46
- You can scope on document fields. For example:
47
-
48
- class PatientFile
49
- include Mongoid::Document
50
- include Mongoid::Autoinc
51
-
52
- field :name
53
- field :number, :type => Integer
54
-
55
- increments :number, :scope => :patient_id
56
-
57
- belongs_to :patient
58
-
59
- end
60
-
61
- Scope can also be a Proc:
62
-
63
- increments :number, :scope => lambda { patient.name }
64
-
65
- === Custom Increment Trigger
66
-
67
- You can trigger the assignment of an increment field manually by passing:
68
- +:auto => false+ to the increment field.
69
- This allows for more flexible assignment of your increment number:
70
-
71
- class Intern
72
- include Mongoid::Document
73
- include Mongoid::Autoinc
74
-
75
- field :name
76
- field :number
77
-
78
- increments :number, :auto => false
79
-
80
- after_save :assign_number_to_jd
81
-
82
- protected
83
-
84
- def assign_number_to_jd
85
- assign!(:number) if number.blank? && name == 'J.D.'
86
- end
87
-
88
- end
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
-
109
- === Development
110
-
111
- $ gem install bundler (if you don't have it)
112
- $ bundle install
113
- $ bundle exec spec
114
-
115
- == Contributions
116
-
117
- Thanks to Johnny Shields (@johnnyshields) for implementing proc support to scopes
118
- And to Marcus Gartner (@mgartner) for implementing the seed functionality
119
-
120
- == Copyright
121
-
122
- See LICENSE for details