Permalink
Browse files

adding episode 183

  • Loading branch information...
1 parent 078e0c0 commit 0eee9eac29aa83cc38a3b0920639912fdbec8148 @ryanb committed Oct 12, 2009
View
@@ -0,0 +1,16 @@
+Railscasts Episode #183: Gemcutter & Jeweler
+
+http://railscasts.com/episodes/183
+
+Commands
+
+ sudo gem update --system
+ sudo gem install gemcutter
+ gem tumble
+ gem build uniquify.gemspec
+ gem push uniquify-0.1.0.gem
+ sudo gem install jeweler
+ rake --tasks
+ rake version:write
+ rake version:bump:minor
+ rake gemcutter:release
@@ -0,0 +1,3 @@
+pkg
+doc
+Manifest
No changes.
@@ -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.
@@ -0,0 +1,19 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gemspec|
+ gemspec.name = "uniquify"
+ gemspec.summary = "Generate a unique token with Active Record."
+ gemspec.description = "Generate a unique token with Active Record."
+ gemspec.email = "ryan@railscasts.com"
+ gemspec.homepage = "http://github.com/ryanb/uniquify"
+ gemspec.authors = ["Ryan Bates"]
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
+end
+
+Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1 @@
+0.1.0
@@ -0,0 +1 @@
+require 'uniquify'
@@ -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
@@ -0,0 +1,41 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE
+# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{uniquify}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Ryan Bates"]
+ s.date = %q{2009-10-11}
+ s.description = %q{Generate a unique token with Active Record.}
+ s.email = %q{ryan@railscasts.com}
+ s.extra_rdoc_files = [
+ "README.rdoc"
+ ]
+ s.files = [
+ ".gitignore",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "init.rb",
+ "lib/uniquify.rb"
+ ]
+ s.homepage = %q{http://github.com/ryanb/uniquify}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Generate a unique token with Active Record.}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end

0 comments on commit 0eee9ea

Please sign in to comment.