unique_token 0.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.
Files changed (7) hide show
  1. data/.gitignore +1 -0
  2. data/README.rdoc +28 -0
  3. data/Rakefile +19 -0
  4. data/VERSION +1 -0
  5. data/init.rb +2 -0
  6. data/lib/unique_token.rb +38 -0
  7. metadata +60 -0
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/README.rdoc ADDED
@@ -0,0 +1,28 @@
1
+ = UniqueToken
2
+
3
+ Rails gem for generating a unique token in an Active Record model.
4
+
5
+ == Install
6
+
7
+ gem install unique_token
8
+
9
+ == Usage
10
+
11
+ Let's say you have a Project model with a "token" string column that you want to be a unique identifier. Just add this to your model.
12
+
13
+ class Project < ActiveRecord::Base
14
+ unique_token :token
15
+ end
16
+
17
+ This will generate a random and unique string before each book is created.
18
+
19
+ You can specify multiple columns and options like this.
20
+
21
+ uniquify :token, :salt, :length => 12, :chars => 0..9
22
+
23
+ You can also pass a block to generate your own characters.
24
+
25
+ uniquify :token do
26
+ rand(99999)
27
+ end
28
+
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "unique_token"
8
+ gemspec.summary = "Generate a unique token with Active Record."
9
+ gemspec.description = "Generate a unique token with Active Record."
10
+ gemspec.email = "femoco@gmail.com"
11
+ gemspec.homepage = "http://github.com/femoco/unique_token"
12
+ gemspec.authors = ["Femoco"]
13
+ end
14
+ Jeweler::GemcutterTasks.new
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'unique_token'
2
+
@@ -0,0 +1,38 @@
1
+ module UniqueToken
2
+ def self.included(base)
3
+ base.extend ClassMethod
4
+ end
5
+
6
+ def ensure_unique(name)
7
+ begin
8
+ self[name] = yield
9
+ end while self.class.exists?(name => self[name])
10
+ end
11
+
12
+ module ClassMethods
13
+
14
+ def unique_token(*args, &block)
15
+ options = { :length => 8, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a }
16
+ options.merge!(args.pop) if args.last.kind_of? Hash
17
+ args.each do |name|
18
+ before_create do |record|
19
+ if block
20
+ record.ensure_unique(name, &block)
21
+ else
22
+ record.ensure_unique(name) do
23
+ Array.new(options[:length]) {
24
+ options[:chars].to_a[rand(options[:chars].to_a.size) ]
25
+ }.join
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
36
+ class ActiveRecord::Base
37
+ include UniqueToken
38
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unique_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Femoco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-24 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generate a unique token with Active Record.
17
+ email: femoco@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - .gitignore
26
+ - README.rdoc
27
+ - Rakefile
28
+ - VERSION
29
+ - init.rb
30
+ - lib/unique_token.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/femoco/unique_token
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options:
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.3.5
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Generate a unique token with Active Record.
59
+ test_files: []
60
+