Permalink
Fetching contributors…
Cannot retrieve contributors at this time
138 lines (115 sloc) 4.58 KB
%h2 Using Groups
.contents
.bullet
.description
Grouping your dependencies allows you
to perform operations on the entire
group.
:code
# lang: ruby
# These gems are in the :default group
gem 'nokogiri'
gem 'sinatra'
gem 'wirble', :group => :development
group :test do
gem 'faker'
gem 'rspec'
end
group :test, :development do
gem 'capybara'
gem 'rspec-rails'
end
gem 'cucumber', :group => [:cucumber, :test]
.bullet
.description
Install all gems, except those in the
listed groups. Gems in at least one
non-excluded group will still be installed.
:code
$ bundle install --without test development
.bullet
.description
Require the gems in particular groups,
noting that gems outside of a named group
are in the :default group
:code
# lang: ruby
Bundler.require(:default, :development)
.bullet
.description
Require the default gems, plus the gems
in a group named the same as the current
Rails environment
:code
# lang: ruby
Bundler.require(:default, Rails.env)
.bullet
.description
Restrict the groups of gems that you
want to add to the load path. Only gems
in these groups will be require'able
:code
# lang: ruby
require 'rubygems'
require 'bundler'
Bundler.setup(:default, :ci)
require 'nokogiri'
= link_to 'Learn More: Bundler.setup', './bundler_setup.html'
%h2#grouping-your-dependencies
Grouping Your Dependencies
.bullet
.description
You'll sometimes have groups of gems that only make sense in particular environments.
For instance, you might develop your app (at an early stage) using SQLite, but deploy it
using <code>mysql2</code> or <code>pg</code>. In this example, you might not have MySQL
or Postgres installed on your development machine, and want bundler to skip it.
To do this, you can group your dependencies:
:code
# lang: ruby
source 'https://rubygems.org'
gem 'rails', '3.2.2'
gem 'rack-cache', :require => 'rack/cache'
gem 'nokogiri', '~> 1.4.2'
group :development do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
.bullet
.description
Now, in development, you can instruct bundler to skip the <code>production</code> group:
:code
$ bundle install --without production
.bullet
.description
Bundler will remember that you installed the gems using <code>--without
production</code>. For curious readers, bundler stores the flag in
<code>APP_ROOT/.bundle/config</code>. You can see all of the settings that bundler saved
there by running <code>bundle config</code>, which will also print out global settings
(stored in <code>~/.bundle/config</code>), and settings set via environment variables.
For more information on configuring bundler, please see:
#{link_to 'bundle config', './bundle_config.html'}
.bullet
%p.description
If you run <code>bundle install</code> later, without any flags, bundler will remember
that you last called <code>bundle install --without production</code>, and use that flag
again. When you <code>require 'bundler/setup'</code>, bundler will ignore gems in these
groups.
%p.description
You can also specify which groups to automatically require through the parameters to
<code>Bundler.require</code>. The <code>:default</code> group includes all gems not
listed under any group. If you call <code>Bundler.require(:default, :development)</code>,
bundler will <code>require</code> all the gems in the <code>:default</code> group, as
well as the gems in the <code>:development</code> group.
%p.description
By default, a Rails generated app calls <code>Bundler.require(:default,
Rails.env)</code> in your <code>application.rb</code>, which links the groups in your
<code>Gemfile</code> to the Rails environment. If you use other groups (not linked to a
Rails environment), you can add them to the call to <code>Bundler.require</code>, if you
want them to be automatically required.
%p.description
Remember that you can always leave groups of gems out of <code>Bundler.require</code>,
and then require them manually using Ruby's <code>require</code> at the appropriate
place in your app. You might do this because requiring a certain gem takes some time,
and you don't need it every time you boot your application.