sr_uniquify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +0 -0
- data/Manifest +7 -0
- data/README.rdoc +43 -0
- data/Rakefile +14 -0
- data/init.rb +1 -0
- data/lib/uniquify.rb +35 -0
- data/uniquify.gemspec +30 -0
- metadata +78 -0
data/CHANGELOG
ADDED
File without changes
|
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= Uniquify
|
2
|
+
Rails gem for generating a unique token in an Active Record model.
|
3
|
+
|
4
|
+
This is from Railscasts episode #135 showing how to make a gem.
|
5
|
+
|
6
|
+
railscasts.com/episodes/135-making-a-gem
|
7
|
+
|
8
|
+
== Install
|
9
|
+
|
10
|
+
gem install spncrgr-uniquify --source http://gems.github.com
|
11
|
+
|
12
|
+
To install as a plugin:
|
13
|
+
|
14
|
+
rails plugin install https://spncrgr@github.com/spncrgr/uniquify.git
|
15
|
+
|
16
|
+
== Usage
|
17
|
+
|
18
|
+
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.
|
19
|
+
|
20
|
+
class Project < ActiveRecord::Base
|
21
|
+
uniquify :token
|
22
|
+
end
|
23
|
+
This will generate a random and unique string before each record is created.
|
24
|
+
|
25
|
+
You can specify multiple columns and options like this.
|
26
|
+
|
27
|
+
uniquify :token, :salt, :length => 12, :chars => 0..9
|
28
|
+
You can also pass a block to generate your own characters.
|
29
|
+
|
30
|
+
uniquify :token do
|
31
|
+
rand(99999)
|
32
|
+
end
|
33
|
+
If the generated value already exists, the block will be run again until the value is unique.
|
34
|
+
|
35
|
+
== License
|
36
|
+
|
37
|
+
Copyright © 2008 Ryan Bates
|
38
|
+
|
39
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
40
|
+
|
41
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('uniquify', '0.1.0') do |p|
|
6
|
+
p.description = "Generate a unique token with Active Record"
|
7
|
+
p.url = "http://github.com/spncrgr/uniquify"
|
8
|
+
p.author = "Spencer Roach"
|
9
|
+
p.email = "sroach@investormedia.com"
|
10
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
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
|
data/uniquify.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{sr_uniquify}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Spencer Roach"]
|
9
|
+
s.date = %q{2010-11-30}
|
10
|
+
s.description = %q{Generate a unique token with Active Record}
|
11
|
+
s.email = %q{sroach@investormedia.com}
|
12
|
+
s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/uniquify.rb"]
|
13
|
+
s.files = ["CHANGELOG", "README.rdoc", "Rakefile", "init.rb", "lib/uniquify.rb", "uniquify.gemspec", "Manifest"]
|
14
|
+
s.homepage = %q{http://github.com/spncrgr/uniquify}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Uniquify", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{uniquify}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Generate a unique token with Active Record}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sr_uniquify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Spencer Roach
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-30 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Generate a unique token with Active Record
|
22
|
+
email: sroach@investormedia.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- CHANGELOG
|
29
|
+
- README.rdoc
|
30
|
+
- lib/uniquify.rb
|
31
|
+
files:
|
32
|
+
- CHANGELOG
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- init.rb
|
36
|
+
- lib/uniquify.rb
|
37
|
+
- uniquify.gemspec
|
38
|
+
- Manifest
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/spncrgr/uniquify
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --line-numbers
|
46
|
+
- --inline-source
|
47
|
+
- --title
|
48
|
+
- Uniquify
|
49
|
+
- --main
|
50
|
+
- README.rdoc
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 2
|
69
|
+
version: "1.2"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: uniquify
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Generate a unique token with Active Record
|
77
|
+
test_files: []
|
78
|
+
|