Thursday, March 28, 2013

Write Rails3 gem

= Introduction
Creating Ruby gems has always been a bit of a pain. When you get right down to it, it's not a hard thing to do, but it's easy to forget how if you don't do it often enough. There are quite a few gems to help you write other gems, but if you're using Rails 3, there's already a gem on your system that does this: Bundler.

It will also generate a stub gem project for you, complete with Rakefile and directory structure.
The Rakefile is fully featured, and will create gem packages and publish them on

= Start
have account on github and

= Install bundler to manage building the gem
gem install bundler

= Create the initial gem layout
bundle gem iprocessor

~/workspace$ bundle gem ilinks
      create  iprocessor/Gemfile
      create  iprocessor/Rakefile
      create  iprocessor/LICENSE.txt
      create  iprocessor/
      create  iprocessor/.gitignore
      create  iprocessor/iprocessor.gemspec
      create  iprocessor/lib/iprocessor.rb
      create  iprocessor/lib/iprocessor/version.rb
bundle gem not only generate stub but also initialized Github Repo if you have Git configured already.

The bundle command also generated a Rakefile. This Rakefile is where you'll package and publish your gem. If you look in this file, you'll see that the actual tasks are buried in Bundler's code, but you can run a quick rake -T to get a list of tasks.

~/workspace/iprocessor$ rake -T
rake build    # Build ilinks-0.0.1.gem into the pkg directory
rake install  # Build and install iprocessor-0.0.1.gem into system gems
rake release  # Create tag v0.0.1 and build and push iprocessor-0.0.1.gem to Rubygems

rake -T is self explanatory.

= Ddit ilinks.gemspec

# -*- encoding: utf-8 -*-

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'iprocessor/version'
require 'iprocessor/conf' do |gem|          = "iprocessor"
  gem.version       = Iprocessor::VERSION
  gem.authors       = ["Moin Haidar"]         = [""]
  gem.description   = %q{ Ruby Wrapper for Card Reader API }
  gem.summary       = %q{ Ruby Wrapper for Card Reader API }
  gem.homepage      = ""

  # ensures that any files tracked in the git repo will be included.
  gem.files         = `git ls-files`.split($/)
  gem.executables   = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
  #Files that are used for testing the gem. Supports TestUnit, MiniTest, RSpec, and Cucumber
  gem.test_files    = gem.files.grep(%r{^(test|spec|features)/})
  #Directories within the gem that need to be loaded in order to load the gem.
  gem.require_paths = ["lib"]
  #Specify any dependencies here
  gem.add_dependency 'httparty'

= Commit to the git repo
git add .
git push origin master

= Setup an .rvmrc for the directory
vim .rvmrc
rvm 1.9.3@iprocessor --create

= Install the gem
- To install in current gemset
rake install

- To install elsewhere
cd path_of_rails_app
rake build
gem install ../gemname/pkg/gemname

Note: Modification does not reflect when you build and install unless you stage it.

= rake release
Make sure to push the changes before release and change the version on further release to avoid error "This tag has already been committed to the repo."

~/workspace/iprocessor$ rake release
iprocessor 0.0.2 built to pkg/iprocessor-0.0.2.gem
Tagged v0.0.2
Pushed git commits and tags
Pushed iprocessor 0.0.2 to

No comments:

Post a Comment