/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. module Kernel
  7. if defined?(gem_original_require) then
  8. # Ruby ships with a custom_require, override its require
  9. remove_method :require
  10. else
  11. ##
  12. # The Kernel#require from before RubyGems was loaded.
  13. alias gem_original_require require
  14. private :gem_original_require
  15. end
  16. ##
  17. # When RubyGems is required, Kernel#require is replaced with our own which
  18. # is capable of loading gems on demand.
  19. #
  20. # When you call <tt>require 'x'</tt>, this is what happens:
  21. # * If the file can be loaded from the existing Ruby loadpath, it
  22. # is.
  23. # * Otherwise, installed gems are searched for a file that matches.
  24. # If it's found in gem 'y', that gem is activated (added to the
  25. # loadpath).
  26. #
  27. # The normal <tt>require</tt> functionality of returning false if
  28. # that file has already been loaded is preserved.
  29. def require path
  30. if Gem.unresolved_deps.empty? or Gem.loaded_path? path then
  31. gem_original_require path
  32. else
  33. spec = Gem.searcher.find_active path
  34. unless spec then
  35. found_specs = Gem.searcher.find_in_unresolved path
  36. unless found_specs.empty? then
  37. found_specs = [found_specs.last]
  38. else
  39. found_specs = Gem.searcher.find_in_unresolved_tree path
  40. end
  41. found_specs.each do |found_spec|
  42. Gem.activate_spec found_spec
  43. end
  44. end
  45. return gem_original_require path
  46. end
  47. rescue LoadError => load_error
  48. if load_error.message.end_with?(path) and Gem.try_activate(path) then
  49. return gem_original_require(path)
  50. end
  51. raise load_error
  52. end
  53. private :require
  54. end