aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/dependencies.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/lib/active_support/dependencies.rb')
-rw-r--r--activesupport/lib/active_support/dependencies.rb43
1 files changed, 32 insertions, 11 deletions
diff --git a/activesupport/lib/active_support/dependencies.rb b/activesupport/lib/active_support/dependencies.rb
index dab6fdbac6..94a8608aeb 100644
--- a/activesupport/lib/active_support/dependencies.rb
+++ b/activesupport/lib/active_support/dependencies.rb
@@ -524,31 +524,52 @@ module ActiveSupport #:nodoc:
explicitly_unloadable_constants.each { |const| remove_constant const }
end
- class Reference
- @@constants = Hash.new { |h, k| h[k] = Inflector.constantize(k) }
+ class ClassCache
+ def initialize
+ @store = Hash.new { |h, k| h[k] = Inflector.constantize(k) }
+ end
+
+ def empty?
+ @store.empty?
+ end
+
+ def key?(key)
+ @store.key?(key)
+ end
- attr_reader :name
+ def []=(key, value)
+ return unless key.respond_to?(:name)
- def initialize(name)
- @name = name.to_s
- @@constants[@name] = name if name.respond_to?(:name)
+ raise(ArgumentError, 'anonymous classes cannot be cached') unless key.name
+
+ @store[key.name] = value
+ end
+
+ def [](key)
+ key = key.name if key.respond_to?(:name)
+
+ @store[key]
end
+ alias :get :[]
- def get
- @@constants[@name]
+ def new(name)
+ self[name] = name
+ self
end
- def self.clear!
- @@constants.clear
+ def clear!
+ @store.clear
end
end
+ Reference = ClassCache.new
+
def ref(name)
references[name] ||= Reference.new(name)
end
def constantize(name)
- ref(name).get
+ ref(name).get(name)
end
# Determine if the given constant has been automatically loaded.