Jan
Jan

Reputation: 6828

gem with multiple requires in gemfile

So I have this gem and it depends on lots of other gems. While in the gemspec it says

s.add_dependency "haml" ...

bundler does not seem to care, so I have to repeat these dependency in the Gemfile. Is there a syntax to require multiple gems? Something like that (does not work):

gem "so-and-so",
   :git => "some-repo",
   :require => ["this-gem", "that-gem", "and-what-not"]

require seems to only allow a single object

Upvotes: 9

Views: 3476

Answers (3)

Dave Sag
Dave Sag

Reputation: 13486

According to the Gemfile documentation you can simply pass an array of requires. I hit this question while researching RSpec like syntactical sugar for Minitest and noting that I'd need to:

require 'minitest/spec'
require 'minitest/autorun'

To get this to work. I'd never tried multiple requires in a Gemfile before, and googling led me here to this question, and more Googling led me to the Gemfile docs which state:

REQUIRE AS (:require)
Each gem MAY specify files that should be used when autorequiring via
Bundler.require. You may pass an array with multiple files, or false
to prevent any file from being autorequired.

gem "sqlite3-ruby", :require => "sqlite3"
gem "redis", :require => ["redis/connection/hiredis", "redis"]
gem "webmock", :require => false

So in my own Gemfile I have included

group :test do
  gem 'minitest', require: ['minitest/autorun', 'minitest/spec']
  gem 'rack-test', require: 'rack/test'
  gem 'simplecov', require: false
end

Which works perfectly and allows me to write a test like

describe 'basic crud' do
  it 'must create a user with valid details' do
    User.transaction do
      user = User.create!(username: 'test', password: 'pass')
      user.username.must_equal 'test'
      user.destroy
    end
  end
end

Which I find reads nicer than assert_equals user.username, 'test' and gives me access to my familiar before :each do… and after :all do… prep and cleanup methods.

Upvotes: 8

Holger Just
Holger Just

Reputation: 55748

You have to tell bundler to use your gemspec as a source for dependencies. To do that, just insert this into your Gemfile

source "http://rubygems.org"

# require this gem's dependencies
gemspec

Typically for a gem, this is the only contents of the Gemfile.

Upvotes: 0

Batkins
Batkins

Reputation: 5706

I'm pretty sure that Bundler looks to the gems themselves to manage their own dependencies. The idea being that you shouldn't need syntax like this for specifying dependencies because the gem does it for you in it's gemspec file. As stated on the bundler docs, the require keyword is used for: 'If a gem's main file is different than the gem name, specify how to require it.'

If a gem has s.add_dependency 'haml' ... and it is not working then either the gem's gempsec file has an error, or the version of bundler you have has an error (or is too old to know about dependencies or something). I would report it to the authors of the gem.

One workaround you could use till you figure out what is wrong with the gem or it's dependencies would be to put the gem and the other required gems it uses in a group, and add a comment to the Gemfile mentioning why you're doing it.

Which gem are you having issues with? If you specify a name others may be able to verify what you're experiencing or give you better help. Also what happens when you try gem dependency gem_name in a shell?

Upvotes: 0

Related Questions