uniquify-uuid 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +45 -0
- data/Rakefile +2 -0
- data/lib/uniquify-uuid.rb +26 -0
- data/lib/uniquify-uuid/uuid.rb +22 -0
- data/lib/uniquify-uuid/version.rb +3 -0
- data/uniquify-uuid.gemspec +23 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Uniquify
|
2
|
+
|
3
|
+
This gem helps to generate unique tokens for ActiveRecord models. It uses UUID as default token algorithm.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Add gem to your Gemfile:
|
8
|
+
|
9
|
+
gem 'uniquify-uuid'
|
10
|
+
|
11
|
+
then bo bundle:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
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.
|
18
|
+
|
19
|
+
class Project < ActiveRecord::Base
|
20
|
+
uniquify :token
|
21
|
+
end
|
22
|
+
|
23
|
+
This will generate a random and unique UUID string before each project is created.
|
24
|
+
|
25
|
+
You can specify multiple columns like this.
|
26
|
+
|
27
|
+
uniquify :token, :salt
|
28
|
+
|
29
|
+
You can also pass a block to generate your own random token.
|
30
|
+
|
31
|
+
uniquify :token do
|
32
|
+
rand(99999)
|
33
|
+
end
|
34
|
+
|
35
|
+
If the generated value already exists, the block will be run again until the value is unique.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
Copyright © 2008 Ryan Bates
|
40
|
+
|
41
|
+
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:
|
42
|
+
|
43
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
44
|
+
|
45
|
+
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,26 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'uniquify-uuid', 'uuid')
|
2
|
+
|
3
|
+
module Uniquify
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module InstanceMethods
|
7
|
+
def ensure_unique(name)
|
8
|
+
begin
|
9
|
+
self[name] = yield
|
10
|
+
end while self.class.exists?(name => self[name])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def uniquify *args, &block
|
16
|
+
args.each do |name|
|
17
|
+
block = Proc.new { Uniquify::UUID.generate } unless block_given?
|
18
|
+
before_create { |record| record.ensure_unique(name, &block) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class ActiveRecord::Base
|
25
|
+
include Uniquify
|
26
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Uniquify; end
|
2
|
+
|
3
|
+
if RUBY_VERSION > '1.9'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
module Uniquify::UUID
|
7
|
+
extend self
|
8
|
+
def generate
|
9
|
+
SecureRandom.uuid
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
else
|
14
|
+
require 'simple_uuid'
|
15
|
+
|
16
|
+
module Uniquify::UUID
|
17
|
+
extend self
|
18
|
+
def generate
|
19
|
+
SimpleUUID::UUID.new.to_guid
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'uniquify-uuid/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'uniquify-uuid'
|
7
|
+
s.version = Uniquify::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['rwz']
|
10
|
+
s.email = ['rwz@duckroll.ru']
|
11
|
+
s.homepage = 'http://github.com/rwz/uniquify-uuid'
|
12
|
+
s.summary = %q{Generate a unique UUID token with Active Record.}
|
13
|
+
s.description = %q{The same as Ryan Bates uniquify gem, but uses UUID as default to generate tokens.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "uniquify-uuid"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = %w(lib)
|
21
|
+
|
22
|
+
s.add_dependency 'simple_uuid', '0.1.2'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uniquify-uuid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- rwz
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-22 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: simple_uuid
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 31
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
version: 0.1.2
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: The same as Ryan Bates uniquify gem, but uses UUID as default to generate tokens.
|
37
|
+
email:
|
38
|
+
- rwz@duckroll.ru
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/uniquify-uuid.rb
|
51
|
+
- lib/uniquify-uuid/uuid.rb
|
52
|
+
- lib/uniquify-uuid/version.rb
|
53
|
+
- uniquify-uuid.gemspec
|
54
|
+
homepage: http://github.com/rwz/uniquify-uuid
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: uniquify-uuid
|
83
|
+
rubygems_version: 1.7.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Generate a unique UUID token with Active Record.
|
87
|
+
test_files: []
|
88
|
+
|