Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does bundler work?

This question is about bundler,rubygems & how does it figure out which method I am calling. Its quite long, so please bear with me.

As per my understanding, Bundler is a dependency management tool for managing gems for ruby. It install all the gems & their dependencies listed in the Gemfile.

The question I want to ask can be best illustrated by an example. so here--> In my rails app I am doing this in my controller:-

module SurveyorControllerCustomMethods
  def create
    super
  end
end
class SurveyorController < ApplicationController
   include Surveyor::SurveyorControllerMethods
end

Here, I am doing two things:

  1. Include SurveyorControllerMethods from Surveyor gem.
  2. As I have used super here, it would call create method from SurveyorControllerMethods which works just fine but I do not understand it.

and the gem is installed at

$ bundle show surveyor
/home/gaurish/.rvm/gems/ruby-1.9.3-p194/gems/surveyor-0.22.0

Which surprisingly is NOT present in ruby's $LOAD_PATH. so question is:

  1. how does it even work?
  2. Install a gem using bundler, what happens behind the scenes during install?
  3. there are some gems ex jquery-rails for which we even don't have to include/call them in our code & yet, jQuery JavaScript file is automatically included. I know its not magic, so how does this work?
like image 361
CuriousMind Avatar asked May 03 '12 16:05

CuriousMind


1 Answers

They key is actually in how Rails is set up out of the box. Which is also one of the reasons Rails is so slow to start on larger projects.[1]

In boot.rb you'll find:

require 'bunder/setup'

and at the top of application.rb you'll find:

Bundler.require(:default, :development)

This last method call looks back to your Gemfile, then iterates all of the gems that are in the given groups (:default and :development in the above). It then takes a stab at determining what the default file is that should be loaded, which is typically the gem name, but can also be specified by a :require option. Bundler has some built-in naming convention exceptions too, such as the handling of "-" instead of "_".

Bundler then just loads the primary file for the gem, which in turn (typically) loads the files inside the gem.

The 'bundler/setup' file performs Bundler.setup, which takes care of the load path. It prepends (unshifts) to the $LOAD_PATH for each gem. The actual information about what those loads paths should be comes from rubygems.

Take a look at the source in:

https://github.com/carlhuda/bundler/blob/master/lib/bundler/setup.rb https://github.com/carlhuda/bundler/blob/master/lib/bundler/runtime.rb

[1]: When you start a new Rails project, you'll probably find it doesn't get so slow over time if you delete the Bundler.require line and just require gems as they are needed.

like image 67
d11wtq Avatar answered Sep 30 '22 13:09

d11wtq