In this post, we will create, test, build and publish the gem to RubyGems! (without breaking a sweat).
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
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.
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
if that doesn’t make much clutter.
To make it modular,
we can add modules in
and import those in
To run tests,
we need to include following
as dev dependencies
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
then import those in
require 'support/schema' require 'support/models'
2.2 Add tests
Add unit tests in
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
gem 'sequenceable', path: '../sequenceable'
$ bundle install
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
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
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)