diff options
author | Nicholas Seckar <nseckar@gmail.com> | 2006-08-16 17:50:52 +0000 |
---|---|---|
committer | Nicholas Seckar <nseckar@gmail.com> | 2006-08-16 17:50:52 +0000 |
commit | 593f04e6a939caeed276b855fc7fa35655ba1204 (patch) | |
tree | 32560660627730aa894d8164aabe3beac54c5b14 /activesupport | |
parent | ae74e8e9feba99b84f5e431239dc6a2039ad5793 (diff) | |
download | rails-593f04e6a939caeed276b855fc7fa35655ba1204.tar.gz rails-593f04e6a939caeed276b855fc7fa35655ba1204.tar.bz2 rails-593f04e6a939caeed276b855fc7fa35655ba1204.zip |
Stop using defined? in Dependencies.qualified_const_defined? since defined? may invoke const_missing.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4774 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 12 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 16 |
3 files changed, 29 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 90b476f667..485e8edc83 100644 --- a/activesupport/CHANGELOG +++ b/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 diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 6241a822a4..b8a8a9b630 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -96,7 +96,17 @@ module Dependencies #:nodoc: 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 diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 92f3e63e1a..3d0ada47f1 100644 --- a/activesupport/test/dependencies_test.rb +++ b/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 @@ class DependenciesTest < Test::Unit::TestCase 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") |