cb_mongoid_auto_inc 0.0.6

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,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 MongoidAutoInc
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,3 @@
1
+ module MongoidAutoInc
2
+ VERSION = "0.0.6"
3
+ end
@@ -0,0 +1,21 @@
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
+ 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 MongoidAutoInc
20
+ end
21
+ 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 = "cb_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 = "cb_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,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cb_mongoid_auto_inc
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 6
10
+ version: 0.0.6
11
+ platform: ruby
12
+ authors:
13
+ - Jeff Smith
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-26 00:00:00 Z
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
+ hash: 62196431
29
+ segments:
30
+ - 2
31
+ - 0
32
+ - 0
33
+ - beta
34
+ - 22
35
+ version: 2.0.0.beta.22
36
+ type: :development
37
+ version_requirements: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ name: mongoid
40
+ prerelease: false
41
+ requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 15424089
47
+ segments:
48
+ - 2
49
+ - 0
50
+ - 0
51
+ - rc
52
+ - 6
53
+ version: 2.0.0.rc.6
54
+ type: :runtime
55
+ version_requirements: *id002
56
+ - !ruby/object:Gem::Dependency
57
+ name: activesupport
58
+ prerelease: false
59
+ requirement: &id003 !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 7
65
+ segments:
66
+ - 3
67
+ - 0
68
+ - 0
69
+ version: 3.0.0
70
+ type: :runtime
71
+ version_requirements: *id003
72
+ description: Adds auto increment capabilities to Mongoid::Document
73
+ email:
74
+ - jffreyjs@gmail.com
75
+ executables: []
76
+
77
+ extensions: []
78
+
79
+ extra_rdoc_files: []
80
+
81
+ files:
82
+ - .gitignore
83
+ - Gemfile
84
+ - LICENSE
85
+ - README.rdoc
86
+ - Rakefile
87
+ - lib/mongoid_auto_inc.rb
88
+ - lib/mongoid_auto_inc/incrementor.rb
89
+ - lib/mongoid_auto_inc/version.rb
90
+ - mongoid_auto_inc.gemspec
91
+ - spec/mongoid_auto_inc_spec.rb
92
+ homepage: ""
93
+ licenses: []
94
+
95
+ post_install_message:
96
+ rdoc_options: []
97
+
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ hash: 3
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ requirements: []
119
+
120
+ rubyforge_project: cb_mongoid_auto_inc
121
+ rubygems_version: 1.8.6
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Adds auto increment capabilities to Mongoid::Document
125
+ test_files:
126
+ - spec/mongoid_auto_inc_spec.rb