mongoid_integer_keys 0.2
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/.gitignore +1 -0
- data/README.md +30 -0
- data/lib/mongoid_integer_keys.rb +66 -0
- data/mongoid_integer_keys.gemspec +22 -0
- metadata +61 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
This gem for change Mongoid id field as Integer like MySQL.
|
2
|
+
|
3
|
+
Idea from MongoDB document: [How to Make an Auto Incrementing Field](http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field)
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'mongoid_auto_increment_id'
|
9
|
+
```
|
10
|
+
|
11
|
+
# REQUIREMENTS
|
12
|
+
|
13
|
+
* Mongoid 2.2.0+
|
14
|
+
|
15
|
+
# USAGE
|
16
|
+
|
17
|
+
```shell
|
18
|
+
ruby > post = Post.new(:title => "Hello world")
|
19
|
+
=> #<Post _id: , _type: nil, title: "Hello world", body: nil>
|
20
|
+
ruby > post.save
|
21
|
+
=> true
|
22
|
+
ruby > post.inspect
|
23
|
+
=> "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
|
24
|
+
ruby > Post.find("6")
|
25
|
+
=> "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
|
26
|
+
ruby > Post.find(6)
|
27
|
+
=> "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
|
28
|
+
ruby > Post.desc(:_id).all.to_a.collect { |row| row.id }
|
29
|
+
=> [20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
|
30
|
+
```
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class Identity
|
3
|
+
# Generate auto increment id
|
4
|
+
def generate_id
|
5
|
+
counter = Mongoid.master.collection("mongoid.auto_increment_ids")
|
6
|
+
table_name = @document.class.to_s.tableize
|
7
|
+
o = counter.find_and_modify({:query => {:_id => table_name},
|
8
|
+
:update => {:$inc => {:c => 1}},
|
9
|
+
:new => true,
|
10
|
+
:upsert => true})
|
11
|
+
o["c"].to_i
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Document
|
16
|
+
included do
|
17
|
+
identity :type => Integer
|
18
|
+
end
|
19
|
+
|
20
|
+
def identify
|
21
|
+
end
|
22
|
+
|
23
|
+
alias_method :super_as_document,:as_document
|
24
|
+
def as_document
|
25
|
+
result = super_as_document
|
26
|
+
if result["_id"].blank?
|
27
|
+
result["_id"] = Identity.new(self).generate_id
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Criterion #:nodoc:
|
34
|
+
class Unconvertable < String
|
35
|
+
def initialize(value)
|
36
|
+
super(value.to_s)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module Extensions #:nodoc:
|
42
|
+
module ObjectId #:nodoc:
|
43
|
+
# Override Mongoid::Extensions::ObjectId::Conversions.convert for covert id to Integer type.
|
44
|
+
module Conversions
|
45
|
+
def convert(klass, args, reject_blank = true)
|
46
|
+
case args
|
47
|
+
when ::Array
|
48
|
+
args.delete_if { |arg| arg.blank? } if reject_blank
|
49
|
+
args.replace(args.map { |arg| convert(klass, arg, reject_blank) })
|
50
|
+
when ::Hash
|
51
|
+
args.tap do |hash|
|
52
|
+
hash.each_pair do |key, value|
|
53
|
+
hash[key] = value
|
54
|
+
end
|
55
|
+
end
|
56
|
+
when ::Integer
|
57
|
+
args
|
58
|
+
else
|
59
|
+
return nil if not args.to_s.match(/\d+/)
|
60
|
+
args.to_i
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "mongoid_integer_keys"
|
6
|
+
s.version = "0.2"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["John Maxwell"]
|
9
|
+
s.email = ["jgwmaxwell@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/jgw-maxwell/mongoid_auto_increment_id"
|
11
|
+
s.summary = %q{Override id field with MySQL like auto increment for Mongoid}
|
12
|
+
s.description = %q{This gem for change Mongoid id field as Integer like MySQL.
|
13
|
+
|
14
|
+
Idea from MongoDB document: [How to Make an Auto Incrementing Field](http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field)}
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "mongoid", ["~> 2.2.0"]
|
21
|
+
end
|
22
|
+
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_integer_keys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- John Maxwell
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-21 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongoid
|
16
|
+
requirement: &15198540 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *15198540
|
25
|
+
description: ! "This gem for change Mongoid id field as Integer like MySQL.\n\n Idea
|
26
|
+
from MongoDB document: [How to Make an Auto Incrementing Field](http://www.mongodb.org/display/DOCS/How+to+Make+an+Auto+Incrementing+Field)"
|
27
|
+
email:
|
28
|
+
- jgwmaxwell@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- README.md
|
35
|
+
- lib/mongoid_integer_keys.rb
|
36
|
+
- mongoid_integer_keys.gemspec
|
37
|
+
homepage: https://github.com/jgw-maxwell/mongoid_auto_increment_id
|
38
|
+
licenses: []
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.8.10
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Override id field with MySQL like auto increment for Mongoid
|
61
|
+
test_files: []
|