mongomapper_id2 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +56 -0
- data/lib/incrementor.rb +51 -0
- data/lib/mongomapper_id2.rb +32 -0
- data/lib/mongomapper_id2/version.rb +3 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/test_auto_increment.rb +33 -0
- data/test/models.rb +9 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +100 -0
- metadata +216 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Pablo Cantero
|
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,56 @@
|
|
1
|
+
= mongomapper_id2
|
2
|
+
|
3
|
+
It’s a MongoMapper plugin to add auto incremented id to your MongoMapper documents
|
4
|
+
|
5
|
+
This gem is inspired in this blog post http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
# https://rubygems.org/gems/mongomapper_id2
|
10
|
+
$ sudo gem install mongomapper_id2
|
11
|
+
|
12
|
+
=== Adding mongomapper_id2 gem
|
13
|
+
|
14
|
+
# Gemfile
|
15
|
+
gem 'mongomapper_id2'
|
16
|
+
|
17
|
+
=== Adding mongomapper_id2 in a document
|
18
|
+
|
19
|
+
# app/models/movie.rb
|
20
|
+
class Movie
|
21
|
+
include MongoMapper::Document
|
22
|
+
|
23
|
+
key :title, String
|
24
|
+
# Here is the mongomapper_id2
|
25
|
+
auto_increment!
|
26
|
+
end
|
27
|
+
|
28
|
+
== Usage
|
29
|
+
|
30
|
+
movie = Movie.create(:title => 'The Simpsons Movie')
|
31
|
+
movie.id # BSON::ObjectId('4d1d150d30f2246bc6000001')
|
32
|
+
# Here is the mongomapper_id2
|
33
|
+
movie.id2 # 1
|
34
|
+
|
35
|
+
movie2 = Movie.create(:title => 'Pirates of Silicon Valley')
|
36
|
+
movie2.id2 # 2
|
37
|
+
|
38
|
+
== Do you want to improve mongomapper_id2
|
39
|
+
|
40
|
+
You’re welcome to make your contributions and send them as a pull request
|
41
|
+
|
42
|
+
http://pablocantero.com/blog/contato
|
43
|
+
|
44
|
+
=== Development
|
45
|
+
|
46
|
+
$ gem install bundler (if you don't have it)
|
47
|
+
$ bundle install
|
48
|
+
$ bundle exec rake
|
49
|
+
|
50
|
+
==== Test
|
51
|
+
|
52
|
+
$ rake test
|
53
|
+
|
54
|
+
== Copyright
|
55
|
+
|
56
|
+
See LICENSE for details
|
data/lib/incrementor.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Based on http://ihswebdesign.com/blog/autoincrement-in-mongodb-with-ruby/
|
2
|
+
module MongomapperId2
|
3
|
+
class Incrementor
|
4
|
+
class Sequence
|
5
|
+
def initialize(sequence)
|
6
|
+
@sequence = sequence.to_s
|
7
|
+
exists? || create
|
8
|
+
end
|
9
|
+
def exists?
|
10
|
+
collection.find(query).count > 0
|
11
|
+
end
|
12
|
+
def db
|
13
|
+
MongoMapper.database # replace this if you're not using MongoMapper
|
14
|
+
end
|
15
|
+
def query
|
16
|
+
{ "seq_name" => @sequence }
|
17
|
+
end
|
18
|
+
def collection
|
19
|
+
db['sequences']
|
20
|
+
end
|
21
|
+
def current
|
22
|
+
collection.find_one(query)["number"]
|
23
|
+
end
|
24
|
+
def inc
|
25
|
+
update_number_with("$inc" => { "number" => 1 })
|
26
|
+
end
|
27
|
+
def create(number = 0)
|
28
|
+
collection.insert(query.merge({ "number" => number }))
|
29
|
+
end
|
30
|
+
def set(number)
|
31
|
+
update_number_with("$set" => { "number" => number })
|
32
|
+
end
|
33
|
+
def update_number_with(mongo_func)
|
34
|
+
opts = {
|
35
|
+
"query" => query,
|
36
|
+
"update" => mongo_func,
|
37
|
+
"new" => true # return the modified document
|
38
|
+
}
|
39
|
+
collection.find_and_modify(opts)["number"]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
class << self
|
43
|
+
def [](sequence)
|
44
|
+
Sequence.new(sequence)
|
45
|
+
end
|
46
|
+
def []=(sequence, number)
|
47
|
+
Sequence.new(sequence).set(number)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'incrementor'
|
2
|
+
|
3
|
+
module MongomapperId2
|
4
|
+
# Your code goes here...
|
5
|
+
end
|
6
|
+
|
7
|
+
# Base on http://railstips.org/blog/archives/2010/02/21/mongomapper-07-plugins/
|
8
|
+
module MongoMapper
|
9
|
+
module Plugins
|
10
|
+
module AutoIncrement
|
11
|
+
module ClassMethods
|
12
|
+
def auto_increment!
|
13
|
+
key :id2
|
14
|
+
class_eval { before_create class_eval { :update_auto_increment }}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
module InstanceMethods
|
18
|
+
private
|
19
|
+
def update_auto_increment
|
20
|
+
self.id2 = MongomapperId2::Incrementor[self.class.name].inc
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module AutoIncrementPluginAddition
|
28
|
+
def self.included(model)
|
29
|
+
model.plugin MongoMapper::Plugins::AutoIncrement
|
30
|
+
end
|
31
|
+
end
|
32
|
+
MongoMapper::Document.append_inclusions(AutoIncrementPluginAddition)
|
@@ -0,0 +1 @@
|
|
1
|
+
I am doing my best to keep unit and functional tests separate. As I see them, functional tests hit the database and should never care about internals. Unit tests do not hit the database.
|
@@ -0,0 +1,33 @@
|
|
1
|
+
File.expand_path("../lib/mongomapper_id2.rb", __FILE__)
|
2
|
+
|
3
|
+
require 'test_helper'
|
4
|
+
require 'mongomapper_id2'
|
5
|
+
|
6
|
+
class AutoIncrementTest < Test::Unit::TestCase
|
7
|
+
context "autoincrementing" do
|
8
|
+
setup do
|
9
|
+
@klass = Doc do
|
10
|
+
key :title, String
|
11
|
+
end
|
12
|
+
@klass.auto_increment!
|
13
|
+
end
|
14
|
+
|
15
|
+
should "set id2 on create" do
|
16
|
+
doc = @klass.new(:title => 'The Simpsons Movie')
|
17
|
+
doc.id2.should be(nil)
|
18
|
+
doc.save
|
19
|
+
doc.id2.should_not be(nil)
|
20
|
+
id2 = doc.id2
|
21
|
+
doc.save
|
22
|
+
doc.id2.should eql id2
|
23
|
+
end
|
24
|
+
|
25
|
+
should "auto increment id2" do
|
26
|
+
doc = @klass.new(:title => 'Pirates of Silicon Valley')
|
27
|
+
doc.save
|
28
|
+
doc2 = @klass.new(:title => 'Tropa de Elite')
|
29
|
+
doc2.save
|
30
|
+
(doc.id2 + 1).should eql doc2.id2
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/test/models.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
# For testing against edge rails also.
|
3
|
+
# $:.unshift '/Users/jnunemaker/dev/ruby/rails/activemodel/lib'
|
4
|
+
require 'active_model'
|
5
|
+
require 'models'
|
6
|
+
|
7
|
+
class ActiveModelLintTest < ActiveModel::TestCase
|
8
|
+
include ActiveModel::Lint::Tests
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@model = Movie.new
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
5
|
+
require 'mongo_mapper'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'ostruct'
|
8
|
+
|
9
|
+
require 'active_support/version'
|
10
|
+
require 'json'
|
11
|
+
require 'log_buddy'
|
12
|
+
require 'matchy'
|
13
|
+
require 'shoulda'
|
14
|
+
require 'timecop'
|
15
|
+
require 'mocha'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
def Doc(name=nil, &block)
|
19
|
+
klass = Class.new do
|
20
|
+
include MongoMapper::Document
|
21
|
+
set_collection_name :test
|
22
|
+
|
23
|
+
if name
|
24
|
+
class_eval "def self.name; '#{name}' end"
|
25
|
+
class_eval "def self.to_s; '#{name}' end"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
klass.class_eval(&block) if block_given?
|
30
|
+
klass.collection.remove
|
31
|
+
klass
|
32
|
+
end
|
33
|
+
|
34
|
+
def EDoc(name=nil, &block)
|
35
|
+
klass = Class.new do
|
36
|
+
include MongoMapper::EmbeddedDocument
|
37
|
+
|
38
|
+
if name
|
39
|
+
class_eval "def self.name; '#{name}' end"
|
40
|
+
class_eval "def self.to_s; '#{name}' end"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
klass.class_eval(&block) if block_given?
|
45
|
+
klass
|
46
|
+
end
|
47
|
+
|
48
|
+
def drop_indexes(klass)
|
49
|
+
if klass.database.collection_names.include?(klass.collection.name)
|
50
|
+
klass.collection.drop_indexes
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
custom_matcher :be_true do |receiver, matcher, args|
|
55
|
+
matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
|
56
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
|
57
|
+
receiver.eql?(true)
|
58
|
+
end
|
59
|
+
|
60
|
+
custom_matcher :be_false do |receiver, matcher, args|
|
61
|
+
matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
|
62
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
|
63
|
+
receiver.eql?(false)
|
64
|
+
end
|
65
|
+
|
66
|
+
custom_matcher :have_error_on do |receiver, matcher, args|
|
67
|
+
receiver.valid?
|
68
|
+
attribute = args[0]
|
69
|
+
expected_message = args[1]
|
70
|
+
|
71
|
+
if expected_message.nil?
|
72
|
+
matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
|
73
|
+
matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
|
74
|
+
!receiver.errors.on(attribute).blank?
|
75
|
+
else
|
76
|
+
actual = receiver.errors.on(attribute)
|
77
|
+
matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
|
78
|
+
matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
|
79
|
+
actual == expected_message
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
custom_matcher :have_index do |receiver, matcher, args|
|
84
|
+
index_name = args[0]
|
85
|
+
matcher.positive_failure_message = "#{receiver} does not have index named #{index_name}, but should"
|
86
|
+
matcher.negative_failure_message = "#{receiver} does have index named #{index_name}, but should not"
|
87
|
+
!receiver.collection.index_information.detect { |index| index[0] == index_name }.nil?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
log_dir = File.expand_path('../../log', __FILE__)
|
92
|
+
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
93
|
+
logger = Logger.new(log_dir + '/test.log')
|
94
|
+
|
95
|
+
LogBuddy.init(:logger => logger)
|
96
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
|
97
|
+
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
98
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
99
|
+
|
100
|
+
puts "\n--- Active Support Version: #{ActiveSupport::VERSION::STRING} ---\n"
|
metadata
ADDED
@@ -0,0 +1,216 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongomapper_id2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Pablo Cantero
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-01 00:00:00 -02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongo_mapper
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activesupport
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 3
|
44
|
+
- 4
|
45
|
+
version: 2.3.4
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: jnunemaker-validatable
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 1
|
58
|
+
- 8
|
59
|
+
- 4
|
60
|
+
version: 1.8.4
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: plucky
|
65
|
+
prerelease: false
|
66
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ~>
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
- 3
|
74
|
+
- 6
|
75
|
+
version: 0.3.6
|
76
|
+
type: :runtime
|
77
|
+
version_requirements: *id004
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: json
|
80
|
+
prerelease: false
|
81
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
type: :development
|
90
|
+
version_requirements: *id005
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: log_buddy
|
93
|
+
prerelease: false
|
94
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
type: :development
|
103
|
+
version_requirements: *id006
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: jnunemaker-matchy
|
106
|
+
prerelease: false
|
107
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ~>
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
- 4
|
115
|
+
- 0
|
116
|
+
version: 0.4.0
|
117
|
+
type: :development
|
118
|
+
version_requirements: *id007
|
119
|
+
- !ruby/object:Gem::Dependency
|
120
|
+
name: shoulda
|
121
|
+
prerelease: false
|
122
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ~>
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
segments:
|
128
|
+
- 2
|
129
|
+
- 11
|
130
|
+
version: "2.11"
|
131
|
+
type: :development
|
132
|
+
version_requirements: *id008
|
133
|
+
- !ruby/object:Gem::Dependency
|
134
|
+
name: timecop
|
135
|
+
prerelease: false
|
136
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ~>
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
- 3
|
144
|
+
- 1
|
145
|
+
version: 0.3.1
|
146
|
+
type: :development
|
147
|
+
version_requirements: *id009
|
148
|
+
- !ruby/object:Gem::Dependency
|
149
|
+
name: mocha
|
150
|
+
prerelease: false
|
151
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
153
|
+
requirements:
|
154
|
+
- - ~>
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
- 9
|
159
|
+
- 8
|
160
|
+
version: 0.9.8
|
161
|
+
type: :development
|
162
|
+
version_requirements: *id010
|
163
|
+
description:
|
164
|
+
email:
|
165
|
+
- pablo@pablocantero
|
166
|
+
executables: []
|
167
|
+
|
168
|
+
extensions: []
|
169
|
+
|
170
|
+
extra_rdoc_files: []
|
171
|
+
|
172
|
+
files:
|
173
|
+
- lib/incrementor.rb
|
174
|
+
- lib/mongomapper_id2/version.rb
|
175
|
+
- lib/mongomapper_id2.rb
|
176
|
+
- test/_NOTE_ON_TESTING
|
177
|
+
- test/functional/test_auto_increment.rb
|
178
|
+
- test/models.rb
|
179
|
+
- test/test_active_model_lint.rb
|
180
|
+
- test/test_helper.rb
|
181
|
+
- LICENSE
|
182
|
+
- README.rdoc
|
183
|
+
has_rdoc: true
|
184
|
+
homepage: https://github.com/phstc/mongomapper_id2
|
185
|
+
licenses: []
|
186
|
+
|
187
|
+
post_install_message:
|
188
|
+
rdoc_options: []
|
189
|
+
|
190
|
+
require_paths:
|
191
|
+
- lib
|
192
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
194
|
+
requirements:
|
195
|
+
- - ">="
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
segments:
|
198
|
+
- 0
|
199
|
+
version: "0"
|
200
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
201
|
+
none: false
|
202
|
+
requirements:
|
203
|
+
- - ">="
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
segments:
|
206
|
+
- 0
|
207
|
+
version: "0"
|
208
|
+
requirements: []
|
209
|
+
|
210
|
+
rubyforge_project: mongomapper_id2
|
211
|
+
rubygems_version: 1.3.7
|
212
|
+
signing_key:
|
213
|
+
specification_version: 3
|
214
|
+
summary: "It\xE2\x80\x99s a MongoMapper plugin to add auto incremented id to your MongoMapper documents"
|
215
|
+
test_files: []
|
216
|
+
|