Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditionals in Gemfile

Our team uses different databases for each other, and we are using bundler so our Gemfile contains the repo creator's db connector(mysql)

I am using pg and due to a bit laziness and fear of breaking something, I don't want to use mysql, so I just add a gem "pg" in our Gemfile.

Of course, since we're using git, it will always show as a modified file, and we all use the Gemfile so we can't gitignore it or commit it with our changes.

Question is, how do we go about this? Is there a conditional in bundler or do I just have to declare that I'm using a certain gem someplace else?

like image 829
corroded Avatar asked Oct 14 '10 17:10

corroded


People also ask

Should I commit my Gemfile lock?

A: Yes, you should commit it. The presence of a `Gemfile. lock` in a gem's repository ensures that a fresh checkout of the repository uses the exact same set of dependencies every time.

What is the difference between Gemfile and Gemfile lock?

The Gemfile is where you specify which gems you want to use, and lets you specify which versions. The Gemfile. lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.

What can you do with Gemfile?

Your gemfile is a list of all gems that you want to include in the project. It is used with bundler (also a gem) to install, update, remove and otherwise manage your used gems. These gems belong to development environment and the test environment since they are for testing the application.

What is require in Gemfile?

Gemfiles. Gemfiles require at least one gem source, in the form of the URL for a RubyGems server. Generate a Gemfile with the default rubygems.org source by running bundle init . If you can, use https so your connection to the rubygems.org server will be verified with SSL.


2 Answers

Since Gemfile, like Rakefile, is just a chunk of Ruby, you can throw in conditionals if you think it will simplify your life. For instance:

if (Gem.available?('pg'))
  gem 'pg'
else
  gem 'mysql2'
end

Sometimes you have to do this for different Ruby versions as 1.8 and 1.9 sometimes need different gems.

like image 167
tadman Avatar answered Oct 21 '22 07:10

tadman


You can use a group. Yehuda Katz explain it how here (taking in example the pg gem) http://yehudakatz.com/2010/05/09/the-how-and-why-of-bundler-groups/

like image 45
hellvinz Avatar answered Oct 21 '22 07:10

hellvinz