mongoid-auto_increment 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 636e36799b1aba6cca4dde72075867e7fa154b9a
4
+ data.tar.gz: 7ba001729ddc015754cb1fea683ecf774e0cc5f9
5
+ SHA512:
6
+ metadata.gz: 4f5fff67deb5e4a6a22d29dfadafd75ef49e9d05bf49b7cb88cd73b5c3616965a342734b1b11839bc6b9ecea0a6e9a2108e36f1f867724d39462555f0eb4961e
7
+ data.tar.gz: 6f9ef4e1bb16eaf273c1886f6a955c4b71522b3d5594f7b79d439116554b8da35f35983771dec9b0fec9d55de6d2a24a5a72be29d642ace037483ab899d37e81
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Geoffroy Planquart
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Default: run unit tests'
6
+ task :default => :test
7
+
8
+ desc 'Run tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
@@ -0,0 +1,6 @@
1
+ module Mongoid
2
+ module AutoIncrement
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
6
+
@@ -0,0 +1,73 @@
1
+ require 'active_support/concern'
2
+
3
+ module Mongoid
4
+ module AutoIncrement
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_attribute :auto_increment_class
9
+ class_attribute :auto_incremented_fields
10
+ class_attribute :auto_increment_classes
11
+ class_attribute :auto_increment_collection
12
+ class_attribute :auto_increment_class_prefix
13
+
14
+ self.auto_increment_class = self
15
+ self.auto_incremented_fields = []
16
+ self.auto_increment_classes = []
17
+ self.auto_increment_collection = '__auto_increments'
18
+ self.auto_increment_class_prefix = self.collection_name
19
+
20
+ set_callback :create, :before, :auto_increment!, unless: :persisted?
21
+
22
+ self.ancestors.each do |klass|
23
+ if klass.respond_to?(:auto_incremented_fields)
24
+ self.auto_increment_classes << klass
25
+ end
26
+ end
27
+ end
28
+
29
+ module ClassMethods
30
+ def auto_incremented(name)
31
+ if self.auto_increment_class != self
32
+ self.auto_increment_classes = self.auto_increment_classes + [self]
33
+ self.auto_incremented_fields = self.auto_incremented_fields.dup
34
+ if self.auto_increment_class.auto_increment_class_prefix == self.auto_increment_class_prefix
35
+ self.auto_increment_class_prefix = self.collection_name
36
+ end
37
+ self.auto_increment_class = self
38
+ end
39
+ self.auto_incremented_fields << name
40
+ field name, type: Integer
41
+ end
42
+ end
43
+
44
+ def auto_increment!
45
+ session = self.class.mongo_session
46
+ self.class.auto_increment_classes.each do |klass|
47
+ klass.auto_incremented_fields.each do |name|
48
+ if !self[name]
49
+ result = session.command({
50
+ findAndModify: self.class.auto_increment_collection,
51
+ query: {_id: "#{klass.auto_increment_class_prefix}_#{name}"},
52
+ update: { '$inc' => { value: 1} },
53
+ upsert: true,
54
+ new: true,
55
+ })
56
+ self[name] = result['value']['value']
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def dup
63
+ copy = super
64
+ self.class.auto_increment_classes.each do |klass|
65
+ klass.auto_incremented_fields.each do |name|
66
+ copy[name] = nil
67
+ end
68
+ end
69
+ copy
70
+ end
71
+ end
72
+ end
73
+
@@ -0,0 +1,2 @@
1
+ require 'mongoid/auto_increment'
2
+
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class ConsistencyTest < BaseTest
4
+ test 'single auto increment consistency' do
5
+ n = 200
6
+
7
+ n.times do
8
+ FirstAutoIncrementedModel.create
9
+ end
10
+
11
+ assert_equal (1..n).to_a, FirstAutoIncrementedModel.only(:identifier).map(&:identifier).sort
12
+ end
13
+
14
+ test 'pre incrementing should not be overriden' do
15
+ n = 200
16
+
17
+ n.times do
18
+ model = FirstAutoIncrementedModel.new
19
+ model.auto_increment!
20
+ model.save
21
+ end
22
+
23
+ assert_equal (1..n).to_a, FirstAutoIncrementedModel.only(:identifier).map(&:identifier).sort
24
+ end
25
+
26
+ test 'dup should create new identifier' do
27
+ n = 100
28
+
29
+ n.times do
30
+ model = FirstAutoIncrementedModel.create
31
+ model.dup.save
32
+ end
33
+
34
+ assert_equal (1..(n * 2)).to_a, FirstAutoIncrementedModel.only(:identifier).map(&:identifier).sort
35
+ end
36
+
37
+ test 'double auto increment consistency' do
38
+ n = 100
39
+
40
+ n.times do
41
+ FirstAutoIncrementedModel.create
42
+ SecondAutoIncrementedModel.create
43
+ end
44
+
45
+ assert_equal (1..n).to_a, FirstAutoIncrementedModel.only(:identifier).map(&:identifier).sort
46
+ assert_equal (1..n).to_a, SecondAutoIncrementedModel.only(:identifier).map(&:identifier).sort
47
+ end
48
+ end
49
+
@@ -0,0 +1,7 @@
1
+ class FirstAutoIncrementedModel
2
+ include Mongoid::Document
3
+ include Mongoid::AutoIncrement
4
+
5
+ auto_incremented :identifier
6
+ end
7
+
@@ -0,0 +1,7 @@
1
+ class SecondAutoIncrementedModel
2
+ include Mongoid::Document
3
+ include Mongoid::AutoIncrement
4
+
5
+ auto_incremented :identifier
6
+ end
7
+
@@ -0,0 +1,4 @@
1
+ class SubclassModel < FirstAutoIncrementedModel
2
+ auto_incremented :count
3
+ end
4
+
data/test/mongoid.yml ADDED
@@ -0,0 +1,6 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_auto_increment_test
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class SubclassTest < BaseTest
4
+ test 'subclass auto increment' do
5
+ n = 200
6
+
7
+ n.times do
8
+ SubclassModel.create
9
+ end
10
+
11
+ assert_equal (1..n).to_a, SubclassModel.only(:identifier).map(&:identifier).sort
12
+ assert_equal (1..n).to_a, SubclassModel.only(:count).map(&:count).sort
13
+ end
14
+
15
+ test 'subclass auto increment works with base' do
16
+ n = 100
17
+
18
+ n.times do |i|
19
+ FirstAutoIncrementedModel.create
20
+ m1 = SubclassModel.create
21
+
22
+ assert_equal i * 2 + 2, m1.identifier
23
+ end
24
+
25
+ assert_equal (1..n).to_a, SubclassModel.only(:count).map(&:count).sort
26
+ end
27
+
28
+ test 'mixed auto increment uniqueness' do
29
+ n = 100
30
+
31
+ n.times do
32
+ FirstAutoIncrementedModel.create
33
+ SubclassModel.create
34
+ end
35
+
36
+ assert_equal n * 2, FirstAutoIncrementedModel.all.uniq.size
37
+ end
38
+
39
+ test 'mixed auto increment consistency' do
40
+ n = 100
41
+
42
+ n.times do
43
+ FirstAutoIncrementedModel.create
44
+ SubclassModel.create
45
+ end
46
+
47
+ assert_equal (1..(n * 2)).to_a, FirstAutoIncrementedModel.only(:identifier).map(&:identifier).sort
48
+ end
49
+ end
50
+
@@ -0,0 +1,24 @@
1
+ require 'bundler/setup'
2
+ require 'simplecov'
3
+ SimpleCov.configure do
4
+ add_filter '/test/'
5
+ end
6
+ SimpleCov.start if ENV['COVERAGE']
7
+
8
+ require 'minitest/autorun'
9
+ require 'mongoid'
10
+
11
+ require File.expand_path("../../lib/mongoid-auto_increment", __FILE__)
12
+
13
+ Mongoid.load!("#{File.dirname(__FILE__)}/mongoid.yml", "test")
14
+
15
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
16
+
17
+ ActiveSupport::TestCase.test_order = :random
18
+
19
+ class BaseTest < ActiveSupport::TestCase
20
+ def teardown
21
+ Mongoid::Sessions.default.use('mongoid_auto_increment_test').drop
22
+ end
23
+ end
24
+
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ class UniquenessTest < BaseTest
4
+ test 'single auto increment uniqueness' do
5
+ n = 200
6
+
7
+ n.times do
8
+ FirstAutoIncrementedModel.create
9
+ end
10
+
11
+ assert_equal n, FirstAutoIncrementedModel.all.uniq.size
12
+ end
13
+
14
+ test 'double auto increment uniqueness' do
15
+ n = 100
16
+
17
+ n.times do
18
+ FirstAutoIncrementedModel.create
19
+ SecondAutoIncrementedModel.create
20
+ end
21
+
22
+ assert_equal n, FirstAutoIncrementedModel.all.uniq.size
23
+ assert_equal n, SecondAutoIncrementedModel.all.uniq.size
24
+ end
25
+ end
26
+
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-auto_increment
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Geoffroy Planquart
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ description: Let you define an auto-incremented field in Mongoid, which will behave
56
+ like SQL id field.
57
+ email:
58
+ - geoffroy@planquart.fr
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/mongoid/auto_increment/version.rb
64
+ - lib/mongoid/auto_increment.rb
65
+ - lib/mongoid-auto_increment.rb
66
+ - MIT-LICENSE
67
+ - Rakefile
68
+ - test/models/first_auto_incremented_model.rb
69
+ - test/models/second_auto_incremented_model.rb
70
+ - test/models/subclass_model.rb
71
+ - test/mongoid.yml
72
+ - test/uniqueness_test.rb
73
+ - test/consistency_test.rb
74
+ - test/subclass_test.rb
75
+ - test/test_helper.rb
76
+ homepage: https://github.com/Aethelflaed/mongoid-auto_increment
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Auto-incremented field for Mongoid
100
+ test_files:
101
+ - test/models/first_auto_incremented_model.rb
102
+ - test/models/second_auto_incremented_model.rb
103
+ - test/models/subclass_model.rb
104
+ - test/mongoid.yml
105
+ - test/uniqueness_test.rb
106
+ - test/consistency_test.rb
107
+ - test/subclass_test.rb
108
+ - test/test_helper.rb
109
+ has_rdoc: