aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorRyan Oblak <rroblak@gmail.com>2011-09-22 12:17:42 -0700
committerJosé Valim <jose.valim@gmail.com>2011-09-23 15:36:33 +0200
commit310565f537b5eeb134e9a4bb0801358432f03e04 (patch)
tree03ffd726f0788a64c2163d1130727b6a2d8a2aa3 /activesupport
parentd62fc8e0211ca2657899f061888d1756a3c257dd (diff)
downloadrails-310565f537b5eeb134e9a4bb0801358432f03e04.tar.gz
rails-310565f537b5eeb134e9a4bb0801358432f03e04.tar.bz2
rails-310565f537b5eeb134e9a4bb0801358432f03e04.zip
Added ActiveSupport::Inflector.safe_constantize and String#safe_constantize; refactored common constantize tests into ConstantizeTestCases
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/inflections.rb19
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb27
-rw-r--r--activesupport/test/constantize_test_cases.rb31
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb23
-rw-r--r--activesupport/test/inflector_test.rb27
5 files changed, 106 insertions, 21 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb
index 002688d6c0..b4218cb42c 100644
--- a/activesupport/lib/active_support/core_ext/string/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -33,14 +33,27 @@ class String
# +constantize+ tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase
- # or is not initialized.
+ # or is not initialized. See ActiveSupport::Inflector.constantize
#
# Examples
- # "Module".constantize # => Module
- # "Class".constantize # => Class
+ # "Module".constantize # => Module
+ # "Class".constantize # => Class
+ # "blargle".safe_constantize # => NameError: wrong constant name blargle
def constantize
ActiveSupport::Inflector.constantize(self)
end
+
+ # +safe_constantize+ tries to find a declared constant with the name specified
+ # in the string. It returns nil when the name is not in CamelCase
+ # or is not initialized. See ActiveSupport::Inflector.safe_constantize
+ #
+ # Examples
+ # "Module".safe_constantize # => Module
+ # "Class".safe_constantize # => Class
+ # "blargle".safe_constantize # => nil
+ def safe_constantize
+ ActiveSupport::Inflector.safe_constantize(self)
+ end
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb
index 423b5abd20..b22e39c7c2 100644
--- a/activesupport/lib/active_support/inflector/methods.rb
+++ b/activesupport/lib/active_support/inflector/methods.rb
@@ -224,6 +224,33 @@ module ActiveSupport
end
end
+ # Tries to find a constant with the name specified in the argument string:
+ #
+ # "Module".safe_constantize # => Module
+ # "Test::Unit".safe_constantize # => Test::Unit
+ #
+ # The name is assumed to be the one of a top-level constant, no matter whether
+ # it starts with "::" or not. No lexical context is taken into account:
+ #
+ # C = 'outside'
+ # module M
+ # C = 'inside'
+ # C # => 'inside'
+ # "C".safe_constantize # => 'outside', same as ::C
+ # end
+ #
+ # nil is returned when the name is not in CamelCase or the constant is
+ # unknown.
+ #
+ # "blargle".safe_constantize # => nil
+ def safe_constantize(camel_cased_word)
+ begin
+ camel_cased_word.constantize
+ rescue NameError
+ nil
+ end
+ end
+
# Turns a number into an ordinal string used to denote the position in an
# ordered sequence such as 1st, 2nd, 3rd, 4th.
#
diff --git a/activesupport/test/constantize_test_cases.rb b/activesupport/test/constantize_test_cases.rb
new file mode 100644
index 0000000000..b8a866b6c9
--- /dev/null
+++ b/activesupport/test/constantize_test_cases.rb
@@ -0,0 +1,31 @@
+module Ace
+ module Base
+ class Case
+ end
+ end
+end
+
+module ConstantizeTestCases
+ def run_constantize_tests_on
+ assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") }
+ assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") }
+ assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
+ assert_raise(NameError) { yield("UnknownClass") }
+ assert_raise(NameError) { yield("An invalid string") }
+ assert_raise(NameError) { yield("InvalidClass\n") }
+ assert_raise(NameError) { yield("Ace::Base::ConstantizeTestCases") }
+ end
+
+ def run_safe_constantize_tests_on
+ assert_nothing_raised { assert_equal Ace::Base::Case, yield("Ace::Base::Case") }
+ assert_nothing_raised { assert_equal Ace::Base::Case, yield("::Ace::Base::Case") }
+ assert_nothing_raised { assert_equal ConstantizeTestCases, yield("ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal ConstantizeTestCases, yield("::ConstantizeTestCases") }
+ assert_nothing_raised { assert_equal nil, yield("UnknownClass") }
+ assert_nothing_raised { assert_equal nil, yield("An invalid string") }
+ assert_nothing_raised { assert_equal nil, yield("InvalidClass\n") }
+ assert_nothing_raised { assert_equal nil, yield("blargle") }
+ assert_nothing_raised { assert_equal nil, yield("Ace::Base::ConstantizeTestCases") }
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 81a284dded..5c1dddaf96 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -2,6 +2,7 @@
require 'date'
require 'abstract_unit'
require 'inflector_test_cases'
+require 'constantize_test_cases'
require 'active_support/inflector'
require 'active_support/core_ext/string'
@@ -9,9 +10,17 @@ require 'active_support/time'
require 'active_support/core_ext/string/strip'
require 'active_support/core_ext/string/output_safety'
+module Ace
+ module Base
+ class Case
+ end
+ end
+end
+
class StringInflectionsTest < Test::Unit::TestCase
include InflectorTestCases
-
+ include ConstantizeTestCases
+
def test_erb_escape
string = [192, 60].pack('CC')
expected = 192.chr + "&lt;"
@@ -292,6 +301,18 @@ class StringInflectionsTest < Test::Unit::TestCase
"\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8').truncate(10)
end
end
+
+ def test_constantize
+ run_constantize_tests_on do |string|
+ string.constantize
+ end
+ end
+
+ def test_safe_constantize
+ run_safe_constantize_tests_on do |string|
+ string.safe_constantize
+ end
+ end
end
class StringBehaviourTest < Test::Unit::TestCase
diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb
index b9e299af75..5c956e0075 100644
--- a/activesupport/test/inflector_test.rb
+++ b/activesupport/test/inflector_test.rb
@@ -2,16 +2,11 @@ require 'abstract_unit'
require 'active_support/inflector'
require 'inflector_test_cases'
-
-module Ace
- module Base
- class Case
- end
- end
-end
+require 'constantize_test_cases'
class InflectorTest < Test::Unit::TestCase
include InflectorTestCases
+ include ConstantizeTestCases
def test_pluralize_plurals
assert_equal "plurals", ActiveSupport::Inflector.pluralize("plurals")
@@ -282,17 +277,15 @@ class InflectorTest < Test::Unit::TestCase
end
def test_constantize
- assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("Ace::Base::Case") }
- assert_nothing_raised { assert_equal Ace::Base::Case, ActiveSupport::Inflector.constantize("::Ace::Base::Case") }
- assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("InflectorTest") }
- assert_nothing_raised { assert_equal InflectorTest, ActiveSupport::Inflector.constantize("::InflectorTest") }
- assert_raise(NameError) { ActiveSupport::Inflector.constantize("UnknownClass") }
- assert_raise(NameError) { ActiveSupport::Inflector.constantize("An invalid string") }
- assert_raise(NameError) { ActiveSupport::Inflector.constantize("InvalidClass\n") }
+ run_constantize_tests_on do |string|
+ ActiveSupport::Inflector.constantize(string)
+ end
end
-
- def test_constantize_does_lexical_lookup
- assert_raise(NameError) { ActiveSupport::Inflector.constantize("Ace::Base::InflectorTest") }
+
+ def test_safe_constantize
+ run_safe_constantize_tests_on do |string|
+ ActiveSupport::Inflector.safe_constantize(string)
+ end
end
def test_ordinal