From 7b6bfe84f332a3c99656f73cf0251bce0a16ba88 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 1 Mar 2011 17:20:10 -0800 Subject: refactor Reference to a ClassCache object, fix lazy lookup in Middleware so that anonymous classes are supported --- activesupport/lib/active_support/dependencies.rb | 43 ++++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) (limited to 'activesupport/lib') 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. -- cgit v1.2.3