Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3134

Show
Ignore:
Timestamp:
11/21/05 07:29:44 (3 years ago)
Author:
bitsweat
Message:

r3240@asus: jeremy | 2005-11-20 23:22:34 -0800
Introduce enable_warnings counterpart to silence_warnings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r3124 r3134  
    11*SVN* 
    22 
    3 * Turn warnings on when loading a file if Dependencies.mechanism == :load.  Common mistakes such as redefined methods will print warnings to stderr.  [Jeremy Kemper] 
     3* Introduce enable_warnings counterpart to silence_warnings.  Turn warnings on when loading a file if Dependencies.mechanism == :load.  Common mistakes such as redefined methods will print warnings to stderr.  [Jeremy Kemper] 
    44 
    55* Add Symbol#to_proc, which allows for, e.g. [:foo, :bar].map(&:to_s). [Marcel Molina Jr.] 
  • trunk/activesupport/lib/active_support/core_ext/kernel.rb

    r2902 r3134  
    3030  end 
    3131 
     32  # Sets $VERBOSE to true for the duration of the block and back to its original value afterwards. 
     33  def enable_warnings 
     34    old_verbose, $VERBOSE = $VERBOSE, true 
     35    yield 
     36  ensure 
     37    $VERBOSE = old_verbose 
     38  end 
     39 
    3240  # Silences stderr for the duration of the block. 
    3341  # 
     
    5563    STDERR.puts "#$0: #{e}" 
    5664  end 
    57    
    58   # Method that requires a library, ensuring that rubygems is loaded 
     65 
     66  # Require a library with fallback to RubyGems.  Warnings during library 
     67  # loading are silenced to increase signal/noise for application warnings. 
    5968  def require_library_or_gem(library_name) 
    60     begin 
    61       require library_name 
    62     rescue LoadError => cannot_require 
    63       # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. 
    64       begin 
    65         require 'rubygems' 
    66       rescue LoadError => rubygems_not_installed 
    67         raise cannot_require 
    68       end 
    69       # 2. Rubygems is installed and loaded. Try to load the library again 
     69    silence_warnings do 
    7070      begin 
    7171        require library_name 
    72       rescue LoadError => gem_not_installed 
    73         raise cannot_require 
     72      rescue LoadError => cannot_require 
     73        # 1. Requiring the module is unsuccessful, maybe it's a gem and nobody required rubygems yet. Try. 
     74        begin 
     75          require 'rubygems' 
     76        rescue LoadError => rubygems_not_installed 
     77          raise cannot_require 
     78        end 
     79        # 2. Rubygems is installed and loaded. Try to load the library again 
     80        begin 
     81          require library_name 
     82        rescue LoadError => gem_not_installed 
     83          raise cannot_require 
     84        end 
    7485      end 
    7586    end 
    7687  end 
    77  
    78  
    7988end 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r3124 r3134  
    11require File.dirname(__FILE__) + '/module_attribute_accessors' 
    22require File.dirname(__FILE__) + '/core_ext/load_error' 
     3require File.dirname(__FILE__) + '/core_ext/kernel' 
    34 
    45module Dependencies #:nodoc: 
     
    3839    file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb' 
    3940    if load? 
    40       begin 
    41         original_verbosity, $VERBOSE = $VERBOSE, true 
    42         load file_name 
    43       ensure 
    44         $VERBOSE = original_verbosity 
    45       end 
     41      enable_warnings { load file_name } 
    4642    else 
    4743      require file_name 
  • trunk/activesupport/test/core_ext/kernel_test.rb

    r3131 r3134  
    1010  def test_silence_warnings_verbose_invariant 
    1111    old_verbose = $VERBOSE 
    12     begin 
    13       silence_warnings { raise } 
    14       flunk 
    15     rescue 
    16       assert_equal old_verbose, $VERBOSE 
    17     end 
     12    silence_warnings { raise } 
     13    flunk 
     14  rescue 
     15    assert_equal old_verbose, $VERBOSE 
    1816  end 
    19    
    20   def test_silence_warnings_with_return_value 
    21     assert_equal 1, silence_warnings { 1 } 
     17 
     18 
     19  def test_enable_warnings 
     20    enable_warnings { assert_equal true, $VERBOSE } 
     21    assert_equal 1234, enable_warnings { 1234 } 
    2222  end 
    23    
     23 
     24  def test_enable_warnings_verbose_invariant 
     25    old_verbose = $VERBOSE 
     26    enable_warnings { raise } 
     27    flunk 
     28  rescue 
     29    assert_equal old_verbose, $VERBOSE 
     30  end 
     31 
     32 
    2433  def test_silence_stderr 
    2534    old_stderr_position = STDERR.tell 
     
    2736    assert_equal old_stderr_position, STDERR.tell 
    2837  end 
    29    
     38 
    3039  def test_silence_stderr_with_return_value 
    3140    assert_equal 1, silence_stderr { 1 }