Browse files

adding episode 135

  • Loading branch information...
1 parent c33a49b commit 353ad5a82cefaf6ba1115161e1fc0eb59caa7ca5 @ryanb committed Nov 9, 2008
View
20 episode-135/README
@@ -0,0 +1,20 @@
+Railscasts Episode #135: Making a Gem
+
+http://railscasts.com/episodes/135
+
+Commands
+
+ sudo gem install echoe
+ mkdir -p uniquify/lib
+ touch README.rdoc Rakefile lib/uniquify.rb
+ rake -T
+ rake manifest
+ rake install
+ rake build_gemspec
+ git init
+ git add .
+ git commit -m "initial commit"
+ git remote add origin git@github.com:ryanb/uniquify.git
+ git push
+ touch CHANGELOG
+ touch init.rb
View
3 episode-135/uniquify/.gitignore
@@ -0,0 +1,3 @@
+pkg
+doc
+Manifest
View
0 episode-135/uniquify/CHANGELOG
No changes.
View
62 episode-135/uniquify/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.
View
14 episode-135/uniquify/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 }
View
1 episode-135/uniquify/init.rb
@@ -0,0 +1 @@
+require 'uniquify'
View
35 episode-135/uniquify/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
View
29 episode-135/uniquify/uniquify.gemspec
@@ -0,0 +1,29 @@
+Gem::Specification.new do |s|
+ s.name = %q{uniquify}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Ryan Bates"]
+ s.date = %q{2008-11-09}
+ s.description = %q{Generate a unique token with Active Record.}
+ s.email = %q{ryan@railscasts.com}
+ s.extra_rdoc_files = ["CHANGELOG", "lib/uniquify.rb", "README.rdoc"]
+ s.files = ["CHANGELOG", "init.rb", "lib/uniquify.rb", "Manifest", "Rakefile", "README.rdoc", "uniquify.gemspec"]
+ s.has_rdoc = true
+ s.homepage = %q{http://github.com/ryanb/uniquify}
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Uniquify", "--main", "README.rdoc"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{uniquify}
+ s.rubygems_version = %q{1.2.0}
+ 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 = 2
+
+ if current_version >= 3 then
+ else
+ end
+ else
+ end
+end

0 comments on commit 353ad5a

Please sign in to comment.