Last Updated: February 25, 2016
·
6.863K
· netoneko

Gemfile and gemspec

The problem with Gemfile and gemspec arises when you need to extract something raw as a gem (for example, stand alone Rails app one day becomes Rails mountable engine) you have to change your usual dependency management workflow. Old good bundle install is not enough anymore.

Let's say, you have a project with Gemfile full of gems. What you gonna do if you need to reference this project in another project?

First of all, create project.gemspec file. Look at the Bundler docs.

bundle gem my_gem

You have to reference your gemspec inside the Gemfile like this:

source 'http://rubygems.org'

gemspec

Every gem you had in the Gemfile as a dependency should now reside inside the gemspec file like this:

Gem::Specification.new do |s|
  ...
  s.add_dependency "how-cute-and-adorable", "= 0.3.2"
  ...
end

Check that bundle install command install all required gems before moving to the next step.

Every gem has to autoload his own dependencies by hand. Let's say, in your lib directory you have project.rb:

require 'project/stuff'
require 'project/more_stuff'

module Project

end

In the project.rb you have to add every single gem you have in dependencies. Be especially carefull if gem's name does not match to what you actually need to require:

require 'how-cute-and-adorable'
require 'cute'
require 'cute/and-adorable'

require 'project/stuff'
require 'project/more_stuff'

module Project

end