hashtrain-acts_as_random_id 0.1.2
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/MIT-LICENSE +20 -0
- data/README +22 -0
- data/Rakefile +56 -0
- data/lib/acts_as_random_id.rb +49 -0
- data/test/acts_as_random_id_test.rb +41 -0
- data/test/database.yml +36 -0
- data/test/schema.rb +21 -0
- data/test/test_helper.rb +33 -0
- metadata +61 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2009 hashtrain.com
|
|
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
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
ActsAsRandomId
|
|
2
|
+
=============
|
|
3
|
+
|
|
4
|
+
Generating unique random id for ActiveRecord models
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Example
|
|
8
|
+
=======
|
|
9
|
+
|
|
10
|
+
class Comment < ActiveRecord::Base
|
|
11
|
+
acts_as_random_id
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Group < ActiveRecord::Base
|
|
15
|
+
acts_as_random_id :generator => :auto_increment
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class Article < ActiveRecord::Base
|
|
19
|
+
acts_as_random_id :generator => Proc.new { Time.now.to_i }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2009 hashtrain.com, released under the MIT license
|
data/Rakefile
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'rake/testtask'
|
|
3
|
+
require 'rake/rdoctask'
|
|
4
|
+
require 'rake/gempackagetask'
|
|
5
|
+
|
|
6
|
+
#require 'rubygems'
|
|
7
|
+
#Gem::manage_gems
|
|
8
|
+
#require 'rake/gempackagetask'
|
|
9
|
+
|
|
10
|
+
desc 'Default: run unit tests.'
|
|
11
|
+
task :default => :test
|
|
12
|
+
|
|
13
|
+
desc 'Test the acts_as_random_id plugin.'
|
|
14
|
+
Rake::TestTask.new(:test) do |t|
|
|
15
|
+
t.libs << 'lib'
|
|
16
|
+
t.pattern = 'test/**/*_test.rb'
|
|
17
|
+
t.verbose = true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
desc 'Generate documentation for the acts_as_random_id plugin.'
|
|
21
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
22
|
+
rdoc.rdoc_dir = 'rdoc'
|
|
23
|
+
rdoc.title = 'ActsAsRandomId'
|
|
24
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
|
25
|
+
rdoc.rdoc_files.include('README')
|
|
26
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
PKG_FILES = FileList[
|
|
31
|
+
'[a-zA-Z]*',
|
|
32
|
+
'generators/**/*',
|
|
33
|
+
'lib/**/*',
|
|
34
|
+
'rails/**/*',
|
|
35
|
+
'tasks/**/*',
|
|
36
|
+
'test/**/*'
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
spec = Gem::Specification.new do |s|
|
|
40
|
+
s.name = "acts_as_random_id"
|
|
41
|
+
s.version = "0.1.2.3"
|
|
42
|
+
s.author = "hashtrain.com and author idea Stanislav Pogrebnyak"
|
|
43
|
+
s.email = "mail@hashtrain.com"
|
|
44
|
+
s.homepage = "http://github.com/hashtrain/acts_as_random_id/"
|
|
45
|
+
s.platform = Gem::Platform::RUBY
|
|
46
|
+
s.summary = "Generate a random id for ActiveRecord models"
|
|
47
|
+
s.files = PKG_FILES.to_a
|
|
48
|
+
s.require_path = "lib"
|
|
49
|
+
s.has_rdoc = false
|
|
50
|
+
s.extra_rdoc_files = ["README"]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
desc 'Turn this plugin into a gem.'
|
|
54
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
|
55
|
+
pkg.gem_spec = spec
|
|
56
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module ActsAsRandomId
|
|
2
|
+
def self.included(base)
|
|
3
|
+
base.send :extend, ClassMethods
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
module ClassMethods
|
|
7
|
+
def acts_as_random_id(options = {})
|
|
8
|
+
cattr_accessor :random_id_generator
|
|
9
|
+
before_create :generate_random_id
|
|
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
|
|
18
|
+
else
|
|
19
|
+
self.random_id
|
|
20
|
+
end
|
|
21
|
+
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
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
ActiveRecord::Base.send :include, ActsAsRandomId
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#require 'test/unit'
|
|
2
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
|
3
|
+
|
|
4
|
+
class ActsAsRandomIdTest < Test::Unit::TestCase
|
|
5
|
+
load_schema
|
|
6
|
+
|
|
7
|
+
class Comment < ActiveRecord::Base
|
|
8
|
+
acts_as_random_id
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
class Group < ActiveRecord::Base
|
|
12
|
+
acts_as_random_id :generator => :auto_increment
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
class Article < ActiveRecord::Base
|
|
16
|
+
acts_as_random_id :generator => Proc.new { Time.now.to_i }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def test_should_empty
|
|
20
|
+
assert_equal [], Comment.all
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def test_type_random_id
|
|
24
|
+
assert Comment.generate_random_id
|
|
25
|
+
assert Comment.create
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_type_auto_incriment
|
|
29
|
+
assert Group.generate_random_id
|
|
30
|
+
g1 = Group.create
|
|
31
|
+
g2 = Group.create
|
|
32
|
+
|
|
33
|
+
assert g1.id < g2.id
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_generator_proc
|
|
37
|
+
puts Article.generate_random_id
|
|
38
|
+
assert Article.generate_random_id
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
data/test/database.yml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# MySQL (default setup). Versions 4.1 and 5.0 are recommended.
|
|
2
|
+
#
|
|
3
|
+
# Install the MySQL driver:
|
|
4
|
+
# gem install mysql
|
|
5
|
+
# On MacOS X:
|
|
6
|
+
# gem install mysql -- --include=/usr/local/lib
|
|
7
|
+
# On Windows:
|
|
8
|
+
# gem install mysql
|
|
9
|
+
# Choose the win32 build.
|
|
10
|
+
# Install MySQL and put its /bin directory on your path.
|
|
11
|
+
#
|
|
12
|
+
# And be sure to use new-style password hashing:
|
|
13
|
+
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
|
|
14
|
+
development:
|
|
15
|
+
adapter: mysql
|
|
16
|
+
database: acts_as_random_id_development
|
|
17
|
+
username: root
|
|
18
|
+
password:
|
|
19
|
+
socket: /var/run/mysqld/mysqld.sock
|
|
20
|
+
|
|
21
|
+
# Warning: The database defined as 'test' will be erased and
|
|
22
|
+
# re-generated from your development database when you run 'rake'.
|
|
23
|
+
# Do not set this db to the same as development or production.
|
|
24
|
+
test:
|
|
25
|
+
adapter: mysql
|
|
26
|
+
database: acts_as_random_id_test
|
|
27
|
+
username: root
|
|
28
|
+
password:
|
|
29
|
+
socket: /var/run/mysqld/mysqld.sock
|
|
30
|
+
|
|
31
|
+
production:
|
|
32
|
+
adapter: mysql
|
|
33
|
+
database: acts_as_random_id_production
|
|
34
|
+
username: root
|
|
35
|
+
password:
|
|
36
|
+
socket: /var/run/mysqld/mysqld.sock
|
data/test/schema.rb
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
ActiveRecord::Schema.define(:version => 20090217091952) do
|
|
2
|
+
|
|
3
|
+
create_table "comments", :force => true do |t|
|
|
4
|
+
t.text "comment"
|
|
5
|
+
t.datetime "created_at"
|
|
6
|
+
t.datetime "updated_at"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
create_table "groups", :force => true do |t|
|
|
10
|
+
t.string "name"
|
|
11
|
+
t.datetime "created_at"
|
|
12
|
+
t.datetime "updated_at"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
create_table "articles", :force => true do |t|
|
|
16
|
+
t.text "content"
|
|
17
|
+
t.datetime "created_at"
|
|
18
|
+
t.datetime "updated_at"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
|
2
|
+
ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
|
|
3
|
+
|
|
4
|
+
require 'test/unit'
|
|
5
|
+
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
|
|
6
|
+
|
|
7
|
+
def load_schema
|
|
8
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
|
9
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
|
10
|
+
|
|
11
|
+
db_adapter = ENV['DB'] || 'mysql'
|
|
12
|
+
|
|
13
|
+
# no db passed, try one of these fine config-free DBs before bombing.
|
|
14
|
+
db_adapter ||=
|
|
15
|
+
begin
|
|
16
|
+
require 'rubygems'
|
|
17
|
+
require 'sqlite'
|
|
18
|
+
'sqlite'
|
|
19
|
+
rescue MissingSourceFile
|
|
20
|
+
begin
|
|
21
|
+
require 'sqlite3' 'sqlite3'
|
|
22
|
+
rescue MissingSourceFile
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
if db_adapter.nil?
|
|
27
|
+
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
|
31
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
|
32
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: hashtrain-acts_as_random_id
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- hashtrain.com
|
|
8
|
+
- Author idea Stanislav Pogrebnyak
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
|
|
13
|
+
date: 2009-04-25 00:00:00 -07:00
|
|
14
|
+
default_executable:
|
|
15
|
+
dependencies: []
|
|
16
|
+
|
|
17
|
+
description:
|
|
18
|
+
email: mail@hashtrain.com
|
|
19
|
+
executables: []
|
|
20
|
+
|
|
21
|
+
extensions: []
|
|
22
|
+
|
|
23
|
+
extra_rdoc_files:
|
|
24
|
+
- README
|
|
25
|
+
files:
|
|
26
|
+
- MIT-LICENSE
|
|
27
|
+
- Rakefile
|
|
28
|
+
- README
|
|
29
|
+
- lib/acts_as_random_id.rb
|
|
30
|
+
- test/acts_as_random_id_test.rb
|
|
31
|
+
- test/schema.rb
|
|
32
|
+
- test/test_helper.rb
|
|
33
|
+
- test/database.yml
|
|
34
|
+
has_rdoc: true
|
|
35
|
+
homepage: http://github.com/hashtrain/acts_as_random_id
|
|
36
|
+
post_install_message:
|
|
37
|
+
rdoc_options: []
|
|
38
|
+
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: "0"
|
|
46
|
+
version:
|
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
requirements: []
|
|
54
|
+
|
|
55
|
+
rubyforge_project:
|
|
56
|
+
rubygems_version: 1.2.0
|
|
57
|
+
signing_key:
|
|
58
|
+
specification_version: 2
|
|
59
|
+
summary: Generate a random id for ActiveRecord models
|
|
60
|
+
test_files: []
|
|
61
|
+
|