📜 ⬆️ ⬇️

We write your first gem

I want to tell you my experience in writing gem. I had this idea a long time ago, but I still couldn’t get it. Everything rested on banal laziness and lack of sufficient motivation. However, a week ago I got a project related to the mailing list server.

Choosing a generator

At the first stage, it was necessary to determine how the library would be created: from scratch or with the help of some kind of generator. For the first time, in my opinion, writing from scratch will be quite difficult and long, so consider the well-known generators. A small search showed such gems: hoe, newgem, bundler. Personally, I liked two - newgem and bundler. The first is a complete set of templates covering a variety of cases. But I chose the bundler for its simplicity, the minimum set of generated files and the rake tasks set for creating a package and its further publication.

What's inside

So, to create a template, execute the following command in the terminal:
')
bundle gem foogem

This command will create a foogem directory containing the lib folder and the files gemfile, Rakefile and foogem.gemspec. Consider each separately. The gemfile file, as in any bundle compatible framework, contains all the necessary gems. Also there is a gemspec line, which loads the declared dependencies in the foofem.gemspec file. On many resources and in the file itself, it is advised to fill in a gemfile, and indicate the necessary gems in the .gemspec file. There, the developer can specify not only the dependencies necessary for the functioning of the code, but also what is needed during the development phase. To indicate global dependency, the add_dependency method is used, for the development mode, add_development_dependency is used. In addition, the file also stores the information about the developers, a brief description and version of the library. In Rakefile, we can register rake tasks that are necessary for normal functioning.

The lib folder is the main part of your component. It contains the file foogem.rb and a folder of the same name. It contains a file for specifying the version vesion.rb. In the foogem.rb file, the bundler kindly put the following code:

require 'foogem/version'
module Foogem
#code place here
end


Then you bring your ideas to life.

Verification written

Having finished developing a gem, it would be nice to write tests for verification. Personally, I prefer rspec over other test environments. To do this, add a dependency to foogem.spec

gem.add_development_dependency 'rspec'

Next, create a folder spec, which will store all our tests. Since all tests can include many dependencies, we will select them into a separate file spec_helper.rb and we will connect it. All tests put in subdirectories. For more automation, add the following rake task:

desc 'Spec all functionality of gem'
task :spec_all do
system("rspec spec/*/")
end


Now with the rake spec_all we run all the specs.

The final step will be the publication of our gem. First we need to create a couple of accounts (if they are missing) on ​​github and rubygem. After that create an empty repository on github. Add it to the remote in your local git repository. And execute the commands:

rake build # gem
rake release # github & rubygems


After that you can install it via commands.

gem install foogem #
rake install #


In the future, to continue development, it will only be necessary to change the gem version in lib / foogem / version.rb and repeat the publication step.

Source: https://habr.com/ru/post/128378/


All Articles