Skip to content
This repository
Browse code

Stop using defined? in Dependencies.qualified_const_defined? since de…

…fined? may invoke const_missing.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4774 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 593f04e6a939caeed276b855fc7fa35655ba1204 1 parent ae74e8e
Nicholas Seckar authored August 16, 2006
2  activesupport/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing. [Nicholas Seckar]
  4
+
3 5
 * Dependencies can autoload directories of nested classes. [Jeremy Kemper]
4 6
     Example:
5 7
       invoice.rb            class Invoice
12  activesupport/lib/active_support/dependencies.rb
@@ -96,7 +96,17 @@ def require_or_load(file_name, const_path = nil)
96 96
   def qualified_const_defined?(path)
97 97
     raise NameError, "#{path.inspect} is not a valid constant name!" unless
98 98
       /^(::)?([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
100 110
   end
101 111
   
102 112
   # Given +path+ return an array of constant paths which would cause Dependencies
16  activesupport/test/dependencies_test.rb
... ...
@@ -1,5 +1,13 @@
1 1
 require File.dirname(__FILE__) + '/abstract_unit'
2 2
 
  3
+module ModuleWithMissing
  4
+  mattr_accessor :missing_count
  5
+  def self.const_missing(name)
  6
+    self.missing_count += 1
  7
+    name
  8
+  end
  9
+end
  10
+
3 11
 class DependenciesTest < Test::Unit::TestCase
4 12
   
5 13
   def teardown
@@ -249,6 +257,14 @@ def test_qualified_const_defined
249 257
     assert Dependencies.qualified_const_defined?("::Test::Unit::TestCase")
250 258
   end
251 259
   
  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
+  
252 268
   def test_autoloaded?
253 269
     with_loading 'autoloading_fixtures' do
254 270
       assert ! Dependencies.autoloaded?("ModuleFolder")

0 notes on commit 593f04e

Please sign in to comment.
Something went wrong with that request. Please try again.