ploymorphic_wuid 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,9 +13,12 @@ class CreateWuids < ActiveRecord::Migration
13
13
  create_table :wuids do |t|
14
14
  t.integer :wuidable_id
15
15
  t.string :wuidable_type
16
+ t.string :rand_token
16
17
  t.integer :reference_to #optional
17
18
  t.timestamps
18
19
  end
20
+
21
+ add_index :wuids, :rand_token, :unique => true, :name => 'token_index'
19
22
  end
20
23
 
21
24
  def self.down
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.3
1
+ 0.5.0
@@ -1,3 +1,4 @@
1
1
  require 'wuid/ploymorphic_module'
2
2
  require 'wuid/wuid'
3
+ require 'wuid/rand_gen'
3
4
  ActiveRecord::Base.send :include, PloymorphicModule
@@ -27,7 +27,12 @@ module PloymorphicModule
27
27
  end
28
28
 
29
29
  def create_wuid
30
- wuid.create
30
+ begin
31
+ wuid.create!
32
+ rescue ActiveRecord::StatementInvalid => e
33
+ logger.error " create wuid faild with #{e.message}"
34
+ retry
35
+ end
31
36
  end
32
37
 
33
38
  end
@@ -0,0 +1,11 @@
1
+ require 'sha1'
2
+
3
+ class RandGen
4
+
5
+ def self.generate len=11
6
+ srand
7
+ seed = "--#{rand(999999999)}--#{Time.now}--"
8
+ Digest::SHA1.hexdigest(seed)[0,len]
9
+ end
10
+
11
+ end
data/lib/wuid/wuid.rb CHANGED
@@ -1,5 +1,50 @@
1
1
  class Wuid < ActiveRecord::Base
2
+
2
3
  belongs_to :wuidable, :polymorphic => true
3
- end
4
4
 
5
+ before_validation do |record|
6
+ record.rand_token = RandGen.generate
7
+ end
8
+
9
+ validates_uniqueness_of :rand_token
5
10
 
11
+ def self.find(*args)
12
+ options = args.extract_options!
13
+ validate_find_options(options)
14
+ set_readonly_option!(options)
15
+ case args.first
16
+ when :first
17
+ find_initial(options)
18
+ when :last
19
+ find_last(options)
20
+ when :all
21
+ find_every(options)
22
+ else
23
+ expects_array = args.first.is_a?(Array)
24
+ return args.first if expects_array && args.first.empty?
25
+ case args.size
26
+ when 0
27
+ raise RecordNotFound, "Couldn't find #{name} without an ID"
28
+ when 1
29
+ if args[0].is_a?(Integer) || (args[0].is_a?(String) && args[0] =~ /^\d{1,}$/)
30
+ super
31
+ else
32
+ if args[0].is_a?(Array)
33
+ if args[0].all?{|i| i.is_a?(Integer)} || args[0].all?{|i| i=~ /^\d{1,}$/}
34
+ super
35
+ else
36
+ origin_find(:all, :conditions => ["rand_token in (?)", args[0]])
37
+ end
38
+ else
39
+ origin_find(:first, :conditions => ["rand_token = ?", args[0]])
40
+ end
41
+ end # if args[0].is_a?(Integer) || (args[0].is_a?(String) && args[0] =~ /^\d{1,}$/)
42
+ end # case args.size
43
+ end #case args.first
44
+ end
45
+
46
+ class << self
47
+ alias origin_find find
48
+ end
49
+
50
+ end
Binary file
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ploymorphic_wuid}
8
- s.version = "0.4.3"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["tim.teng"]
12
- s.date = %q{2010-04-28}
12
+ s.date = %q{2010-06-23}
13
13
  s.description = %q{ This is a gem to generate an unique id in your application world.}
14
14
  s.email = %q{tim.rubist@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,7 +24,9 @@ Gem::Specification.new do |s|
24
24
  "VERSION",
25
25
  "lib/ploymorphic_wuid.rb",
26
26
  "lib/wuid/ploymorphic_module.rb",
27
+ "lib/wuid/rand_gen.rb",
27
28
  "lib/wuid/wuid.rb",
29
+ "pkg/ploymorphic_wuid-0.5.0.gem",
28
30
  "ploymorphic_wuid.gemspec",
29
31
  "test/helper.rb",
30
32
  "test/test_ploymorphic_wuid.rb"
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 4
8
- - 3
9
- version: 0.4.3
7
+ - 5
8
+ - 0
9
+ version: 0.5.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - tim.teng
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-28 00:00:00 +08:00
17
+ date: 2010-06-23 00:00:00 +08:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -35,7 +35,9 @@ files:
35
35
  - VERSION
36
36
  - lib/ploymorphic_wuid.rb
37
37
  - lib/wuid/ploymorphic_module.rb
38
+ - lib/wuid/rand_gen.rb
38
39
  - lib/wuid/wuid.rb
40
+ - pkg/ploymorphic_wuid-0.5.0.gem
39
41
  - ploymorphic_wuid.gemspec
40
42
  - test/helper.rb
41
43
  - test/test_ploymorphic_wuid.rb