Skip to content

Commit

Permalink
Stop using defined? in Dependencies.qualified_const_defined? since de…
Browse files Browse the repository at this point in the history
…fined? may invoke const_missing.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4774 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
seckar committed Aug 16, 2006
1 parent ae74e8e commit 593f04e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion activesupport/lib/active_support/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions activesupport/test/dependencies_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 593f04e

Please sign in to comment.