acts_as_random_id 0.0.2.beta
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 +6 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +36 -0
- data/LICENSE +20 -0
- data/README.rdoc +49 -0
- data/Rakefile +6 -0
- data/acts_as_random_id.gemspec +24 -0
- data/lib/acts_as_random_id/model_additions.rb +30 -0
- data/lib/acts_as_random_id/railtie.rb +9 -0
- data/lib/acts_as_random_id/version.rb +3 -0
- data/lib/acts_as_random_id.rb +6 -0
- data/spec/acts_as_random_id/model_additions_spec.rb +60 -0
- data/spec/acts_as_random_id_spec.rb +7 -0
- data/spec/spec_helper.rb +2 -0
- metadata +128 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
acts_as_random_id (0.0.2.beta)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.0.11)
|
10
|
+
activesupport (= 3.0.11)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
i18n (~> 0.5.0)
|
13
|
+
activesupport (3.0.11)
|
14
|
+
builder (2.1.2)
|
15
|
+
diff-lcs (1.1.3)
|
16
|
+
i18n (0.5.0)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
rspec (2.7.0)
|
19
|
+
rspec-core (~> 2.7.0)
|
20
|
+
rspec-expectations (~> 2.7.0)
|
21
|
+
rspec-mocks (~> 2.7.0)
|
22
|
+
rspec-core (2.7.1)
|
23
|
+
rspec-expectations (2.7.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.7.0)
|
26
|
+
supermodel (0.1.6)
|
27
|
+
activemodel (~> 3.0.0)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
acts_as_random_id!
|
34
|
+
rake (= 0.9.2.2)
|
35
|
+
rspec
|
36
|
+
supermodel
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009-2011 Shalva Usubov
|
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,49 @@
|
|
1
|
+
=ActsAsRandomId
|
2
|
+
|
3
|
+
Generating unique random id for ActiveRecord models
|
4
|
+
|
5
|
+
==Example
|
6
|
+
|
7
|
+
class Comment < ActiveRecord::Base
|
8
|
+
acts_as_random_id
|
9
|
+
end
|
10
|
+
|
11
|
+
class Group < ActiveRecord::Base
|
12
|
+
acts_as_random_id do
|
13
|
+
rand(3_14159265) + 1
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Article < ActiveRecord::Base
|
18
|
+
acts_as_random_id :field => :secure_identifier do
|
19
|
+
Time.now.to_i
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
==Install
|
24
|
+
|
25
|
+
gem install hashtrain-acts_as_random_id --source http://gems.github.com
|
26
|
+
|
27
|
+
==Usage
|
28
|
+
|
29
|
+
As you know rails generates field ID easy auto_incriment. Thus, there is an opportunity to check how many objects in the DB. Sometimes it's secret information.
|
30
|
+
|
31
|
+
Using the plugin "acts_as_random_id" could generate a unique value id. Generation of ID will be until you will find a unique value ID. Of course you go to the full responsibility for the range.
|
32
|
+
|
33
|
+
By default, the plugin "acts_as_random_id" generates a unique id in the range of 1 to 2_147_483_647 (mysql and SQLite type integer)
|
34
|
+
|
35
|
+
class Comment < ActiveRecord::Base
|
36
|
+
acts_as_random_id
|
37
|
+
end
|
38
|
+
|
39
|
+
You can specify a range of transfer method "acts_as_random_id" block where the generator is implemented
|
40
|
+
|
41
|
+
class Group < ActiveRecord::Base
|
42
|
+
acts_as_random_id do
|
43
|
+
rand(3_14159265) + 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
==License
|
48
|
+
|
49
|
+
Copyright (c) 2009-2011 Shalva Usubov and author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com), released under the MIT license.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_random_id/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_random_id"
|
7
|
+
s.version = ActsAsRandomId::VERSION
|
8
|
+
s.authors = ["Shaliko Usubov"]
|
9
|
+
s.email = ["shaliko@ezid.ru"]
|
10
|
+
s.homepage = "https://github.com/shaliko/acts_as_random_id"
|
11
|
+
s.summary = %q{Generating random id}
|
12
|
+
s.description = %q{Random ID generator}
|
13
|
+
|
14
|
+
s.rubyforge_project = "acts_as_random_id"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "supermodel"
|
23
|
+
s.add_development_dependency "rake", '0.9.2.2'
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActsAsRandomId
|
2
|
+
module ModelAdditions
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
|
6
|
+
def ensure_unique_id(options)
|
7
|
+
begin
|
8
|
+
self.send "#{options[:field]}=", yield
|
9
|
+
end while self.class.exists?(options[:field] => self.send(options[:field]))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
# Default options:
|
15
|
+
# :field => :id
|
16
|
+
def acts_as_random_id(options={:field => :id}, &block)
|
17
|
+
before_create do |record|
|
18
|
+
if block
|
19
|
+
record.ensure_unique_id(options, &block)
|
20
|
+
else
|
21
|
+
record.ensure_unique_id(options) do
|
22
|
+
rand(2_147_483_647) + 1 # mysql and SQLite type integer
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
class Comment < SuperModel::Base
|
4
|
+
include ActiveModel::Validations::Callbacks
|
5
|
+
include ActsAsRandomId::ModelAdditions
|
6
|
+
|
7
|
+
acts_as_random_id
|
8
|
+
end
|
9
|
+
|
10
|
+
class Payment < SuperModel::Base
|
11
|
+
include ActiveModel::Validations::Callbacks
|
12
|
+
include ActsAsRandomId::ModelAdditions
|
13
|
+
|
14
|
+
acts_as_random_id :field => :secure_identifier do
|
15
|
+
Time.now.to_i
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Group < SuperModel::Base
|
20
|
+
include ActiveModel::Validations::Callbacks
|
21
|
+
include ActsAsRandomId::ModelAdditions
|
22
|
+
|
23
|
+
acts_as_random_id do
|
24
|
+
rand(9) + 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Article < SuperModel::Base
|
29
|
+
include ActiveModel::Validations::Callbacks
|
30
|
+
include ActsAsRandomId::ModelAdditions
|
31
|
+
|
32
|
+
acts_as_random_id do
|
33
|
+
Time.now.to_i
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ActsAsRandomId::ModelAdditions do
|
38
|
+
|
39
|
+
it "adds id upon saving" do
|
40
|
+
Comment.create!.id.should_not be_nil
|
41
|
+
Group.create!.id.should_not be_nil
|
42
|
+
Article.create!.id.should_not be_nil
|
43
|
+
Payment.create!.secure_identifier.should_not be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "in rage 1..9" do
|
47
|
+
9.times do
|
48
|
+
(1..9).should include(Group.create.id)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "Time.now generator" do
|
53
|
+
Article.create.id.should eql(Time.now.to_i)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "assign custom field" do
|
57
|
+
Payment.create.secure_identifier.should eql(Time.now.to_i)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_random_id
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31098185
|
5
|
+
prerelease: 6
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
- beta
|
11
|
+
version: 0.0.2.beta
|
12
|
+
platform: ruby
|
13
|
+
authors:
|
14
|
+
- Shaliko Usubov
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-12-04 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: supermodel
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rake
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - "="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 11
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
- 9
|
61
|
+
- 2
|
62
|
+
- 2
|
63
|
+
version: 0.9.2.2
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
description: Random ID generator
|
67
|
+
email:
|
68
|
+
- shaliko@ezid.ru
|
69
|
+
executables: []
|
70
|
+
|
71
|
+
extensions: []
|
72
|
+
|
73
|
+
extra_rdoc_files: []
|
74
|
+
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- Gemfile.lock
|
79
|
+
- LICENSE
|
80
|
+
- README.rdoc
|
81
|
+
- Rakefile
|
82
|
+
- acts_as_random_id.gemspec
|
83
|
+
- lib/acts_as_random_id.rb
|
84
|
+
- lib/acts_as_random_id/model_additions.rb
|
85
|
+
- lib/acts_as_random_id/railtie.rb
|
86
|
+
- lib/acts_as_random_id/version.rb
|
87
|
+
- spec/acts_as_random_id/model_additions_spec.rb
|
88
|
+
- spec/acts_as_random_id_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: https://github.com/shaliko/acts_as_random_id
|
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
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ">"
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: 25
|
113
|
+
segments:
|
114
|
+
- 1
|
115
|
+
- 3
|
116
|
+
- 1
|
117
|
+
version: 1.3.1
|
118
|
+
requirements: []
|
119
|
+
|
120
|
+
rubyforge_project: acts_as_random_id
|
121
|
+
rubygems_version: 1.8.11
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: Generating random id
|
125
|
+
test_files:
|
126
|
+
- spec/acts_as_random_id/model_additions_spec.rb
|
127
|
+
- spec/acts_as_random_id_spec.rb
|
128
|
+
- spec/spec_helper.rb
|