How to create your own gem in Ruby

How to create your own gem in Ruby

This article provides a step-by-step guide on how to create your own gem in Ruby. It covers the necessary tools and techniques required to package and distribute your Ruby code as a gem. Whether you are a beginner or an experienced Ruby developer, this article will help you understand the process of gem creation and empower you to share your code with the Ruby community.
How to create your own gem in Ruby

WHAT IS RUBY GEM ?

Gems are open-source Ruby libraries or applications that lend extra utility to other Ruby programs. Each gem is composed of the following:

  • The source code it was built with (including any tests).

  • Any documentation with metadata such as the name, version, and platform.

  • A .gemspec file holding all gem-related metadata.

Gems can be referenced, installed, and published from an online registry called RubyGems.

HOW TO BUILD YOUR OWN GEM ?

Generate Gem Structure       

$ gem install bundler
$ bundle gem tony_demo_gem

   

 Change content of file *.gemspec

After you have created a gemspec, you can build a gem from it. Then you can install the generated gem locally to test it out.

$ gem build tony_demo_gem.gemspec
$ gem install ./tony_demo_gem-0.1.0.gem

USING AND DEVELOPMENT

Ruby gems are packages of Ruby code that allow you to extend or modify functionality in Ruby applications. These gems are typically installed from a hosted gem repository like RubyGems.org. However, there might be situations where you want to install a gem directly from a GitHub repository. This could be the case if the gem is not yet published on RubyGems.org or if you want to use the latest version of the gem that might not yet be published.

To install a gem directly from a GitHub repository, you would typically use the `gem` command with the `:git` option in your Gemfile, specifying the URL of the gem's GitHub repository. For example:
$ gem "tony_demo_gem", git: "https://github.com/tonynguyen-mfv/tony_demo_gem.git"

To install a gem from a local file path, you would use the gem install command followed by the path to the .gem file. For example:

$ gem "tony_demo_gem", path: "/Users/mfv-computer-0195/Projects/tony_demo_gem"

To install a gem published on RubyGems.org, you would use the gem install command followed by the path to the .gem file. For example:

$ gem "tony_demo_gem"
After adding this line to your Gemfile, you would run `bundle install` to install the gem.

Remember, when installing gems directly from GitHub, you're bypassing the RubyGems.org infrastructure, which could have implications for the stability and security of your application. Always ensure you trust the source of the gems you're installing.

PUBLISH YOUR GEM

After building your gem, you can use the gem push command to publish your gem to RubyGems.org. You'll need to have an account on RubyGems.org and be signed in to push your gem. The command would look something like this:

$ gem signin

$ gem push tony_demo_gem-0.1.0.gem

GEM ENGINE CREATION IN RAILS

A Rails engine is a gem that behaves like a miniature Rails application. It allows you to package reusable components, including models, controllers, and views. To generate a new engine, run the following command:

$ rails plugin new tony_demo_plugin --mountable --full

To generate models, controllers, and views run the following command:

$ bin/rails generate scaffold User name:string email:string 

Import your gem into rails app:

gem “tony_demo_plugin”, path: “path/to/tony_demo_plugin”

Add the below line to mount your gems route in your rails app.

mount TonyDemoPlugin::Engine => "/tony_demo_plugin"

Add gems migration into your rails app:

$ rails tony_demo_plugin:install:migrations
$ rails db:migrate

Visit http://localhost:3000/tony_demo_plugin/users/

HOW TO OVERRIDE GEM ENGINE

Overriding Models and Controllers

Engine models and controllers can be reopened by the parent application to extend or decorate them.

Overrides may be organized in a dedicated directory app/overrides, ignored by the autoloader, and preloaded in a to_prepare callback:

For example, in order to override the engine model

You just create a file that reopens that class:

For example, in order to override the engine controller:

Overriding Views

You can override this view in the application by simply creating a new file at app/views/*

Other way to Overriding

https://edgeguides.rubyonrails.org/engines.html#reopening-existing-classes-using-activesupport-concern

DEMO

https://github.com/tonynguyen-mfv/tony_demo_gem
https://github.com/tonynguyen-mfv/tony_demo_plugin
https://github.com/tonynguyen-mfv/tony_demo_blog

More like this

AWS Technical Essential Training
Apr 25, 2023

AWS Technical Essential Training