diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/kernel.rb | 22 |
2 files changed, 24 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cf8b935be1..630c8bdae9 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Moved require_library_or_gem into Kernel. #1992 [Michael Schuerig <michael@schuerig.de>] + * Add :rfc822 as an option for Time#to_s (to get rfc822-formatted times) * Chain the const_missing hook to any previously existing hook so rails can play nicely with rake diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index 54979c1a1a..168c9cddd3 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -28,4 +28,26 @@ module Kernel ensure $VERBOSE = old_verbose end + + # Method that requires a library, ensuring that rubygems is loaded + def require_library_or_gem(library_name) + begin + require library_name + rescue LoadError => cannot_require + # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. + begin + require 'rubygems' + rescue LoadError => rubygems_not_installed + raise cannot_require + end + # 2. Rubygems is installed and loaded. Try to load the library again + begin + require library_name + rescue LoadError => gem_not_installed + raise cannot_require + end + end + end + + end |