Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added some method documentation

  • Loading branch information...
commit 1e381c0990d7ed6792270abafc56aef1cd108374 1 parent 6f6d2ab
@sdsykes sdsykes authored
Showing with 20 additions and 0 deletions.
  1. +20 −0 lib/optimizations/result_sets/updateable_result_set.rb
View
20 lib/optimizations/result_sets/updateable_result_set.rb
@@ -16,6 +16,9 @@ def initialize(result_set_array, klass)
@klass = klass # expected class of items in the array
end
+ # Called by a ScroogedAttributes hash when it is asked for a column
+ # that was not fetched from the DB
+ #
def reload_columns!(columns_to_fetch)
reloaded_columns = hash_by_primary_key(reload_columns_for_ids(columns_to_fetch, result_set_ids))
if !reloaded_columns.has_key?(@updaters_attributes[primary_key_name])
@@ -40,6 +43,10 @@ def result_set_attributes
end.uniq
end
+ # The result set is weak referenced by its object_id
+ # So we check that a unique id matches what we have remembered to make sure
+ # that we got the right object (object ids are recycled by ruby)
+ #
def result_set
return nil unless @result_set_object_id
result_set = ObjectSpace._id2ref(@result_set_object_id)
@@ -48,10 +55,17 @@ def result_set
nil
end
+ # When called from after_find and after_initialize, the object
+ # being accessed (and causing the reload) is not in the result set yet.
+ # We make sure that we add its attributes to result_set_attributes so it
+ # gets reloaded too
+ #
def default_attributes
[@updaters_attributes]
end
+ # Ids of the items to be reloaded
+ #
def result_set_ids
result_set_attributes.inject([]) do |memo, attributes|
unless attributes.fully_fetched
@@ -61,6 +75,9 @@ def result_set_ids
end
end
+ # Update the result set with attributes provided, as returned by
+ # the sql server
+ #
def update_with(remaining_attributes)
current_attributes = hash_by_primary_key(result_set_attributes)
remaining_attributes.each do |r_id, r_att|
@@ -71,6 +88,9 @@ def update_with(remaining_attributes)
end
end
+ # Make an efficient data structure to access items when the
+ # primary key is known
+ #
def hash_by_primary_key(rows)
rows.inject({}) {|memo, row| memo[row[primary_key_name]] = row; memo}
end
Please sign in to comment.
Something went wrong with that request. Please try again.