2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
10 alias gem_original_require require # :nodoc:
13 # We replace Ruby's require with our own, which is capable of
14 # loading gems on demand.
16 # When you call <tt>require 'x'</tt>, this is what happens:
17 # * If the file can be loaded from the existing Ruby loadpath, it
19 # * Otherwise, installed gems are searched for a file that matches.
20 # If it's found in gem 'y', that gem is activated (added to the
23 # The normal <tt>require</tt> functionality of returning false if
24 # that file has already been loaded is preserved.
26 def require(path) # :nodoc:
27 gem_original_require path
28 rescue LoadError => load_error
29 if load_error.message =~ /#{Regexp.escape path}\z/ and
30 spec = Gem.searcher.find(path) then
31 Gem.activate(spec.name, "= #{spec.version}")
32 gem_original_require path