aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/aggregations.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2011-01-07 13:53:34 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-07 13:53:34 -0800
commit344a2d5adca154a4d13539421bdf5ea7865e0235 (patch)
treeb0bab12e2f7a471dd253b56391beecb479e8de13 /activerecord/lib/active_record/aggregations.rb
parentf3d92f07565d1ec4b03fec7f3ba7c1c7d81e6073 (diff)
downloadrails-344a2d5adca154a4d13539421bdf5ea7865e0235.tar.gz
rails-344a2d5adca154a4d13539421bdf5ea7865e0235.tar.bz2
rails-344a2d5adca154a4d13539421bdf5ea7865e0235.zip
use a hash for caching aggregations rather than ivars
Diffstat (limited to 'activerecord/lib/active_record/aggregations.rb')
-rw-r--r--activerecord/lib/active_record/aggregations.rb18
1 files changed, 6 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/aggregations.rb b/activerecord/lib/active_record/aggregations.rb
index 0224187fed..4b1fa5c59c 100644
--- a/activerecord/lib/active_record/aggregations.rb
+++ b/activerecord/lib/active_record/aggregations.rb
@@ -4,9 +4,7 @@ module ActiveRecord
extend ActiveSupport::Concern
def clear_aggregation_cache #:nodoc:
- self.class.reflect_on_all_aggregations.to_a.each do |assoc|
- instance_variable_set "@#{assoc.name}", nil
- end if self.persisted?
+ @aggregation_cache.clear if persisted?
end
# Active Record implements aggregation through a macro-like class method called +composed_of+
@@ -224,11 +222,7 @@ module ActiveRecord
def reader_method(name, class_name, mapping, allow_nil, constructor)
module_eval do
define_method(name) do
- unless instance_variable_defined?("@#{name}")
- instance_variable_set("@#{name}", nil)
- end
-
- if (instance_variable_get("@#{name}").nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
+ if (@aggregation_cache[name].nil?) && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
attrs = mapping.collect {|pair| read_attribute(pair.first)}
object = case constructor
when Symbol
@@ -238,9 +232,9 @@ module ActiveRecord
else
raise ArgumentError, 'Constructor must be a symbol denoting the constructor method to call or a Proc to be invoked.'
end
- instance_variable_set("@#{name}", object)
+ @aggregation_cache[name] = object
end
- instance_variable_get("@#{name}")
+ @aggregation_cache[name]
end
end
@@ -251,7 +245,7 @@ module ActiveRecord
define_method("#{name}=") do |part|
if part.nil? && allow_nil
mapping.each { |pair| self[pair.first] = nil }
- instance_variable_set("@#{name}", nil)
+ @aggregation_cache[name] = nil
else
unless part.is_a?(class_name.constantize) || converter.nil?
part = case converter
@@ -265,7 +259,7 @@ module ActiveRecord
end
mapping.each { |pair| self[pair.first] = part.send(pair.last) }
- instance_variable_set("@#{name}", part.freeze)
+ @aggregation_cache[name] = part.freeze
end
end
end