Subscribe

Recent Articles

Bootstrapping a Minimal RubyGem

These are the steps I currently follow when starting a new project to be distributed as a RubyGem. Let’s assume the new RubyGem will be named “wozziegoggle”.

  1. Bootstrap the project using Bundler:
    $ bundle gem wozziegoggle
    
  2. I’m using RSpec and Mocha on new projects, so I add development gem dependencies for each.
    # wozziegoggle.gemspec
    Gem::Specification.new do |s|
      ...
      s.add_development_dependency 'rspec', '~>2.6.0'
      s.add_development_dependency 'mocha', '~>0.9.12'
    end
    
  3. I also create a basic spec_helper.rb file that configures RSpec and requires needed libraries.
    # spec/spec_helper.rb
    spec_dir = File.dirname(__FILE__)
    lib_dir  = File.expand_path(File.join(spec_dir, '..', 'lib'))
    $:.unshift(lib_dir)
    $:.uniq!
    RSpec.configure do |config|
      config.mock_with :mocha
    end
    require 'mocha'
    require 'wozziegoggle'
    
  4. The RSpec rake taks will also be needed, so I make changes to the Rakefile. I’ll also add a task which spawns IRB with the gem libraries preloaded.
    # Rakefile
    require 'rspec/core/rake_task'
    RSpec::Core::RakeTask.new
    task :default => :spec
    desc 'Start IRB with preloaded environment'
    task :console do
      exec 'irb', "-I#{File.join(File.dirname(__FILE__), 'lib')}", '-rwozziegoggle'
    end
    
  5. I usually use ZenTest, specifically autotest so that the specs can by automatically re-run as I’m developing. A discover file is needed to make sure there are sane default mappings.
    # autotest/discover.rb
    Autotest.add_discover {'rspec2'}
    
  6. Finally, I’ll create a .rspec (née spec/spec.opts) file with any options I want rspec to be run with.
    # .rspec
    --colour
    
blog comments powered by Disqus