aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activesupport/CHANGELOG2
-rw-r--r--activesupport/lib/active_support/inflector.rb8
-rw-r--r--activesupport/test/inflector_test.rb5
3 files changed, 12 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 58727c3533..d9a94b47d1 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*1.2.3* (November 7th, 2005)
+* Change Inflector#constantize to use eval instead of const_get. [Nicholas Seckar]
+
* Fix const_missing handler to ignore the trailing '.rb' on files when comparing paths. [Nicholas Seckar]
* Define kernel.rb methods in "class Object" instead of "module Kernel" to work around a Windows peculiarity [Sam Stephenson]
diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb
index 7009628cb3..2e69b74c54 100644
--- a/activesupport/lib/active_support/inflector.rb
+++ b/activesupport/lib/active_support/inflector.rb
@@ -142,9 +142,11 @@ module Inflector
end
def constantize(camel_cased_word)
- camel_cased_word.split("::").inject(Object) do |final_type, part|
- final_type = final_type.const_get(part)
- end
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" unless
+ camel_cased_word.split("::").all? { |part| /^[A-Z]\w*$/ =~ part }
+
+ camel_cased_word = "::#{camel_cased_word}" unless camel_cased_word[0, 2] == '::'
+ Object.module_eval(camel_cased_word, __FILE__, __LINE__)
end
def ordinalize(number)
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index 995871f826..d92b4a76a8 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -276,6 +276,11 @@ class InflectorTest < Test::Unit::TestCase
assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case")
assert_equal InflectorTest, Inflector.constantize("InflectorTest")
assert_raises(NameError) { Inflector.constantize("UnknownClass") }
+ assert_raises(NameError) { Inflector.constantize("An invalid string") }
+ end
+
+ def test_constantize_doesnt_look_in_parent
+ assert_raises(NameError) { Inflector.constantize("Ace::Base::InflectorTest") }
end
def test_ordinal