In this post, we will create, test, build and publish the gem to RubyGems! (without breaking a sweat).
During this post I will use reference to sequenceable gem that I created. The source code for that can be found here.
TL;DR
1. Create
Creating a gem structure is as easy as running
$ bundle gem sequenceable
Use underscore case for the name. Dashes will create a nested folder structure. Read name your gem guide for more details.
The above command creates all the files needed for a gem. Here’s the output:
Creating gem 'sequenceable'...
MIT License enabled in config
Code of conduct enabled in config
create sequenceable/Gemfile
create sequenceable/lib/sequenceable.rb
create sequenceable/lib/sequenceable/version.rb
create sequenceable/sequenceable.gemspec
create sequenceable/Rakefile
create sequenceable/README.md
create sequenceable/bin/console
create sequenceable/bin/setup
create sequenceable/.gitignore
create sequenceable/.travis.yml
create sequenceable/test/test_helper.rb
create sequenceable/test/sequenceable_test.rb
create sequenceable/LICENSE.txt
create sequenceable/CODE_OF_CONDUCT.md
Initializing git repo in /Users/sandipmane/Work/sequenceable
Gem 'sequenceable' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html
1.1 Modify sequenceable.gemspec
This is the gem specifications file, it defines the gem. RubyGems will use information from this file to show on the gem page. We will not be able to build the gem until all the required information is given in this file.
This is also where we define the dependencies that we have.
1.2 Check sequenceable/version.rb
We will set the current version for our gem in this file. We need to update the version while releasing a new version everytime. Check Semantic versioning docs for more information on versioning.
1.3 THE CODE
We can add our code to lib/sequenceable.rb
if that doesn’t make much clutter.
To make it modular,
we can add modules in
lib/sequenceable
directory
and import those in lib/sequenceable.rb
.
2. Test
To run tests,
we need to include following
as dev dependencies
in sequenceable.gemspec
.
- active_record
- sqlite3
Since our gem deals with ActiveRecord, we need to include that and a database in our dev dependencies. These may not be required for other gems.
2.1 Create test models
Create test models and database schema
in test/support/
directory
then import those in test/test_helper.rb
.
require 'support/schema'
require 'support/models'
2.2 Add tests
Add unit tests in test/sequenceable_test.rb
.
We will have two tests
created by default with our gem.
Run the tests with
$ bundle exec rake test
2.3 Test with Rails
Personally, I load the local gem to manually test it. It gives us the instant results and happiness😎!
To do this, mention it in the Gemfile
gem 'sequenceable', path: '../sequenceable'
Then
$ bundle install
3. Build
Now that we’ve created our gem, we are ready to share it. Lets first build it.
$ gem build sequenceable.gemspec
#=> Successfully built RubyGem
#=> Name: sequenceable
#=> Version: 0.1.0
#=> File: sequenceable-0.1.0.gem
We could also use rake build
for this,
that will create sequenceable-0.1.0.gem
in pkg
directory.
Much Better!
4. Publish
To publish our gem to RubyGems, we first need a RubyGems account. Create an account if you dont have it already!
We only need to login once,
RubyGems stores our credentials in
~/.gem/credentials
to make the gem pushing experience seamless.
$ gem push pkg/sequenceable-0.1.0.gem
#=> Pushing gem to RubyGems.org...
#=> Successfully registered gem: sequenceable (0.1.0)