Last Updated: February 25, 2016
·
1.155K
· swapnilabnave

Journey of a `ruby library` to `rubygems.org`

Scaffold

To begin to create a gem using Bundler, use the bundle gem command like this:
bundle gem dj_unique

This command creates a scaffold directory for our new gem and, if we have Git installed, initializes a Git repository in this directory so we can start committing right away. The files generated are:

Gemfile:
Rakefile
.gitignore:
dj_unique.gemspec:
lib/dj_unique.rb: 
lib/dj_unique: 
lib/dj_unique/version.rb:

There's our base and our layout, now get developing!

RVMize your repo,

I do so, it keeps my gemsets clean.

rvm use 2.0.0-p247@dj_unique --create

rvm --create --ruby-version use 2.0.0-p247@dj_unique

Add and install Dependencies

# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'dj_unique/version'

Gem::Specification.new do |spec|
  spec.name          = "dj_unique"
  spec.version       = DjUnique::VERSION
  spec.authors       = ["Swapnil Abnave"]
  spec.email         = ["swapnilabnave42@gmail.com"]
  spec.description   = %q{An armour for delayed_job_active_record to ensure no duplicate jobs are enqueued}
  spec.summary       = %q{dj stands for delayed_job}
  spec.homepage      = "https://github.com/swapnilabnave/dj_unique"
  spec.license       = "MIT"

  spec.add_development_dependency "bundler", "~> 1.3"
  spec.add_development_dependency "rake"
  spec.add_development_dependency "rspec", "~> 2.6"
  spec.add_dependency             "delayed_job_active_record", "~> 4.0.0"

  spec.files         = `git ls-files`.split($/)
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]
end

Bundle it $ bundle

Testing

Create :
spec/
And
spec/spec_helper.rb

require 'rubygems'
require 'rspec'
require 'dj_unique'

I18n.config.enforce_available_locales = true

RSpec.configure do |config|
  config.order = :rand
  config.color_enabled = true
end

Run the test

require "rspec/core/rake_task"

RSpec::Core::RakeTask.new

task :default => :spec
task :test => :spec

Run your specs

$ rake

Results:

Delayed::UniqueJob
  get active_jobs for the performable object
  reject active jobs with not matching handler

Finished in 0.03308 seconds
2 examples, 0 failures

Randomized with seed 39927

Go Go Go

Version Control it(GIT)

If we haven't already, we should commit all the files for our repository:

create github repo

hub create dj_unique (To install hub)

add -> commit -> push

git add .
git commit -m "Say Hello to dj_unique"
git push origin master

Build and Release

rake build

will build a local copy of our gem and then gem install pkg/dj_unique-0.0.1.gem to install it

To release the first version of our gem we can use following command

rake release