Permalink
Browse files

Delegate callsite signature and columns to updateable result set, mak…

…ing scrooged attributes lighter. Only dup columns set when needed.
  • Loading branch information...
1 parent dbf541a commit e1e98213e9ade50d60f63ef5bca4396808ec1ed5 @sdsykes sdsykes committed Mar 31, 2009
@@ -31,14 +31,12 @@ class ScroogedAttributes < Hash
# Hash container for attributes with scrooge monitoring of attribute access
#
- attr_accessor :callsite_signature, :scrooge_columns, :fully_fetched, :klass, :updateable_result_set
+ attr_accessor :fully_fetched, :klass, :updateable_result_set
- def self.setup(record, scrooge_columns, klass, callsite_signature, updateable_result_set)
+ def self.setup(record, klass, updateable_result_set)
hash = new.replace(record)
- hash.scrooge_columns = scrooge_columns.dup
hash.fully_fetched = false
hash.klass = klass
- hash.callsite_signature = callsite_signature
hash.updateable_result_set = updateable_result_set
hash
end
@@ -65,7 +63,7 @@ def [](attr_name)
if interesting_for_scrooge?( attr_name )
augment_callsite!( attr_name )
fetch_remaining
- @scrooge_columns << attr_name
+ add_to_scrooge_columns(attr_name)
end
super
end
@@ -76,7 +74,7 @@ def fetch(*args, &block)
end
def []=(attr_name, value)
- @scrooge_columns << attr_name
+ add_to_scrooge_columns(attr_name)
super
end
@@ -110,35 +108,50 @@ def update(hash)
def fetch_remaining
unless @fully_fetched
- columns_to_fetch = @klass.column_names - @scrooge_columns.to_a
+ columns_to_fetch = @klass.column_names - scrooge_columns.to_a
unless columns_to_fetch.empty?
fetch_remaining!( columns_to_fetch )
end
@fully_fetched = true
end
end
-
+
+ def callsite_signature
+ @updateable_result_set.callsite_signature
+ end
+
+ def scrooge_columns
+ @scrooge_columns || @updateable_result_set.scrooge_columns
+ end
+
protected
def fetch_remaining!( columns_to_fetch )
@updateable_result_set.updaters_attributes = self # for after_initialize & after_find
@updateable_result_set.reload_columns!(columns_to_fetch)
end
- def interesting_for_scrooge?( attr_s )
- @klass.columns_hash.has_key?(attr_s) && !@scrooge_columns.include?(attr_s)
+ def interesting_for_scrooge?( attr_name )
+ @klass.columns_hash.has_key?(attr_name) && !scrooge_columns.include?(attr_name)
+ end
+
+ def augment_callsite!( attr_name )
+ @klass.scrooge_seen_column!(callsite_signature, attr_name)
end
- def augment_callsite!( attr_s )
- @klass.scrooge_seen_column!(callsite_signature, attr_s)
+ def add_to_scrooge_columns(attr_name)
+ unless frozen?
+ @scrooge_columns ||= @updateable_result_set.scrooge_columns.dup
+ @scrooge_columns << attr_name
+ end
end
def primary_key_name
@klass.primary_key
end
def dup_self
- @scrooge_columns = @scrooge_columns.dup
+ @scrooge_columns = @scrooge_columns.dup if @scrooge_columns
self
end
end
@@ -75,17 +75,17 @@ def find_by_sql_with_scrooge(sql)
callsite_sig = callsite_signature( caller, callsite_sql( sql ) )
callsite = scrooge_callsite(callsite_sig)
callsite.reset
+ site_columns = callsite.columns
- sql = sql.gsub(scrooge_select_regex, "SELECT #{scrooge_select_sql(callsite.columns)} FROM")
+ sql = sql.gsub(scrooge_select_regex, "SELECT #{scrooge_select_sql(site_columns)} FROM")
results = connection.select_all(sanitize_sql(sql), "#{name} Load Scrooged")
result_set = ResultSets::ResultArray.new
- updateable = ResultSets::UpdateableResultSet.new(result_set, self)
- site_columns = callsite.columns
+ updateable = ResultSets::UpdateableResultSet.new(result_set, self, callsite_sig, site_columns.dup)
results.collect! do |record|
- instantiate(ScroogedAttributes.setup(record, site_columns, self, callsite_sig, updateable))
+ instantiate(ScroogedAttributes.setup(record, self, updateable))
end
result_set.replace(results)
@@ -217,7 +217,9 @@ def scrooge_fetch_remaining
# Dumped objects should not contain object_ids of old result sets
#
def scrooge_invalidate_updateable_result_set
- @attributes.updateable_result_set = ResultSets::UpdateableResultSet.new(nil, self) if scrooged?
+ if scrooged?
+ @attributes.updateable_result_set = ResultSets::UpdateableResultSet.new(nil, self, callsite_signature, @attributes.scrooge_columns)
+ end
end
# New objects should get an UnscroogedAttributes as their @attributes hash
@@ -7,13 +7,16 @@ class UpdateableResultSet
#
attr_accessor :updaters_attributes
+ attr_reader :callsite_signature, :scrooge_columns
- def initialize(result_set_array, klass)
+ def initialize(result_set_array, klass, callsite_signature, scrooge_columns)
if result_set_array
@result_set_object_id = result_set_array.object_id
@unique_id = result_set_array.unique_id ||= "#{Time.now.to_f}#{object_id}" # avoid recycled object ids
end
@klass = klass # expected class of items in the array
+ @callsite_signature = callsite_signature
+ @scrooge_columns = scrooge_columns
end
# Called by a ScroogedAttributes hash when it is asked for a column
View
@@ -27,7 +27,7 @@ class << self
#
def find_by_sql(sql)
if scope_with_scrooge?(sql)
- find_by_sql_with_scrooge(sql)
+ find_by_sql_without_scrooge(sql)
else
find_by_sql_without_scrooge(sql)
end

0 comments on commit e1e9821

Please sign in to comment.