uniquify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/README.rdoc +62 -0
- data/Rakefile +19 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/uniquify.rb +35 -0
- metadata +60 -0
data/.gitignore
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= Uniquify
|
2
|
+
|
3
|
+
Rails gem for generating a unique token in an Active Record model.
|
4
|
+
|
5
|
+
This is from Railscasts episode #135 showing how to make a gem.
|
6
|
+
|
7
|
+
http://railscasts.com/episodes/135-making-a-gem
|
8
|
+
|
9
|
+
|
10
|
+
== Install
|
11
|
+
|
12
|
+
gem install ryanb-uniquify --source http://gems.github.com
|
13
|
+
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
Let's say you have a Project model with a "token" string column that you
|
18
|
+
want to be a unique identifier. Just add this to your model.
|
19
|
+
|
20
|
+
class Project < ActiveRecord::Base
|
21
|
+
uniquify :token
|
22
|
+
end
|
23
|
+
|
24
|
+
This will generate a random and unique string before each book is
|
25
|
+
created.
|
26
|
+
|
27
|
+
You can specify multiple columns ands options like this.
|
28
|
+
|
29
|
+
uniquify :token, :salt, :length => 12, :chars => 0..9
|
30
|
+
|
31
|
+
You can also pass a block to generate your own characters.
|
32
|
+
|
33
|
+
uniquify :token do
|
34
|
+
rand(99999)
|
35
|
+
end
|
36
|
+
|
37
|
+
If the generated value already exists, the block will be run again
|
38
|
+
until the value is unique.
|
39
|
+
|
40
|
+
|
41
|
+
== License
|
42
|
+
|
43
|
+
Copyright (c) 2008 Ryan Bates
|
44
|
+
|
45
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
46
|
+
a copy of this software and associated documentation files (the
|
47
|
+
"Software"), to deal in the Software without restriction, including
|
48
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
49
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
50
|
+
permit persons to whom the Software is furnished to do so, subject to
|
51
|
+
the following conditions:
|
52
|
+
|
53
|
+
The above copyright notice and this permission notice shall be
|
54
|
+
included in all copies or substantial portions of the Software.
|
55
|
+
|
56
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
57
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
58
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
59
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
60
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
61
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
62
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
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 = "uniquify"
|
8
|
+
gemspec.summary = "Generate a unique token with Active Record."
|
9
|
+
gemspec.description = "Generate a unique token with Active Record."
|
10
|
+
gemspec.email = "ryan@railscasts.com"
|
11
|
+
gemspec.homepage = "http://github.com/ryanb/uniquify"
|
12
|
+
gemspec.authors = ["Ryan Bates"]
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gems.gemcutter.org"
|
17
|
+
end
|
18
|
+
|
19
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'uniquify'
|
data/lib/uniquify.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Uniquify
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
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 uniquify(*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]) { options[:chars].to_a[rand(options[:chars].to_a.size)] }.join
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class ActiveRecord::Base
|
34
|
+
include Uniquify
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uniquify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Bates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Generate a unique token with Active Record.
|
17
|
+
email: ryan@railscasts.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/uniquify.rb
|
31
|
+
has_rdoc: true
|
32
|
+
homepage: http://github.com/ryanb/uniquify
|
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
|
+
|