What I learned by writing gems.

Clément Morisset
4 min readJan 23, 2020

I have been publishing 3 gems for 2 years, of course I didn’t change the game with them. No much stars, no much users..and not really sure to save time/life of many developers.

But embrace gems development and more widely the open-source is oddly satisfying, each gem taught me something. Below my thoughts that I wanted to share with you.

1/ Conchiterz

My first gem, I was looking for a small project to publish. Conchiterz is a simply gem to feminize french word/sentence. As many gems are dedicated to english speaker I was guessing that there was a need.

How to monkey patch String class method

I wanted to use my method like a traditional String method like length or gsub. So I should monkey patch to get the result below.

<%= 'Inscrit'.conchiterz(@pratiquant.female?) %>

In my main file I have added:

module Conchiterz  module StringMethods    
def conchiterz(switch, escape = [])
Conchiterz.translate(self, switch, escape)
end
end
....

def monkey_patch(str)
str.send(:include, Conchiterz::StringMethods)
end
end
if defined?(Rails)
Conchiterz.monkey_patch(String)
end

Feel free to check out my repo

2/ See Double

It’s a small tool CLI, for Rspec Lover, to monitoring your features specs and check your duplication. SeeDouble go through all your specs and count the number of occurrence of your «expect» and «result».

Create DummyApp engine

Sometimes you are building gem that will interact with rails application. In order to test your gem you should had to create a dummy app to simulate a real rails app.

In my case I had to add dummy app to have a spec folder to count the number of occurrence of «expect» and «result».

Command to add engine:

rails plugin new rails_app — skip-test-unit — dummy-path=spec/dummy

These command will generate an engine that looks like a traditional rails app.

So I have added fake spec in my dummy app spec:

see_double/rails_app/dummy/spec/admin_spec.rb

And in my code I have added a statement to find the right spec path

def renvoie_le_bon_chemin
Dir.exist?(“./rails_app/dummy”) ? “./rails_app/dummy/spec/**/*.rb” : “./spec/features/**/*.rb”
end

It turns out that I am not really proud of it but I didn’t try to go in depth so far. Anyway you get the idea.

Add an executable

What I expected with this gem is to let the user to run the command from the terminal:

see_double expect

To achieve it I had to add two things. First, I created in my bin folder a bash file with a command I want to run.

The first line allow you to write ruby in bash file.

#!/usr/bin/env ruby -wU

Then I add in my see_double.gemspecthe executable:

s.executables << “see_double”

That’s it! Feel free to take a look to my repository.

3/ Like Im Five

The elevator pitch:

Like Im Five generate a file with factories you need to test a specific object. It use you DB development to retrieve the object you want and all associations you will have need to setup the test.

The main issue encountered has been to connect gem to user DB development. Indeed the gem need to make multiple requests to find associated object and collect attributes.

Connect gem to user database

class GeneralConfiguration
def self.initialize_configuration
config_db = File.read(‘./config/database.yml’)
db_config = YAML.load(ERB.new(config_db).result)
ActiveRecord::Base.establish_connection(db_config[‘development’])
end
end

The first two lines in the method collect informations from the database.yml of the user. We will use them in the last line to establish a connection with the ActiveRecord database. More informations here.

Once we are connected to user DB we can start to make request.

Make requests

ActiveRecord::Base.connection.execute(“SELECT * FROM #{table} WHERE id=’#{value}’”).to_a

As it was too complicated and overkill to use traditional ActiveRecord query I decided to use raw SQL query by using the connection adaptater. As mentioned in documentation: «Executes the SQL statement in the context of this connection and returns the raw result from the connection adapter.»

This request render a hash object with the raw attributes of the targeted object.

Feel free to check out my repo to get more informations.

Conclusion

Participate to open source community is a wonderful experience and a huge source of learning. Building gem is really satisfying and push you to experiment stuff you don’t need to use daily. Everyday you have to use gem in your projects, coding one allow you to have a better understanding of what you use and how you can debug one or even monkey_patching one to get the result you expect. So if you never still experimented this, I encourage you to embrace gem development. Take a small issue you can encounter in your job and start to code a gem to fix it. Learning by doing.

--

--