hashtrain-acts_as_random_id 0.1.2 → 1.0.0
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/README +1 -1
- data/Rakefile +2 -2
- data/lib/acts_as_random_id.rb +16 -37
- data/test/acts_as_random_id_test.rb +19 -21
- metadata +2 -2
data/README
CHANGED
|
@@ -19,4 +19,4 @@ Example
|
|
|
19
19
|
acts_as_random_id :generator => Proc.new { Time.now.to_i }
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
Copyright (c) 2009 hashtrain.com, released under the MIT license
|
|
22
|
+
Copyright (c) 2009 hashtrain.com and author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com), released under the MIT license
|
data/Rakefile
CHANGED
|
@@ -38,8 +38,8 @@ PKG_FILES = FileList[
|
|
|
38
38
|
|
|
39
39
|
spec = Gem::Specification.new do |s|
|
|
40
40
|
s.name = "acts_as_random_id"
|
|
41
|
-
s.version = "
|
|
42
|
-
s.author = "hashtrain.com and author idea Stanislav Pogrebnyak"
|
|
41
|
+
s.version = "1.0.0"
|
|
42
|
+
s.author = "hashtrain.com and author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com)"
|
|
43
43
|
s.email = "mail@hashtrain.com"
|
|
44
44
|
s.homepage = "http://github.com/hashtrain/acts_as_random_id/"
|
|
45
45
|
s.platform = Gem::Platform::RUBY
|
data/lib/acts_as_random_id.rb
CHANGED
|
@@ -1,49 +1,28 @@
|
|
|
1
1
|
module ActsAsRandomId
|
|
2
|
+
|
|
2
3
|
def self.included(base)
|
|
3
|
-
base.send :extend, ClassMethods
|
|
4
|
+
base.send :extend, ClassMethods
|
|
5
|
+
|
|
6
|
+
def ensure_unique_id
|
|
7
|
+
begin
|
|
8
|
+
self.id = yield
|
|
9
|
+
end while self.class.exists?(:id => self.id)
|
|
10
|
+
end
|
|
4
11
|
end
|
|
5
12
|
|
|
6
|
-
module ClassMethods
|
|
7
|
-
def acts_as_random_id(options
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
self.random_id_generator = (options[:generator] || :random_id)
|
|
12
|
-
|
|
13
|
-
def generate_random_id
|
|
14
|
-
if self.random_id_generator.is_a?(Proc)
|
|
15
|
-
self.random_id_generator.call
|
|
16
|
-
elsif self.random_id_generator == :auto_increment
|
|
17
|
-
self.auto_increment
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def acts_as_random_id(options={}, &block)
|
|
15
|
+
before_create do |record|
|
|
16
|
+
if block
|
|
17
|
+
record.ensure_unique_id(&block)
|
|
18
18
|
else
|
|
19
|
-
|
|
19
|
+
record.ensure_unique_id do
|
|
20
|
+
rand(2_147_483_647) + 1 #- mysql type "int 4 bytes"
|
|
21
|
+
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
|
-
|
|
23
|
-
protected
|
|
24
|
-
def auto_increment
|
|
25
|
-
current_id = ActiveRecord::Base.connection.select_value("SELECT max(#{self.primary_key}) FROM #{self.table_name}").to_i
|
|
26
|
-
current_id += rand(10) + 1
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def random_id
|
|
30
|
-
begin
|
|
31
|
-
rand_id = rand(2_147_483_647) + 1 #- mysql type "int 4 bytes"
|
|
32
|
-
end until ActiveRecord::Base.connection.select_value("SELECT #{self.primary_key} FROM #{self.table_name} WHERE #{self.primary_key} = #{rand_id}").blank?
|
|
33
|
-
rand_id
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
send :include, InstanceMethods
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
module InstanceMethods
|
|
41
|
-
protected
|
|
42
|
-
def generate_random_id
|
|
43
|
-
self.id = self.class.generate_random_id
|
|
44
24
|
end
|
|
45
25
|
end
|
|
46
|
-
|
|
47
26
|
end
|
|
48
27
|
|
|
49
28
|
ActiveRecord::Base.send :include, ActsAsRandomId
|
|
@@ -7,35 +7,33 @@ class ActsAsRandomIdTest < Test::Unit::TestCase
|
|
|
7
7
|
class Comment < ActiveRecord::Base
|
|
8
8
|
acts_as_random_id
|
|
9
9
|
end
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
class Group < ActiveRecord::Base
|
|
12
|
-
acts_as_random_id
|
|
12
|
+
acts_as_random_id do
|
|
13
|
+
rand(9) + 1
|
|
14
|
+
end
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
class Article < ActiveRecord::Base
|
|
16
|
-
acts_as_random_id
|
|
18
|
+
acts_as_random_id do
|
|
19
|
+
Time.now.to_i
|
|
20
|
+
end
|
|
17
21
|
end
|
|
18
|
-
|
|
19
|
-
def
|
|
20
|
-
assert_equal [], Comment.all
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def test_type_random_id
|
|
24
|
-
assert Comment.generate_random_id
|
|
22
|
+
|
|
23
|
+
def test_random_id
|
|
25
24
|
assert Comment.create
|
|
26
25
|
end
|
|
27
|
-
|
|
28
|
-
def
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
assert g1.id < g2.id
|
|
26
|
+
|
|
27
|
+
def test_generator_rand_9
|
|
28
|
+
9.times do
|
|
29
|
+
g = Group.create
|
|
30
|
+
assert g.id <= 9 && g.id > 0
|
|
31
|
+
end
|
|
34
32
|
end
|
|
35
|
-
|
|
36
|
-
def
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
|
|
34
|
+
def test_generator_with_time_now
|
|
35
|
+
a = Article.create
|
|
36
|
+
assert_equal Time.now.to_i, a.id
|
|
39
37
|
end
|
|
40
38
|
|
|
41
39
|
end
|
metadata
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hashtrain-acts_as_random_id
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 1.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- hashtrain.com
|
|
8
|
-
- Author idea Stanislav Pogrebnyak
|
|
8
|
+
- Author idea Stanislav Pogrebnyak (stanislav.pogrebnyak@gmail.com)
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|