Skip to content

Commit

Permalink
Fix constantize to handle names beginning with '::'. Closes #3803.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3571 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
seckar committed Feb 11, 2006
1 parent b9a9893 commit 91cdd59
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Fix constantize to properly handle names beginning with '::'. [Nicholas Seckar]

* Make String#last return the string instead of nil when it is shorter than the limit [Scott Barron].

* Added delegation support to Module that allows multiple delegations at once (unlike Forwardable in the stdlib) [DHH]. Example:
Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/inflector.rb
Expand Up @@ -143,9 +143,9 @@ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)

def constantize(camel_cased_word)
raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" unless
camel_cased_word.split("::").all? { |part| /^[A-Z]\w*$/ =~ part }
/^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word

camel_cased_word = "::#{camel_cased_word}" unless camel_cased_word[0, 2] == '::'
camel_cased_word = "::#{camel_cased_word}" unless $1
Object.module_eval(camel_cased_word, __FILE__, __LINE__)
end

Expand Down
2 changes: 2 additions & 0 deletions activesupport/test/inflector_test.rb
Expand Up @@ -274,7 +274,9 @@ def test_humanize

def test_constantize
assert_equal Ace::Base::Case, Inflector.constantize("Ace::Base::Case")
assert_equal Ace::Base::Case, Inflector.constantize("::Ace::Base::Case")
assert_equal InflectorTest, Inflector.constantize("InflectorTest")
assert_equal InflectorTest, Inflector.constantize("::InflectorTest")
assert_raises(NameError) { Inflector.constantize("UnknownClass") }
assert_raises(NameError) { Inflector.constantize("An invalid string") }
end
Expand Down

0 comments on commit 91cdd59

Please sign in to comment.