Using ActiveSupport::Dependencies outside a Rails project

I wanted to have the ability to auto-load dependencies based on the symbol name the way Rails does in my own non-Rails project, as it would save me writing a lot of require statements.

This can be achieved using ActiveSupport.

I added a file called boot.rb which is included in the other files of the project.

First of all, the file should require the active_support gem. I use Bundler, so in my case I specified a dependency on activesupport in the Gemfile and then loaded the Bundler environment in boot.rb:

require "#{File.dirname(__FILE__)}/vendor/bundler_gems/environment"
Bundler.require_env  

After that, I added the following lines:

include ActiveSupport
Dependencies.load_paths << File.join(File.dirname(__FILE__), 'config')
Dependencies.load_paths << File.join(File.dirname(__FILE__), 'lib')

This code means that unknown symbols will be resolved by searching the files inside the config and lib folders. Also, to force the inclusion of a particular file in case the load can't be triggered by an unknown symbol name, you can do:

Dependencies.depend_on("activerecord_extensions") 

After setting up boot.rb, you just need to require it in the files that do actual real work to get automatic dependency loading:

require File.expand_path("#{File.dirname(__FILE__)}/boot")

Works using: activesupport 2.3.5