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

Changeset 4774

Show
Ignore:
Timestamp:
08/16/06 17:50:52 (2 years ago)
Author:
ulysses
Message:

Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing.

Files:

Legend:

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

    r4769 r4774  
    11*SVN* 
     2 
     3* Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing. [Nicholas Seckar] 
    24 
    35* Dependencies can autoload directories of nested classes. [Jeremy Kemper] 
  • trunk/activesupport/lib/active_support/dependencies.rb

    r4769 r4774  
    9797    raise NameError, "#{path.inspect} is not a valid constant name!" unless 
    9898      /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path 
    99     Object.module_eval("defined?(#{path})", __FILE__, __LINE__) 
     99     
     100    names = path.split('::') 
     101    names.shift if names.first.empty? 
     102     
     103    # We can't use defined? because it will invoke const_missing for the parent 
     104    # of the name we are checking. 
     105    names.inject(Object) do |mod, name| 
     106      return false unless mod.const_defined? name 
     107      mod.const_get name 
     108    end 
     109    return true 
    100110  end 
    101111   
  • trunk/activesupport/test/dependencies_test.rb

    r4769 r4774  
    11require File.dirname(__FILE__) + '/abstract_unit' 
     2 
     3module ModuleWithMissing 
     4  mattr_accessor :missing_count 
     5  def self.const_missing(name) 
     6    self.missing_count += 1 
     7    name 
     8  end 
     9end 
    210 
    311class DependenciesTest < Test::Unit::TestCase 
     
    250258  end 
    251259   
     260  def test_qualified_const_defined_should_not_call_method_missing 
     261    ModuleWithMissing.missing_count = 0 
     262    assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A") 
     263    assert_equal 0, ModuleWithMissing.missing_count 
     264    assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A::B") 
     265    assert_equal 0, ModuleWithMissing.missing_count 
     266  end 
     267   
    252268  def test_autoloaded? 
    253269    with_loading 'autoloading_fixtures' do