PageRenderTime 68ms CodeModel.GetById 53ms app.highlight 7ms RepoModel.GetById 7ms app.codeStats 0ms

/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb

http://github.com/agross/netopenspace
Ruby | 66 lines | 33 code | 12 blank | 21 comment | 3 complexity | 2c29de8fea2d2dbbcfd85af10a25a148 MD5 | raw file
 1#--
 2# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
 3# All rights reserved.
 4# See LICENSE.txt for permissions.
 5#++
 6
 7module Kernel
 8
 9  if defined?(gem_original_require) then
10    # Ruby ships with a custom_require, override its require
11    remove_method :require
12  else
13    ##
14    # The Kernel#require from before RubyGems was loaded.
15
16    alias gem_original_require require
17    private :gem_original_require
18  end
19
20  ##
21  # When RubyGems is required, Kernel#require is replaced with our own which
22  # is capable of loading gems on demand.
23  #
24  # When you call <tt>require 'x'</tt>, this is what happens:
25  # * If the file can be loaded from the existing Ruby loadpath, it
26  #   is.
27  # * Otherwise, installed gems are searched for a file that matches.
28  #   If it's found in gem 'y', that gem is activated (added to the
29  #   loadpath).
30  #
31  # The normal <tt>require</tt> functionality of returning false if
32  # that file has already been loaded is preserved.
33
34  def require path
35    if Gem.unresolved_deps.empty? or Gem.loaded_path? path then
36      gem_original_require path
37    else
38      spec = Gem.searcher.find_active path
39
40      unless spec then
41        found_specs = Gem.searcher.find_in_unresolved path
42        unless found_specs.empty? then
43          found_specs = [found_specs.last]
44        else
45          found_specs = Gem.searcher.find_in_unresolved_tree path
46        end
47
48        found_specs.each do |found_spec|
49          Gem.activate_spec found_spec
50        end
51      end
52
53      return gem_original_require path
54    end
55  rescue LoadError => load_error
56    if load_error.message.end_with?(path) and Gem.try_activate(path) then
57      return gem_original_require(path)
58    end
59
60    raise load_error
61  end
62
63  private :require
64
65end
66