aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2010-06-05 20:33:53 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2010-06-05 22:05:58 -0700
commitfd1a5041362f5e65b813b7938d477143bd82de40 (patch)
tree5365eede7fe25b23d2a3f37beb21c11ddad281b0 /activesupport
parent35ae42be4f9e23ae954e4705276b460998cb401b (diff)
downloadrails-fd1a5041362f5e65b813b7938d477143bd82de40.tar.gz
rails-fd1a5041362f5e65b813b7938d477143bd82de40.tar.bz2
rails-fd1a5041362f5e65b813b7938d477143bd82de40.zip
ActiveSupport::Dependencies.constantize shortcut for caching named constant lookups
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/conversions.rb11
-rw-r--r--activesupport/lib/active_support/core_ext/string/inflections.rb11
-rw-r--r--activesupport/lib/active_support/dependencies.rb12
-rw-r--r--activesupport/test/dependencies_test.rb6
4 files changed, 26 insertions, 14 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb
index 6a243fe982..cd7a42ed2b 100644
--- a/activesupport/lib/active_support/core_ext/string/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -47,4 +47,15 @@ class String
d[5] += d.pop
::DateTime.civil(*d)
end
+
+ # +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.
+ #
+ # Examples
+ # "Module".constantize # => Module
+ # "Class".constantize # => Class
+ def constantize
+ ActiveSupport::Inflector.constantize(self)
+ end
end
diff --git a/activesupport/lib/active_support/core_ext/string/inflections.rb b/activesupport/lib/active_support/core_ext/string/inflections.rb
index 48b028bb64..ef479d559e 100644
--- a/activesupport/lib/active_support/core_ext/string/inflections.rb
+++ b/activesupport/lib/active_support/core_ext/string/inflections.rb
@@ -146,15 +146,4 @@ class String
def foreign_key(separate_class_name_and_id_with_underscore = true)
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
end
-
- # +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.
- #
- # Examples
- # "Module".constantize # => Module
- # "Class".constantize # => Class
- def constantize
- ActiveSupport::Inflector.constantize(self)
- end
end
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb
index e0d2b70306..5091abc3a4 100644
--- a/activesupport/lib/active_support/dependencies.rb
+++ b/activesupport/lib/active_support/dependencies.rb
@@ -484,8 +484,10 @@ module ActiveSupport #:nodoc:
end
class Reference
- def initialize(constant, name)
- @constant, @name = constant, name
+ attr_reader :name
+
+ def initialize(name, constant = nil)
+ @name, @constant = name, constant
end
def get
@@ -498,7 +500,11 @@ module ActiveSupport #:nodoc:
end
def ref(name)
- references[name] ||= Reference.new(Inflector.constantize(name), name)
+ references[name] ||= Reference.new(name)
+ end
+
+ def constantize(name)
+ ref(name).get
end
# Determine if the given constant has been automatically loaded.
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb
index 15543e07c3..5422c75f5f 100644
--- a/activesupport/test/dependencies_test.rb
+++ b/activesupport/test/dependencies_test.rb
@@ -446,6 +446,12 @@ class DependenciesTest < Test::Unit::TestCase
end
end
+ def test_constantize_shortcut_for_cached_constant_lookups
+ with_loading 'dependencies' do
+ assert_equal ServiceOne, ActiveSupport::Dependencies.constantize("ServiceOne")
+ end
+ end
+
def test_nested_load_error_isnt_rescued
with_loading 'dependencies' do
assert_raise(MissingSourceFile) do