ryanb-uniquify 0.1.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 (5) hide show
  1. data/README.rdoc +62 -0
  2. data/Rakefile +14 -0
  3. data/lib/uniquify.rb +35 -0
  4. data/uniquify.gemspec +29 -0
  5. metadata +63 -0
@@ -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.
@@ -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/ryanb/uniquify"
8
+ p.author = "Ryan Bates"
9
+ p.email = "ryan@railscasts.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 }
@@ -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
@@ -0,0 +1,29 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{uniquify}
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["Ryan Bates"]
7
+ s.date = %q{2008-11-09}
8
+ s.description = %q{Generate a unique token with Active Record.}
9
+ s.email = %q{ryan@railscasts.com}
10
+ s.extra_rdoc_files = ["lib/uniquify.rb", "README.rdoc"]
11
+ s.files = ["lib/uniquify.rb", "Rakefile", "README.rdoc", "Manifest", "uniquify.gemspec"]
12
+ s.has_rdoc = true
13
+ s.homepage = %q{http://github.com/ryanb/uniquify}
14
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Uniquify", "--main", "README.rdoc"]
15
+ s.require_paths = ["lib"]
16
+ s.rubyforge_project = %q{uniquify}
17
+ s.rubygems_version = %q{1.2.0}
18
+ s.summary = %q{Generate a unique token with Active Record.}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if current_version >= 3 then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ryanb-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: 2008-11-09 00:00:00 -08: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
+ - lib/uniquify.rb
24
+ - README.rdoc
25
+ files:
26
+ - lib/uniquify.rb
27
+ - Rakefile
28
+ - README.rdoc
29
+ - Manifest
30
+ - uniquify.gemspec
31
+ has_rdoc: true
32
+ homepage: http://github.com/ryanb/uniquify
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - Uniquify
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.2"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: uniquify
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Generate a unique token with Active Record.
62
+ test_files: []
63
+