提交 593f04e6 编写于 作者: N Nicholas Seckar

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

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
上级 ae74e8e9
*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
......
......@@ -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
......
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")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册