diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-01 17:20:10 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-03-01 17:20:35 -0800 |
commit | 7b6bfe84f332a3c99656f73cf0251bce0a16ba88 (patch) | |
tree | fe0fa3c7ef7d0638b9ef9f3db1b259ade1c656de /activesupport/lib | |
parent | 50ed1a25a4e496a7a67861b1a04d667bd3445bb6 (diff) | |
download | rails-7b6bfe84f332a3c99656f73cf0251bce0a16ba88.tar.gz rails-7b6bfe84f332a3c99656f73cf0251bce0a16ba88.tar.bz2 rails-7b6bfe84f332a3c99656f73cf0251bce0a16ba88.zip |
refactor Reference to a ClassCache object, fix lazy lookup in Middleware so that anonymous classes are supported
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/dependencies.rb | 43 |
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. |