Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit 88fe79404754408f320d2a0cceb29eb87723b14c 0 parents
Ryan Bates authored
Showing with 114 additions and 0 deletions.
  1. +3 −0  .gitignore
  2. +62 −0 README.rdoc
  3. +14 −0 Rakefile
  4. +35 −0 lib/uniquify.rb
3  .gitignore
@@ -0,0 +1,3 @@
+pkg
+doc
+Manifest
62 README.rdoc
@@ -0,0 +1,62 @@
+= Uniquify
+
+Rails gem for generating a unique token in an Active Record model.
+
+This is from Railscasts episode #135 showing how to make a gem.
+
+http://railscasts.com/episodes/135-making-a-gem
+
+
+== Install
+
+ gem install ryanb-uniquify --source http://gems.github.com
+
+
+== Usage
+
+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.
+
+ class Project < ActiveRecord::Base
+ uniquify :token
+ end
+
+This will generate a random and unique string before each book is
+created.
+
+You can specify multiple columns ands options like this.
+
+ uniquify :token, :salt, :length => 12, :chars => 0..9
+
+You can also pass a block to generate your own characters.
+
+ uniquify :token do
+ rand(99999)
+ end
+
+If the generated value already exists, the block will be run again
+until the value is unique.
+
+
+== License
+
+Copyright (c) 2008 Ryan Bates
+
+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:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+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.
14 Rakefile
@@ -0,0 +1,14 @@
+require 'rubygems'
+require 'rake'
+require 'echoe'
+
+Echoe.new('uniquify', '0.1.0') do |p|
+ p.description = "Generate a unique token with Active Record."
+ p.url = "http://github.com/ryanb/uniquify"
+ p.author = "Ryan Bates"
+ p.email = "ryan@railscasts.com"
+ p.ignore_pattern = ["tmp/*", "script/*"]
+ p.development_dependencies = []
+end
+
+Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
35 lib/uniquify.rb
@@ -0,0 +1,35 @@
+module Uniquify
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def ensure_unique(name)
+ begin
+ self[name] = yield
+ end while self.class.exists?(name => self[name])
+ end
+
+ module ClassMethods
+
+ def uniquify(*args, &block)
+ options = { :length => 8, :chars => ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a }
+ options.merge!(args.pop) if args.last.kind_of? Hash
+ args.each do |name|
+ before_create do |record|
+ if block
+ record.ensure_unique(name, &block)
+ else
+ record.ensure_unique(name) do
+ Array.new(options[:length]) { options[:chars].to_a[rand(options[:chars].to_a.size)] }.join
+ end
+ end
+ end
+ end
+ end
+
+ end
+end
+
+class ActiveRecord::Base
+ include Uniquify
+end
Please sign in to comment.
Something went wrong with that request. Please try again.