diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/name_error.rb | 7 | ||||
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 18 | ||||
-rw-r--r-- | activesupport/test/core_ext/name_error_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/dependencies_test.rb | 27 |
5 files changed, 53 insertions, 5 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index d652e1d17c..13fb1521f2 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Raise fully qualified names upon name errors. #5533 [lars@pinds.com, Nicholas Seckar] + * Add extention to obtain the missing constant from NameError instances. [Nicholas Seckar] * Thoroughly document inflections. #5700 [petermichaux@gmail.com] diff --git a/activesupport/lib/active_support/core_ext/name_error.rb b/activesupport/lib/active_support/core_ext/name_error.rb index 42b89d3d28..2b617b0083 100644 --- a/activesupport/lib/active_support/core_ext/name_error.rb +++ b/activesupport/lib/active_support/core_ext/name_error.rb @@ -9,7 +9,12 @@ class NameError < StandardError # Was this exception raised because the given name was missing? def missing_name?(name) - missing_name == name.to_s + if name.is_a? Symbol + last_name = (missing_name || '').split('::').last + last_name == name.to_s + else + missing_name == name.to_s + end end end
\ No newline at end of file diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb index 17ca446650..a58989173e 100644 --- a/activesupport/lib/active_support/dependencies.rb +++ b/activesupport/lib/active_support/dependencies.rb @@ -69,6 +69,11 @@ module Dependencies #:nodoc: history << file_name end + # Return the a constant path for the provided parent and constant name + def constant_path_for(mod, name) + ([Object, Kernel].include? mod) ? name.to_s : "#{mod}::#{name}" + end + class LoadingModule # Old style environment.rb referenced this method directly. Please note, it doesn't # actualy *do* anything any more. @@ -120,7 +125,8 @@ class Module #:nodoc: end end - raise NameError.new("uninitialized constant #{class_id}").copy_blame!(e) + qualified_name = Dependencies.constant_path_for self, class_id + raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) end end end @@ -130,7 +136,15 @@ class Class if [Object, Kernel].include?(self) || parent == self super else - parent.send :const_missing, class_id + begin + parent.send :const_missing, class_id + rescue NameError => e + # Make sure that the name we are missing is the one that caused the error + parent_qualified_name = Dependencies.constant_path_for parent, class_id + raise unless e.missing_name? parent_qualified_name + qualified_name = Dependencies.constant_path_for self, class_id + raise NameError.new("uninitialized constant #{qualified_name}").copy_blame!(e) + end end end end diff --git a/activesupport/test/core_ext/name_error_test.rb b/activesupport/test/core_ext/name_error_test.rb index 13910d2e01..e49b88eb38 100644 --- a/activesupport/test/core_ext/name_error_test.rb +++ b/activesupport/test/core_ext/name_error_test.rb @@ -8,9 +8,9 @@ class NameErrorTest < Test::Unit::TestCase SomeNameThatNobodyWillUse____Really ? 1 : 0 flunk "?!?!" rescue NameError => exc - assert_equal "SomeNameThatNobodyWillUse____Really", exc.missing_name + assert_equal "NameErrorTest::SomeNameThatNobodyWillUse____Really", exc.missing_name assert exc.missing_name?(:SomeNameThatNobodyWillUse____Really) - assert exc.missing_name?("SomeNameThatNobodyWillUse____Really") + assert exc.missing_name?("NameErrorTest::SomeNameThatNobodyWillUse____Really") end end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 397548973a..f19542bdb0 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -155,4 +155,31 @@ class DependenciesTest < Test::Unit::TestCase Object.send :remove_const, :ModuleFolder end end + + def test_non_existing_const_raises_name_error_with_fully_qualified_name + with_loading 'autoloading_fixtures' do + begin + A::DoesNotExist + flunk "No raise!!" + rescue NameError => e + assert_equal "uninitialized constant A::DoesNotExist", e.message + end + begin + A::B::DoesNotExist + flunk "No raise!!" + rescue NameError => e + assert_equal "uninitialized constant A::B::DoesNotExist", e.message + end + end + end + + def test_smart_name_error_strings + begin + Object.module_eval "ImaginaryObject" + flunk "No raise!!" + rescue NameError => e + assert e.message.include?("uninitialized constant ImaginaryObject") + end + end + end |