mongoid_autoinc_id 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_autoinc_id.gemspec
4
+ gemspec
5
+ group :development, :test do
6
+ gem "mongoid", git: 'git://github.com/mongoid/mongoid.git'
7
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 doabit
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,38 @@
1
+ # MongoidAutoincId
2
+
3
+ Auto increment id for mongoid 3.0.0
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid_autoinc_id'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid_autoinc_id
18
+
19
+ ## Requiement
20
+
21
+ $ mongoid 3.0.0+
22
+
23
+ ## Usage
24
+
25
+ $ 1.9.3p125 :001 > p = Post.new(title: 'Do it')
26
+ $ => #<Post _id: , _type: nil, title: 'Do it', body: nil>
27
+ $ 1.9.3p125 :004 > p.save
28
+ $ => true
29
+ $ 1.9.3p125 :005 > p.id
30
+ $ => 1
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "mongoid_autoinc_id/auto_increment"
@@ -0,0 +1,21 @@
1
+ module MongoidAutoincId
2
+ module AutoIncrement
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ field :_id, :type => Integer
7
+ before_create :inc_id
8
+ end
9
+
10
+ def inc_id
11
+ self.id = Mongoid.default_session.command(findAndModify: "mongoid_autoinc_id",
12
+ query: {_id:self.class.name},
13
+ update: {"$inc" => {next:1}},
14
+ new: true,
15
+ upsert: true)["value"]['next'].to_s
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+ Mongoid::Document.send :include, MongoidAutoincId::AutoIncrement
@@ -0,0 +1,3 @@
1
+ module MongoidAutoincId
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mongoid_autoinc_id/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["doabit"]
6
+ gem.email = ["doinsist@gmail.com"]
7
+ gem.description = %q{Auto increment id for mongoid 3.0.0}
8
+ gem.summary = %q{Auto increment id for mongoid 3.0.0}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "mongoid_autoinc_id"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MongoidAutoincId::VERSION
17
+ gem.add_dependency 'mongoid', '~> 3.0.0'
18
+ gem.add_development_dependency 'rspec', '~> 2.10.0'
19
+ end
@@ -0,0 +1,5 @@
1
+ class Category
2
+ include Mongoid::Document
3
+ field :name
4
+ has_many :posts
5
+ end
@@ -0,0 +1,6 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ field :title
4
+ field :body
5
+ belongs_to :category
6
+ end
@@ -0,0 +1,45 @@
1
+ require "spec_helper"
2
+
3
+ describe "Mongoid::AutoIncrement" do
4
+
5
+ it "id should start with 1" do
6
+ category = Category.create(name: 'Foo')
7
+ post = category.posts.create(title: 'Do it')
8
+ category.id.should be(1)
9
+ post.id.should be(1)
10
+ end
11
+
12
+ it "should auto increment id width step 1" do
13
+ post = Post.create(title: 'Do it')
14
+ post2 = Post.create(title: 'Do it again')
15
+ (post.id + 1).should == post2.id
16
+ end
17
+
18
+ it "should unique id" do
19
+ post = Post.create(title: 'Do it')
20
+ post2 = Post.create(title: 'Do it again')
21
+ post2.destroy
22
+ post3 = Post.create(title: 'Do it again again')
23
+ post3.id.should be(3)
24
+ post3.id.should == post.id + 2
25
+ end
26
+
27
+ it "should can find by id with int or string" do
28
+ post = Post.create(title: 'Do it')
29
+ post2 = Post.create(title: 'Do it again')
30
+ Post.find(1).should == post
31
+ Post.find('2').should == post2
32
+ end
33
+
34
+ it "should can find by _id with int or string" do
35
+ post = Post.create(title: 'Do it')
36
+ post2 = Post.create(title: 'Do it again')
37
+ Post.where(_id: 1).first.should == post
38
+ Post.where(_id: '2').first.should == post2
39
+ end
40
+
41
+ it "should with nil id" do
42
+ post = Post.new
43
+ post.id.should be(nil)
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ test:
2
+ sessions:
3
+ default:
4
+ database: mongoid_autoinc_id_test
5
+ hosts:
6
+ - <%=ENV["MONGOID_SPEC_HOST"]%>:<%=ENV["MONGOID_SPEC_PORT"]%>
7
+ options:
8
+ consistency: :strong
9
+ options:
10
+ allow_dynamic_fields: true
11
+ identity_map_enabled: false
12
+ include_root_in_json: false
13
+ include_type_for_serialization: false
14
+ preload_models: false
15
+ scope_overwrite_exception: false
16
+ raise_not_found_error: true
17
+ skip_version_check: true
18
+ use_activesupport_time_zone: true
19
+ use_utc: false
@@ -0,0 +1,48 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ MODELS = File.join(File.dirname(__FILE__), "app/models")
5
+ $LOAD_PATH.unshift(MODELS)
6
+
7
+ require "rubygems"
8
+ require "bundler"
9
+ Bundler.setup
10
+
11
+ require "rspec"
12
+ require "mongoid"
13
+ require 'active_support'
14
+ require "mongoid_autoinc_id"
15
+
16
+
17
+ # These environment variables can be set if wanting to test against a database
18
+ # that is not on the local machine.
19
+ ENV["MONGOID_SPEC_HOST"] ||= "localhost"
20
+ ENV["MONGOID_SPEC_PORT"] ||= "27017"
21
+
22
+ # These are used when creating any connection in the test suite.
23
+ HOST = ENV["MONGOID_SPEC_HOST"]
24
+ PORT = ENV["MONGOID_SPEC_PORT"].to_i
25
+
26
+ def database_id
27
+ "mongoid_autoinc_id_test"
28
+ end
29
+
30
+ # Set the database that the spec suite connects to.
31
+ Mongoid.configure do |config|
32
+ config.connect_to(database_id)
33
+ end
34
+
35
+ # Autoload every model for the test suite that sits in spec/app/models.
36
+ Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
37
+ name = File.basename(file, ".rb")
38
+ autoload name.camelize.to_sym, name
39
+ end
40
+
41
+
42
+ RSpec.configure do |config|
43
+ # Drop all collections and clear the identity map before each spec.
44
+ config.before(:each) do
45
+ Mongoid.purge!
46
+ Mongoid::IdentityMap.clear
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_autoinc_id
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - doabit
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.10.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.10.0
46
+ description: Auto increment id for mongoid 3.0.0
47
+ email:
48
+ - doinsist@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .DS_Store
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/mongoid_autoinc_id.rb
61
+ - lib/mongoid_autoinc_id/auto_increment.rb
62
+ - lib/mongoid_autoinc_id/version.rb
63
+ - mongoid_autoinc_id.gemspec
64
+ - spec/app/models/category.rb
65
+ - spec/app/models/post.rb
66
+ - spec/autoinc_id_spec.rb
67
+ - spec/config/mongoid.yml
68
+ - spec/spec_helper.rb
69
+ homepage: ''
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.21
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Auto increment id for mongoid 3.0.0
93
+ test_files:
94
+ - spec/app/models/category.rb
95
+ - spec/app/models/post.rb
96
+ - spec/autoinc_id_spec.rb
97
+ - spec/config/mongoid.yml
98
+ - spec/spec_helper.rb
99
+ has_rdoc: