#135 Making a Gem
Want to create a Ruby Gem instead of a Rails plugin? In this episode I will walk you through creating a gem to extend Rails.
- Download:
- source codeProject Files in Zip (4.59 KB)
- mp4Full Size H.264 Video (14 MB)
- m4vSmaller H.264 Video (9.39 MB)
- webmFull Size VP8 Video (21.4 MB)
- ogvFull Size Theora Video (16.8 MB)
Resources
bash
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
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
Rakefile
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 }
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 }
lib/uniquify.rb
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
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
init.rb
require 'uniquify'
require 'uniquify'
