Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@seckar seckar authored
View
2  activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing. [Nicholas Seckar]
+
* Dependencies can autoload directories of nested classes. [Jeremy Kemper]
Example:
invoice.rb class Invoice
View
12 activesupport/lib/active_support/dependencies.rb
@@ -96,7 +96,17 @@ def require_or_load(file_name, const_path = nil)
def qualified_const_defined?(path)
raise NameError, "#{path.inspect} is not a valid constant name!" unless
/^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ path
- Object.module_eval("defined?(#{path})", __FILE__, __LINE__)
+
+ names = path.split('::')
+ names.shift if names.first.empty?
+
+ # We can't use defined? because it will invoke const_missing for the parent
+ # of the name we are checking.
+ names.inject(Object) do |mod, name|
+ return false unless mod.const_defined? name
+ mod.const_get name
+ end
+ return true
end
# Given +path+ return an array of constant paths which would cause Dependencies
View
16 activesupport/test/dependencies_test.rb
@@ -1,5 +1,13 @@
require File.dirname(__FILE__) + '/abstract_unit'
+module ModuleWithMissing
+ mattr_accessor :missing_count
+ def self.const_missing(name)
+ self.missing_count += 1
+ name
+ end
+end
+
class DependenciesTest < Test::Unit::TestCase
def teardown
@@ -249,6 +257,14 @@ def test_qualified_const_defined
assert Dependencies.qualified_const_defined?("::Test::Unit::TestCase")
end
+ def test_qualified_const_defined_should_not_call_method_missing
+ ModuleWithMissing.missing_count = 0
+ assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A")
+ assert_equal 0, ModuleWithMissing.missing_count
+ assert ! Dependencies.qualified_const_defined?("ModuleWithMissing::A::B")
+ assert_equal 0, ModuleWithMissing.missing_count
+ end
+
def test_autoloaded?
with_loading 'autoloading_fixtures' do
assert ! Dependencies.autoloaded?("ModuleFolder")

0 comments on commit 593f04e

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