extracting ruby gem
DESCRIPTION
Create ruby gem step by step with bundlerTRANSCRIPT
Yura TolstikRuby/Rails developer at Altoros Development
twitter: @yltsrcemail: [email protected]
Extracting ruby gem
Why?
Reusable codeEasy to installEasy to share
Lets start
Find code duplicationsWrite tests if not exists
Ready?
How to create
creating a gem specification file (mygem.gemspec)and do all things
by hand...
bundle gem mygem
By hand?
mygem.gemspecGem::Specification.new do |s| s.name = "mygem" s.version = Mygem::VERSION s.authors = ["Yura Tolstik"] s.email = ["[email protected]"] s.homepage = "http://github.com/yltsrc/mygem" s.summary = %q{My first gem} s.description = %q{Create ruby gem step by step with bundler} s.license = "MIT"
s.files = `git ls-files`.split("\n") s.test_files = `git ls-files -- spec/*`.split("\n") s.require_paths = ["lib"] s.add_development_dependency "rake" s.add_development_dependency "rspec"end
spec_helper.rb
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))$LOAD_PATH.unshift(File.dirname(__FILE__))require 'rspec'require 'newgem'
RSpec.configure do |config| # if neededend
Extract tests
describe "Mygem" do describe "#hello" do it "should return 'Hello world!'" do @base = "" @base.extend(Mygem::Base) @base.hello.should eql("Hello world!") end endend
Extract methods
module Mygem module Base def hello "Hello #{self.empty? ? 'world' : self}!" end endend
Rakefile
require "bundler/gem_tasks"
require 'rspec/core'require 'rspec/core/rake_task'RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = FileList['spec/**/*_spec.rb']endtask :default => :spec
Test
rake spec
Build
rake build
or
gem build mygem.gemspec
Publish
rake release
or
gem push mygem-0.0.1.gem
My own gem server
gem help generate_index
gem help server
Questions
http://gembundler.com/rubygems.htmlhttp://docs.rubygems.org/read/chapter/20