diff options
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/core_ext/kernel.rb | 41 | ||||
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 8 |
2 files changed, 27 insertions, 22 deletions
diff --git a/activesupport/lib/active_support/core_ext/kernel.rb b/activesupport/lib/active_support/core_ext/kernel.rb index b687af03f6..d722c306cd 100644 --- a/activesupport/lib/active_support/core_ext/kernel.rb +++ b/activesupport/lib/active_support/core_ext/kernel.rb @@ -29,6 +29,14 @@ class Object $VERBOSE = old_verbose end + # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards. + def enable_warnings + old_verbose, $VERBOSE = $VERBOSE, true + yield + ensure + $VERBOSE = old_verbose + end + # Silences stderr for the duration of the block. # # silence_stderr do @@ -54,26 +62,27 @@ class Object rescue Errno::ENOENT => e STDERR.puts "#$0: #{e}" end - - # Method that requires a library, ensuring that rubygems is loaded + + # Require a library with fallback to RubyGems. Warnings during library + # loading are silenced to increase signal/noise for application warnings. 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 + silence_warnings do begin require library_name - rescue LoadError => gem_not_installed - raise cannot_require + 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 - - end diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 30911453bc..b0aca1ee97 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -1,5 +1,6 @@ require File.dirname(__FILE__) + '/module_attribute_accessors' require File.dirname(__FILE__) + '/core_ext/load_error' +require File.dirname(__FILE__) + '/core_ext/kernel' module Dependencies #:nodoc: extend self @@ -37,12 +38,7 @@ module Dependencies #:nodoc: def require_or_load(file_name) file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb' if load? - begin - original_verbosity, $VERBOSE = $VERBOSE, true - load file_name - ensure - $VERBOSE = original_verbosity - end + enable_warnings { load file_name } else require file_name end |