mongoid-sequence2 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YzI5ZmZkNzNjNjVmNTUzNjE4NGJmYTQ5YWYxZWNlYmNmMjdhODg4YQ==
5
+ data.tar.gz: !binary |-
6
+ Y2QyNWU3NGY1MWMwODYwOGI3MzliNjBjMjQ1NWE1ZTU3ZTdlYTYzZg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjExYjE3M2E1OTg4Y2RkMTMyZDIwZDA5N2U3ZDM1YjEzOTE4M2I3NzFhNWIw
10
+ NzcxZTc5NTMyYmMzODlhZjM0OWI5YzAxMTM3ODFmNDU5NjVkMzM5NTJhMDEx
11
+ ZmNmMTEzNzQzZDg3Mjc5OGI0MDQzZDg5YjYxZGQyNjUxMDYwOWI=
12
+ data.tar.gz: !binary |-
13
+ ZDRhNGQ1YjA1NGZlZjkyMTYxYTZlMGVkNTA4YzViNWIwZWIyNWUwM2NmZGQ1
14
+ OTE3NGYzY2RkMmQzYzZmNmViMjNkZmI2NjliZjU0OGUyYmFiN2ZlMTNiM2Qz
15
+ MjliYmE3MTI1MmQ1MDhlYmMxZmIzMzM0ZTFhOTQwNWUxYTEyNmI=
data/.gitignore ADDED
@@ -0,0 +1,25 @@
1
+ # Ignore all swap files
2
+ *.sql
3
+ .*.swp
4
+ .*.swo
5
+ *~
6
+ *.bak
7
+ *.log
8
+ nohup.out
9
+ *.gem
10
+ *.rbc
11
+ .bundle
12
+ .config
13
+ .yardoc
14
+ Gemfile.lock
15
+ InstalledFiles
16
+ _yardoc
17
+ coverage
18
+ doc/
19
+ lib/bundler/man
20
+ pkg
21
+ rdoc
22
+ spec/reports
23
+ test/tmp
24
+ test/version_tmp
25
+ tmp
data/Changelogs.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 0.2.0
2
+
3
+ * support to mongoid 3.0 - 4.0. and unit-test is passed
4
+
5
+ ## 0.1.0
6
+
7
+ * fork from [goncalossilva/mongoid-sequence](https://github.com/goncalossilva/mongoid-sequence)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT LICENSE
2
+
3
+ Copyright (c) Gonçalo Silva <jhjguxin@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # Mongoid Sequence
2
+
3
+ Mongoid Sequence allows you to specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).
4
+
5
+ ## Credits
6
+
7
+ This gem was inspired by a couple of gists by [masatomo](https://gist.github.com/730677) and [ShogunPanda](https://gist.github.com/1086265).
8
+
9
+ and
10
+
11
+ https://github.com/agile42/mongoid_sequence2/ **from francis**
12
+
13
+ ## Usage
14
+
15
+ Include `Mongoid::Sequence` in your class and call `sequence(:field)`.
16
+
17
+ Like this:
18
+
19
+ ```ruby
20
+ class Sequenced
21
+ include Mongoid::Document
22
+ include Mongoid::Sequence
23
+
24
+ field :my_sequence, :type => Integer
25
+ sequence :my_sequence
26
+ end
27
+
28
+ s1 = Sequenced.create
29
+ s1.sequence #=> 1
30
+
31
+ s2 = Sequenced.create
32
+ s2.sequence #=> 2 # and so on
33
+ ```
34
+
35
+ It's also possible to make the `id` field behave like this:
36
+
37
+ ```ruby
38
+ class Sequenced
39
+ include Mongoid::Document
40
+ include Mongoid::Sequence
41
+
42
+ sequence :_id
43
+ end
44
+
45
+ s1 = Sequenced.create
46
+ s1.id #=> 1
47
+
48
+ s2 = Sequenced.create
49
+ s2.id #=> 2 # and so on
50
+ ```
51
+
52
+ ## Consistency
53
+
54
+ Mongoid::Sequence uses the atomic [findAndModify](http://www.mongodb.org/display/DOCS/findAndModify+Command) command, so you shouldn't have to worry about the sequence's consistency.
55
+
56
+ ## Installation
57
+
58
+ Just add it to your projects' `Gemfile`:
59
+
60
+ ```ruby
61
+ gem "mongoid-sequence2"
62
+ ```
63
+
64
+ ## Thanks
65
+
66
+ thanks [masatomo](https://github.com/masatomo), [goncalossilva](https://github.com/goncalossilva), [agile42](https://github.com/agile42)
67
+
68
+
69
+ <hr/>
70
+
71
+ Copyright © 2010 Gonçalo Silva, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ desc 'Default: run unit tests'
6
+ task :default => :test
7
+
8
+ desc 'Run tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Sequence
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,44 @@
1
+ require "mongoid-sequence2/version"
2
+ require "active_support/concern"
3
+
4
+ module Mongoid
5
+ module Sequence
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ set_callback :validate, :before, :set_sequence, :unless => :persisted?
10
+ end
11
+
12
+ def self.included(base)
13
+ base.extend(ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+ attr_accessor :sequence_fields
18
+
19
+ def sequence(fieldname)
20
+ self.sequence_fields ||= []
21
+ self.sequence_fields << fieldname
22
+ end
23
+ end
24
+
25
+ class Sequences
26
+ include Mongoid::Document
27
+ store_in collection: "__sequences"
28
+
29
+ field :fieldname
30
+ field :seq, type: Integer
31
+
32
+ def self.get_next_sequence(collection, fieldname)
33
+ Sequences.where(fieldname: "#{collection}_#{fieldname}").find_and_modify({'$inc' => {'seq' => 1}}, {'upsert' => 'true', :new => true}).seq
34
+ end
35
+ end
36
+
37
+ def set_sequence
38
+ self.class.sequence_fields.each do |f|
39
+ self[f] = Sequences.get_next_sequence(self.class.name.underscore, f)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/mongoid-sequence2/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["jhjguxin"]
6
+ gem.email = ["864248765@qq.com"]
7
+ gem.description = %q{Mongoid::Sequence2 gives you the ability to specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).}
8
+ gem.summary = %q{Specify fields to behave like a sequence number (exactly like the "id" column in conventional SQL flavors).}
9
+ gem.homepage = "https://github.com/jhjguxin/mongoid-sequence2"
10
+
11
+ gem.add_dependency("mongoid", [">= 3.0.0","< 4.1.0"])
12
+ gem.add_dependency("activesupport", "~> 3.1")
13
+ gem.add_development_dependency("rake", "~> 0.9")
14
+ gem.add_development_dependency "database_cleaner", "~> 0.9.1"
15
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ gem.name = "mongoid-sequence2"
19
+ gem.require_paths = ["lib"]
20
+ gem.version = Mongoid::Sequence::VERSION
21
+ end
@@ -0,0 +1,80 @@
1
+ development:
2
+ # Configure available database sessions. (required)
3
+ sessions:
4
+ # Defines the default session. (required)
5
+ default:
6
+ # Defines the name of the default database that Mongoid can connect to.
7
+ # (required).
8
+ database: mongoid-sequence_development
9
+ # Provides the hosts the default session can connect to. Must be an array
10
+ # of host:port pairs. (required)
11
+ hosts:
12
+ - localhost:27017
13
+ options:
14
+ # Change whether the session persists in safe mode by default.
15
+ # (default: false)
16
+ # safe: false
17
+
18
+ # Change the default consistency model to :eventual or :strong.
19
+ # :eventual will send reads to secondaries, :strong sends everything
20
+ # to master. (default: :eventual)
21
+ # consistency: :eventual
22
+
23
+ # How many times Moped should attempt to retry an operation after
24
+ # failure. (default: 30)
25
+ # max_retries: 30
26
+
27
+ # The time in seconds that Moped should wait before retrying an
28
+ # operation on failure. (default: 1)
29
+ # retry_interval: 1
30
+ # Configure Mongoid specific options. (optional)
31
+ options:
32
+ # Configuration for whether or not to allow access to fields that do
33
+ # not have a field definition on the model. (default: true)
34
+ # allow_dynamic_fields: true
35
+
36
+ # Enable the identity map, needed for eager loading. (default: false)
37
+ # identity_map_enabled: false
38
+
39
+ # Includes the root model name in json serialization. (default: false)
40
+ # include_root_in_json: false
41
+
42
+ # Include the _type field in serializaion. (default: false)
43
+ # include_type_for_serialization: false
44
+
45
+ # Preload all models in development, needed when models use
46
+ # inheritance. (default: false)
47
+ # preload_models: false
48
+
49
+ # Protect id and type from mass assignment. (default: true)
50
+ # protect_sensitive_fields: true
51
+
52
+ # Raise an error when performing a #find and the document is not found.
53
+ # (default: true)
54
+ # raise_not_found_error: true
55
+
56
+ # Raise an error when defining a scope with the same name as an
57
+ # existing method. (default: false)
58
+ # scope_overwrite_exception: false
59
+
60
+ # Skip the database version check, used when connecting to a db without
61
+ # admin access. (default: false)
62
+ # skip_version_check: false
63
+
64
+ # User Active Support's time zone in conversions. (default: true)
65
+ # use_activesupport_time_zone: true
66
+
67
+ # Ensure all times are UTC in the app side. (default: false)
68
+ # use_utc: false
69
+ test:
70
+ sessions:
71
+ default:
72
+ database: mongoid-sequence_test
73
+ hosts:
74
+ - localhost:27017
75
+ options:
76
+ consistency: :strong
77
+ # In the test environment we lower the retries and retry interval to
78
+ # low amounts for fast failures.
79
+ max_retries: 1
80
+ retry_interval: 0
@@ -0,0 +1,35 @@
1
+ require "test_helper"
2
+
3
+ class SequenceTest < BaseTest
4
+ def test_single_sequence_consistency
5
+ n = 200
6
+
7
+ n.times do
8
+ FirstSequencedModel.create
9
+ end
10
+
11
+ assert_equal FirstSequencedModel.only(:auto_increment).map(&:auto_increment).sort, (1..n).to_a
12
+ end
13
+
14
+ def test_id_sequence_consistency
15
+ n = 200
16
+
17
+ n.times do
18
+ IdSequencedModel.create
19
+ end
20
+
21
+ assert_equal IdSequencedModel.only(:id).map(&:id).sort, (1..n).to_a
22
+ end
23
+
24
+ def test_double_sequence_consistency
25
+ n = 100
26
+
27
+ n.times do
28
+ FirstSequencedModel.create
29
+ SecondSequencedModel.create
30
+ end
31
+
32
+ assert_equal FirstSequencedModel.only(:auto_increment).map(&:auto_increment).sort, (1..n).to_a
33
+ assert_equal SecondSequencedModel.only(:auto_increment).map(&:auto_increment).sort, (1..n).to_a
34
+ end
35
+ end
@@ -0,0 +1,7 @@
1
+ class FirstSequencedModel
2
+ include Mongoid::Document
3
+ include Mongoid::Sequence
4
+
5
+ field :auto_increment, :type => Integer
6
+ sequence :auto_increment
7
+ end
@@ -0,0 +1,6 @@
1
+ class IdSequencedModel
2
+ include Mongoid::Document
3
+ include Mongoid::Sequence
4
+
5
+ sequence :_id
6
+ end
@@ -0,0 +1,8 @@
1
+ class ParanoiaSequencedModel
2
+ include Mongoid::Document
3
+ include Mongoid::Paranoia
4
+ include Mongoid::Sequence
5
+
6
+ field :auto_increment, :type => Integer
7
+ sequence :auto_increment
8
+ end
@@ -0,0 +1,7 @@
1
+ class SecondSequencedModel
2
+ include Mongoid::Document
3
+ include Mongoid::Sequence
4
+
5
+ field :auto_increment, :type => Integer
6
+ sequence :auto_increment
7
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ class ParanoiaTest < BaseTest
4
+ def test_paranoia_doesnt_affect_sequence
5
+ m1 = ParanoiaSequencedModel.create
6
+ m2 = ParanoiaSequencedModel.create
7
+
8
+ assert_equal m1.auto_increment, 1
9
+ assert_equal m2.auto_increment, 2
10
+
11
+ m2.destroy
12
+ m3 = ParanoiaSequencedModel.create
13
+
14
+ assert_equal m3.auto_increment, 3
15
+
16
+ m3.destroy!
17
+ m4 = ParanoiaSequencedModel.create
18
+
19
+ assert_equal m4.auto_increment, 4
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require "bundler/setup"
4
+ require "test/unit"
5
+ require "mongoid"
6
+ require 'database_cleaner'
7
+
8
+ require File.expand_path("../../lib/mongoid-sequence2", __FILE__)
9
+
10
+ Mongoid.load!("./test/config/mongoid.yml", :test)
11
+
12
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
13
+
14
+ class BaseTest < Test::Unit::TestCase
15
+ def setup
16
+ DatabaseCleaner.start
17
+ end
18
+
19
+ def teardown
20
+ DatabaseCleaner.clean
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ require "test_helper"
2
+
3
+ class UniquenessTest < BaseTest
4
+ def test_single_sequence_uniqueness
5
+ n = 200
6
+
7
+ n.times do
8
+ FirstSequencedModel.create
9
+ end
10
+
11
+ assert_equal FirstSequencedModel.only(:auto_increment).uniq.size, n
12
+ end
13
+
14
+ def test_id_sequence_uniqueness
15
+ n = 200
16
+
17
+ n.times do
18
+ IdSequencedModel.create
19
+ end
20
+
21
+ assert_equal IdSequencedModel.only(:id).uniq.size, n
22
+ end
23
+
24
+ def test_double_sequence_uniqueness
25
+ n = 100
26
+
27
+ n.times do
28
+ FirstSequencedModel.create
29
+ SecondSequencedModel.create
30
+ end
31
+
32
+ assert_equal FirstSequencedModel.only(:auto_increment).uniq.size, n
33
+ assert_equal SecondSequencedModel.only(:auto_increment).uniq.size, n
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-sequence2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - jhjguxin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: 4.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - - <
31
+ - !ruby/object:Gem::Version
32
+ version: 4.1.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: activesupport
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ~>
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: '3.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.9'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ version: '0.9'
61
+ - !ruby/object:Gem::Dependency
62
+ name: database_cleaner
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ version: 0.9.1
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ version: 0.9.1
75
+ description: Mongoid::Sequence2 gives you the ability to specify fields to behave
76
+ like a sequence number (exactly like the "id" column in conventional SQL flavors).
77
+ email:
78
+ - 864248765@qq.com
79
+ executables: []
80
+ extensions: []
81
+ extra_rdoc_files: []
82
+ files:
83
+ - .gitignore
84
+ - Changelogs.md
85
+ - Gemfile
86
+ - LICENSE
87
+ - README.md
88
+ - Rakefile
89
+ - lib/mongoid-sequence2.rb
90
+ - lib/mongoid-sequence2/version.rb
91
+ - mongoid-sequence2.gemspec
92
+ - test/config/mongoid.yml
93
+ - test/consistency_test.rb
94
+ - test/models/first_sequenced_model.rb
95
+ - test/models/id_sequenced_model.rb
96
+ - test/models/paranoia_sequenced_model.rb
97
+ - test/models/second_sequenced_model.rb
98
+ - test/paranoia_test.rb
99
+ - test/test_helper.rb
100
+ - test/uniqueness_test.rb
101
+ homepage: https://github.com/jhjguxin/mongoid-sequence2
102
+ licenses: []
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.0.3
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Specify fields to behave like a sequence number (exactly like the "id" column
124
+ in conventional SQL flavors).
125
+ test_files: []