mongoid_auto_inc 0.0.4

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 ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
6
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongoid_auto_inc.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jeff Smith
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/README.rdoc ADDED
@@ -0,0 +1,26 @@
1
+ = mongoid_auto_inc
2
+ Add SQL like auto-incrementing fields to your Mongoid documents.
3
+
4
+ This gem is inspired by http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/ and the mongomapper_id2[https://github.com/phstc/mongomapper_id2] gem.
5
+ == Install
6
+ gem install mongoid_auto_inc
7
+ == Usage
8
+ Just add <tt>auto_increment :field</tt> to your Mongoid model where <tt>:field</tt> is the name of the auto-incremented field you want to create.
9
+ Example:
10
+ class Book
11
+ include Mongoid::Document
12
+
13
+ field :title
14
+ field :author
15
+
16
+ auto_increment :sequence
17
+ end
18
+ <tt>auto_increment :sequence</tt> will create a field of type <tt>Integer</tt> named <tt>sequence</tt> for <tt>Book</tt>. Whenever an instance of the model is created (intially saved to mongoDB), the <tt>auto_increment</tt> field will automatically be set to the next number in the sequence.
19
+
20
+ You can add more than one auto-incremented field per model.
21
+ === Options
22
+ auto_increment :sequence, :collection => :some_collection
23
+ <tt>mongoid_auto_inc</tt> keeps track of the current number in the sequence by creating a separate document mongoDB to query and update. By default <tt>auto_increment</tt> will save this document to a mongoDB collection called <tt>sequences</tt>. If you wish to save to a different collection use the <tt>:collection</tt> option to specify its name.
24
+
25
+ auto_increment :sequence, :seed => 3333
26
+ Use the <tt>:seed</tt> option to set the initial value of the auto-incremented field. The first number assigned from the sequence will be the next number after the seed value.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,64 @@
1
+ # This is a modified version of the code found on this blog post:
2
+ # http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/
3
+ module MongoidAutoInc
4
+ class Incrementor
5
+ class Sequence
6
+ def initialize(sequence, collection_name)
7
+ @sequence = sequence.to_s
8
+ @collection = collection_name.to_s
9
+ exists? || create
10
+ end
11
+
12
+ def inc
13
+ update_number_with("$inc" => { "number" => 1 })
14
+ end
15
+
16
+ def set(number)
17
+ update_number_with("$set" => { "number" => number })
18
+ end
19
+
20
+ private
21
+
22
+ def exists?
23
+ collection.find(query).count > 0
24
+ end
25
+
26
+ def create(number = 0)
27
+ collection.insert(query.merge({ "number" => number }))
28
+ end
29
+
30
+ def collection
31
+ Mongoid.database[@collection]
32
+ end
33
+
34
+ def query
35
+ { "seq_name" => @sequence }
36
+ end
37
+
38
+ def current
39
+ collection.find_one(query)["number"]
40
+ end
41
+
42
+ def update_number_with(mongo_func)
43
+ opts = {
44
+ "query" => query,
45
+ "update" => mongo_func,
46
+ "new" => true # return the modified document
47
+ }
48
+ collection.find_and_modify(opts)["number"]
49
+ end
50
+ end
51
+
52
+ def initialize(options={})
53
+ @collection = options[:collection] || "sequences"
54
+ end
55
+
56
+ def [](sequence)
57
+ Sequence.new(sequence, @collection)
58
+ end
59
+
60
+ def []=(sequence, number)
61
+ Sequence.new(sequence, @collection).set(number)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidAutoInc
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,23 @@
1
+ require "mongoid_auto_inc/incrementor"
2
+
3
+ module MongoidAutoInc
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def auto_increment(name, options={})
8
+ field name, :type => Integer
9
+
10
+ seq_name = "#{self.name.downcase}_#{name}"
11
+ @@incrementor = MongoidAutoInc::Incrementor.new(options)
12
+ @@incrementor[seq_name].set(options[:seed]) if options[:seed].is_a? Integer
13
+
14
+ before_create { self.send("#{name}=", @@incrementor[seq_name].inc) }
15
+ end
16
+ end
17
+ end
18
+
19
+ module Mongoid
20
+ module Document
21
+ include MongoidAutoInc
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid_auto_inc/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_auto_inc"
7
+ s.version = MongoidAutoInc::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jeff Smith"]
10
+ s.email = ["jffreyjs@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Adds auto increment capabilities to Mongoid::Document}
13
+ s.description = %q{Adds auto increment capabilities to Mongoid::Document}
14
+
15
+ s.rubyforge_project = "mongoid_auto_inc"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_development_dependency %q<rspec>, ['~> 2.0.0.beta.22']
26
+ s.add_dependency %q<mongoid>, ['>= 2.0.0.rc.6']
27
+ s.add_dependency %q<activesupport>, ['>=3.0.0']
28
+ else
29
+ s.add_dependency %q<rspec>, ['~> 2.0.0.beta.22']
30
+ s.add_dependency %q<mongoid>, ['>= 2.0.0.rc.6']
31
+ s.add_dependency %q<activesupport>, ['>= 3.0.0']
32
+ end
33
+ else
34
+ s.add_dependency %q<rspec>, ['~> 2.0.0.beta.22']
35
+ s.add_dependency %q<mongoid>, ['>= 2.0.0.rc.6']
36
+ s.add_dependency %q<activesupport>, ['>= 3.0.0']
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ require 'mongoid_auto_inc'
2
+
3
+ describe MongoidAutoInc
4
+ pending "I should add some tests at some point."
5
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_auto_inc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 4
9
+ version: 0.0.4
10
+ platform: ruby
11
+ authors:
12
+ - Jeff Smith
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-06 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ - beta
33
+ - 22
34
+ version: 2.0.0.beta.22
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mongoid
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ segments:
46
+ - 2
47
+ - 0
48
+ - 0
49
+ - rc
50
+ - 6
51
+ version: 2.0.0.rc.6
52
+ type: :runtime
53
+ version_requirements: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ name: activesupport
56
+ prerelease: false
57
+ requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 3
64
+ - 0
65
+ - 0
66
+ version: 3.0.0
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description: Adds auto increment capabilities to Mongoid::Document
70
+ email:
71
+ - jffreyjs@gmail.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.rdoc
83
+ - Rakefile
84
+ - lib/mongoid_auto_inc.rb
85
+ - lib/mongoid_auto_inc/incrementor.rb
86
+ - lib/mongoid_auto_inc/version.rb
87
+ - mongoid_auto_inc.gemspec
88
+ - spec/mongoid_auto_inc_spec.rb
89
+ has_rdoc: true
90
+ homepage: ""
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project: mongoid_auto_inc
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Adds auto increment capabilities to Mongoid::Document
121
+ test_files: []
122
+