mongoid_auto_increment 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/.rvmrc +2 -0
- data/Gemfile +17 -0
- data/LICENSE +21 -0
- data/README.rdoc +26 -0
- data/Rakefile +60 -0
- data/TODO +1 -0
- data/lib/mongoid_auto_increment.rb +21 -0
- data/lib/mongoid_auto_increment/incrementor.rb +66 -0
- data/mongoid_auto_increment.gemspec +71 -0
- data/spec/mongoid_auto_increment_spec.rb +5 -0
- metadata +150 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Add dependencies required to use your gem here.
|
4
|
+
|
5
|
+
gem "bson_ext"
|
6
|
+
gem "mongoid"
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.6.2"
|
13
|
+
gem "simplecov", '>= 0.4.0', :require => false
|
14
|
+
gem "rdoc", ">= 0"
|
15
|
+
gem "rspec", ">= 2.0.0"
|
16
|
+
gem "database_cleaner"
|
17
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 Jeff Smith
|
2
|
+
Copyright (c) 2011 Peter Savichev (proton) <proton@proton.name>
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= mongoid_auto_increment
|
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_increment
|
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,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'rake'
|
5
|
+
require 'bundler'
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'jeweler'
|
17
|
+
Jeweler::Tasks.new do |gem|
|
18
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
19
|
+
gem.name = "mongoid_auto_increment"
|
20
|
+
gem.homepage = "http://github.com/proton/mongoid_auto_increment"
|
21
|
+
gem.license = "MIT"
|
22
|
+
gem.summary = %q{Auto-incrementing fields for Mongoid documents}
|
23
|
+
gem.description = %q{Add SQL like auto-incrementing fields to your Mongoid documents.}
|
24
|
+
gem.email = "psavichev@gmail.com"
|
25
|
+
gem.authors = ["Peter Savichev (proton)"]
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
# require 'rspec/core/rake_task'
|
31
|
+
#
|
32
|
+
# RSpec::Core::RakeTask.new(:spec) do |spec|
|
33
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
# task :default => :spec
|
37
|
+
|
38
|
+
require 'rdoc/task'
|
39
|
+
RDoc::Task.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "mongoid_auto_increment #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Build gem"
|
49
|
+
task :build do
|
50
|
+
puts "Regenerating gemspec"
|
51
|
+
system "rake gemspec"
|
52
|
+
puts "Building"
|
53
|
+
system "gem build mongoid_rateable.gemspec"
|
54
|
+
end
|
55
|
+
|
56
|
+
desc "Release gem"
|
57
|
+
task :release => :build do
|
58
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "mongoid_auto_increment/incrementor"
|
2
|
+
|
3
|
+
module MongoidAutoIncrement
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def auto_increment(name, options={})
|
8
|
+
field name, :type => Integer
|
9
|
+
seq_name = "#{self.name.downcase}_#{name}"
|
10
|
+
@@incrementor = MongoidAutoInc::Incrementor.new(options)
|
11
|
+
|
12
|
+
before_create { self.send("#{name}=", @@incrementor[seq_name].inc) }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module Mongoid
|
18
|
+
module Document
|
19
|
+
include MongoidAutoIncrement
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,66 @@
|
|
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 MongoidAutoIncrement
|
4
|
+
class Incrementor
|
5
|
+
class Sequence
|
6
|
+
def initialize(sequence, collection_name, seed)
|
7
|
+
@sequence = sequence.to_s
|
8
|
+
@collection = collection_name.to_s
|
9
|
+
exists? || create(seed)
|
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=nil)
|
53
|
+
options ||= {}
|
54
|
+
@collection = options[:collection] || "sequences"
|
55
|
+
@seed = options[:seed].to_i
|
56
|
+
end
|
57
|
+
|
58
|
+
def [](sequence)
|
59
|
+
Sequence.new(sequence, @collection, @seed)
|
60
|
+
end
|
61
|
+
|
62
|
+
def []=(sequence, number)
|
63
|
+
Sequence.new(sequence, @collection, @seed).set(number)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mongoid_auto_increment}
|
8
|
+
s.version = "0.0.6"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = [%q{Peter Savichev (proton)}]
|
12
|
+
s.date = %q{2011-08-03}
|
13
|
+
s.description = %q{Add SQL like auto-incrementing fields to your Mongoid documents.}
|
14
|
+
s.email = %q{psavichev@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc",
|
18
|
+
"TODO"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".rspec",
|
22
|
+
".rvmrc",
|
23
|
+
"Gemfile",
|
24
|
+
"LICENSE",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"lib/mongoid_auto_increment.rb",
|
28
|
+
"lib/mongoid_auto_increment/incrementor.rb",
|
29
|
+
"mongoid_auto_increment.gemspec",
|
30
|
+
"spec/mongoid_auto_increment_spec.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/proton/mongoid_auto_increment}
|
33
|
+
s.licenses = [%q{MIT}]
|
34
|
+
s.require_paths = [%q{lib}]
|
35
|
+
s.rubygems_version = %q{1.8.6}
|
36
|
+
s.summary = %q{Auto-incrementing fields for Mongoid documents}
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
42
|
+
s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
|
43
|
+
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
44
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
45
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
46
|
+
s.add_development_dependency(%q<simplecov>, [">= 0.4.0"])
|
47
|
+
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0"])
|
49
|
+
s.add_development_dependency(%q<database_cleaner>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
52
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
53
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
54
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
55
|
+
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
56
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
57
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
58
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
62
|
+
s.add_dependency(%q<mongoid>, [">= 0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
65
|
+
s.add_dependency(%q<simplecov>, [">= 0.4.0"])
|
66
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
67
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0"])
|
68
|
+
s.add_dependency(%q<database_cleaner>, [">= 0"])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_auto_increment
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Peter Savichev (proton)
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-03 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bson_ext
|
16
|
+
requirement: &84896010 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *84896010
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: mongoid
|
27
|
+
requirement: &84895710 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *84895710
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &84895410 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *84895410
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &84895130 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.6.2
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *84895130
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: simplecov
|
60
|
+
requirement: &84894890 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.4.0
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *84894890
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rdoc
|
71
|
+
requirement: &84894650 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *84894650
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rspec
|
82
|
+
requirement: &84894410 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 2.0.0
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *84894410
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: database_cleaner
|
93
|
+
requirement: &84894020 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *84894020
|
102
|
+
description: Add SQL like auto-incrementing fields to your Mongoid documents.
|
103
|
+
email: psavichev@gmail.com
|
104
|
+
executables: []
|
105
|
+
extensions: []
|
106
|
+
extra_rdoc_files:
|
107
|
+
- LICENSE
|
108
|
+
- README.rdoc
|
109
|
+
- TODO
|
110
|
+
files:
|
111
|
+
- .rspec
|
112
|
+
- .rvmrc
|
113
|
+
- Gemfile
|
114
|
+
- LICENSE
|
115
|
+
- README.rdoc
|
116
|
+
- Rakefile
|
117
|
+
- lib/mongoid_auto_increment.rb
|
118
|
+
- lib/mongoid_auto_increment/incrementor.rb
|
119
|
+
- mongoid_auto_increment.gemspec
|
120
|
+
- spec/mongoid_auto_increment_spec.rb
|
121
|
+
- TODO
|
122
|
+
homepage: http://github.com/proton/mongoid_auto_increment
|
123
|
+
licenses:
|
124
|
+
- MIT
|
125
|
+
post_install_message:
|
126
|
+
rdoc_options: []
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
hash: -196396419
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
requirements: []
|
145
|
+
rubyforge_project:
|
146
|
+
rubygems_version: 1.8.6
|
147
|
+
signing_key:
|
148
|
+
specification_version: 3
|
149
|
+
summary: Auto-incrementing fields for Mongoid documents
|
150
|
+
test_files: []
|