Sha256: 969d85f2d823ae7c6797d02abf32fe0eb357bdb6a613dec5db7757920b55406f

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

# Author: Stephen Sykes

module SlimScrooge
  # A ResultSet contains all the rows found by an sql query
  # A call to reload! will cause all the rows in the set to be fully loaded
  # from the database - this should be called when a column access that hasn't previously
  # been seen by SlimScrooge is encountered
  #
  class ResultSet
    attr_reader :rows, :callsite_key
    
    def initialize(rows, callsite_key, fetched_columns)
      @rows = rows
      @callsite_key = callsite_key
      @fetched_columns = fetched_columns
    end
    
    def rows_by_key(key)
      @rows.inject({}) {|hash, row| hash[row[key]] = row; hash}
    end
    
    # Reload all the rows in the sql result at once
    # Reloads only those columns we didn't fetch the first time
    #
    def reload!
      callsite = Callsites[@callsite_key]
      rows_hash = rows_by_key(callsite.primary_key)
      sql = callsite.reload_sql(rows_hash.keys, @fetched_columns)
      model_class = callsite.model_class
      new_rows = model_class.connection.send(:select, sql, "#{model_class.name} Reload SlimScrooged")
      new_rows.each do |row|
        if old_row = rows_hash[row[callsite.primary_key]]
          old_row.result_set = nil
          old_row.monitored_columns.merge!(row)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
slim_scrooge-1.0.3 lib/slim_scrooge/result_set.rb
slim_scrooge-1.0.2 lib/slim_scrooge/result_set.rb
slim_scrooge-1.0.1 lib/slim_scrooge/result_set.rb