mongoid_auto_increment_id 0.4.0 → 0.5.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.
Files changed (3) hide show
  1. data/README.md +29 -26
  2. data/lib/mongoid_auto_increment_id.rb +15 -58
  3. metadata +11 -6
data/README.md CHANGED
@@ -9,27 +9,26 @@ Idea from MongoDB document: [How to Make an Auto Incrementing Field](http://www.
9
9
  ## Installation
10
10
 
11
11
  # for Mongoid 2.2.x
12
- gem 'mongoid_auto_increment_id', "0.2.2"
12
+ gem 'mongoid_auto_increment_id', "0.2.2"
13
13
  # for Mongoid 2.3.x
14
- gem 'mongoid_auto_increment_id', "0.3.1"
15
-
16
-
17
- ## REQUIREMENTS
18
-
19
- * Mongoid 2.2.0+
14
+ gem 'mongoid_auto_increment_id', "0.3.1"
15
+ # for Mongoid 2.4.x
16
+ gem 'mongoid_auto_increment_id', "0.4.0"
17
+ # for Mongoid 3.0.x
18
+ gem 'mongoid_auto_increment_id', "0.5.0"
20
19
 
21
20
  ## USAGE
22
21
 
23
22
  ruby > post = Post.new(:title => "Hello world")
24
- => #<Post _id: , _type: nil, title: "Hello world", body: nil>
23
+ => #<Post _id: , _type: nil, title: "Hello world", body: nil>
25
24
  ruby > post.save
26
25
  => true
27
26
  ruby > post.inspect
28
- => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
27
+ => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
29
28
  ruby > Post.find("6")
30
- => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
29
+ => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
31
30
  ruby > Post.find(6)
32
- => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
31
+ => "#<Post _id: 6, _type: nil, title: \"Hello world\", body: nil>"
33
32
  ruby > Post.desc(:_id).all.to_a.collect { |row| row.id }
34
33
  => [20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
35
34
 
@@ -38,24 +37,28 @@ Idea from MongoDB document: [How to Make an Auto Incrementing Field](http://www.
38
37
 
39
38
  with `mongoid_auto_increment_id`:
40
39
 
41
- user system total real
42
- Generate 1 0.010000 0.000000 0.010000 ( 0.006182)
43
- Post current: 1
40
+ ```
41
+ user system total real
42
+ Generate 1 0.010000 0.000000 0.010000 ( 0.004616)
43
+ Post current: 1
44
+
45
+ Generate 100 0.110000 0.010000 0.120000 ( 0.161558)
46
+ Post current: 101
44
47
 
45
- Generate 100 0.130000 0.010000 0.140000 ( 0.406619)
46
- Post current: 101
48
+ Generate 10,000 12.460000 1.710000 14.170000 ( 17.940543)
49
+ Post current: 10101
50
+ ```
47
51
 
48
- Generate 10,000 10.160000 0.560000 10.720000 ( 12.518690)
49
- Post current: 10101
50
-
51
52
  without:
52
53
 
53
- user system total real
54
- Generate 1 0.000000 0.000000 0.000000 ( 0.171523)
55
- Post current: 1
54
+ ```
55
+ user system total real
56
+ Generate 1 0.000000 0.000000 0.000000 ( 0.004660)
57
+ Post current: 1
56
58
 
57
- Generate 100 0.110000 0.000000 0.110000 ( 0.129670)
58
- Post current: 101
59
+ Generate 100 0.090000 0.000000 0.090000 ( 0.091748)
60
+ Post current: 101
59
61
 
60
- Generate 10,000 9.340000 0.470000 9.810000 ( 11.233426)
61
- Post current: 10101
62
+ Generate 10,000 7.710000 0.950000 8.660000 ( 8.660268)
63
+ Post current: 10101
64
+ ```
@@ -1,24 +1,20 @@
1
- module Mongoid
2
- class Identity
3
- def identify
4
- nil
5
- end
6
-
7
- # Generate auto increment id
8
- def generate_id
9
- counter = Mongoid.master.collection("mongoid.auto_increment_ids")
10
- o = counter.find_and_modify({:query => {:_id => document.collection_name},
11
- :update => {:$inc => {:c => 1}},
12
- :new => true,
13
- :upsert => true})
14
- o["c"].to_i
1
+ module Mongoid
2
+ class Identity
3
+ # Generate auto increment id
4
+ def self.generate_id(document)
5
+ o = Mongoid::Sessions.default.command({:findAndModify => "mongoid.auto_increment_ids",
6
+ :query => { :_id => document.collection_name },
7
+ :update => { "$inc" => { :c => 1 } },
8
+ :upsert => true,
9
+ :new => true })
10
+ o["value"]["c"]
15
11
  end
16
12
  end
17
-
18
- module Document
13
+
14
+ module Document
19
15
  # define Integer for id field
20
16
  included do
21
- identity :type => Integer
17
+ field :_id, :type => Integer
22
18
  end
23
19
 
24
20
  # hack id nil when Document.new
@@ -26,53 +22,14 @@ module Mongoid
26
22
  Identity.new(self).create
27
23
  nil
28
24
  end
29
-
25
+
30
26
  alias_method :super_as_document,:as_document
31
27
  def as_document
32
28
  result = super_as_document
33
29
  if result["_id"].blank?
34
- result["_id"] = Identity.new(self).generate_id
30
+ result["_id"] = Identity.generate_id(self)
35
31
  end
36
32
  result
37
33
  end
38
34
  end
39
-
40
- module Criterion #:nodoc:
41
- class Unconvertable < String
42
- def initialize(value)
43
- super(value.to_s)
44
- end
45
- end
46
- end
47
-
48
- module Extensions #:nodoc:
49
- module ObjectId #:nodoc:
50
- # Override Mongoid::Extensions::ObjectId::Conversions.convert for covert id to Integer type.
51
- module Conversions
52
- def convert(klass, args, reject_blank = true)
53
-
54
- case args
55
- when ::Array
56
- args.delete_if { |arg| arg.blank? } if reject_blank
57
- args.replace(args.map { |arg| convert(klass, arg, reject_blank) })
58
- when ::Hash
59
- args.tap do |hash|
60
- hash.each_pair do |key, value|
61
- # TODO: this code it not good
62
- if key.to_s == "_id" and value.class == "".class
63
- value = value.to_i
64
- end
65
- hash[key] = value
66
- end
67
- end
68
- when ::Integer
69
- args
70
- else
71
- return nil if not args.to_s.match(/\d+/)
72
- args.to_i
73
- end
74
- end
75
- end
76
- end
77
- end
78
35
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_auto_increment_id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,19 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-04 00:00:00.000000000 Z
12
+ date: 2012-05-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &70160606208780 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 2.4.0
21
+ version: 3.0.0.rc
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70160606208780
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0.rc
25
30
  description: This gem for change Mongoid id field as Integer like MySQL.
26
31
  email:
27
32
  - huacnlee@gmail.com
@@ -51,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
56
  version: '0'
52
57
  requirements: []
53
58
  rubyforge_project:
54
- rubygems_version: 1.8.15
59
+ rubygems_version: 1.8.24
55
60
  signing_key:
56
61
  specification_version: 3
57
62
  summary: Override id field with MySQL like auto increment for Mongoid