Saturday, February 21, 2009
Creating New Gem in Ruby on Rails
In the Past I have created gem for my plugin. Now I am sharing my approach that I have used to do it. Finally I feel how easy it is.
We can do it just add gem specifications within rake file (vendor\plugins\
Classes to be require
require 'rubygems'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/packagetask'
require 'rake/gempackagetask'
Define Specification
desc 'Default: run unit tests.'
task :default => :test
desc 'description of your gem'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
desc 'Generate documentation for your gem.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'display title'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
spec = Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.summary = "Ruby based make-like utility."
s.name = 'plugin_name'
s.version = '0.0.1'
s.requirements << 'none'
s.require_path = 'lib'
s.autorequire = 'plugin_name'
s.files = s.files + Dir.glob( "lib/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
s.description = 'Test act_as_random gem'
end
Rake::GemPackageTask.new(spec) do |pkg|
pkg.gem_spec = spec
pkg.need_zip = true
end
After specify the above specification in rake file
projpath> rake gem
Now it will create a gem file in a “pkg” directory.
You can install this gem using
gemfolderpath> gem install your_new_gem.gem
Post comment if you feel it is useful for you.
Deploy on Server
Move Few Steps to Deploy Ruby code on Server
The Followings steps help you the deploy your ruby code on server from your local environment. you need to have capistrano 2.2.0 and railsmachine 1.0.0 installed locally or upgraded version.
1) Need to install rails-machine.
c:\> gem install railsmachine
if you find any problem then directly download .gem file from http://rubyforge.org/frs/?group_id=1336&release_id=30415.
After downloading the gem file try this.
c:\> gem install railsmachine-1.0.1.gem
2)Install Capistrano with all dependancies
gem install -y capistrano
if you find any problem then download directly from http://www.capify.org/download.
3) Capify your application
c:> capify .
Now the deploy.rb file will created under
4) Configure Capistrano for Rails Machine
c:> railsmachine --apply-to . --name my_app --domain my_domain.com
5) Modify the deploy.rb as per your requirements(if any)
6) Deploy the Code
c:\
or,if you need to run migrations as well
c:\