Module PseudoCursors::ClassMethods
In: lib/pseudo_cursors.rb

Methods

Constants

DEFAULT_BATCH_SIZE = 100

Public Instance methods

[Source]

    # File lib/pseudo_cursors.rb, line 11
11:     def find_each (options = {})
12:       return nil unless block_given?
13:       
14:       batch_size = options[:batch_size] || DEFAULT_BATCH_SIZE
15:       options.delete(:batch_size)
16:       batch_size = batch_size.to_i
17:       batch_size = 1 if batch_size < 1
18:       
19:       wrap_in_transaction = options[:transaction]
20:       options.delete(:transaction)
21:       
22:       select_cursor_ids(options).in_groups_of(batch_size, false) do |row_ids|
23:         records = find(:all, :conditions => {primary_key => row_ids})
24:         
25:         if options[:order]
26:           sort_cursor_rows!(records, row_ids)
27:         end
28:         
29:         if wrap_in_transaction
30:           transaction do
31:             records.each{|record| yield(record)}
32:           end
33:         else
34:           records.each{|record| yield(record)}
35:         end
36:       end
37:       
38:       nil
39:     end

Protected Instance methods

[Source]

    # File lib/pseudo_cursors.rb, line 49
49:     def select_cursor_ids (options)
50:       ids_sql = send(:construct_finder_sql, options.merge(:select => "#{table_name}.#{primary_key}"))
51:       connection.select_all(ids_sql, "#{table_name} pseudo cursor").collect{|row| row['id'].to_i}
52:     end

[Source]

    # File lib/pseudo_cursors.rb, line 43
43:     def sort_cursor_rows! (records, row_ids)
44:       sort_hash = {}
45:       row_ids.each_with_index{|row_id, i| sort_hash[row_id] = i}
46:       records.sort!{|a, b| sort_hash[a.id] <=> sort_hash[b.id]}
47:     end

[Validate]