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\\rake).

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.

No comments:

Post a Comment