class ActiveRecord::DelayTouching::State

Tracking of the touch state. This class has no class-level data, so you can store per-thread instances in thread-local variables.

Attributes

nesting[RW]

Public Class Methods

new() click to toggle source
# File lib/activerecord/delay_touching/state.rb, line 11
def initialize
  @records = Hash.new { Set.new }
  @already_updated_records = Hash.new { Set.new }
  @nesting = 0
end

Public Instance Methods

add_record(record, *columns) click to toggle source
# File lib/activerecord/delay_touching/state.rb, line 45
def add_record(record, *columns)
  columns << nil if columns.empty? #if no arguments are passed, we will use nil to infer default column
  columns.each do |column|
    @records[column] += [ record ] unless @already_updated_records[column].include?(record)
  end
end
clear_records() click to toggle source
# File lib/activerecord/delay_touching/state.rb, line 52
def clear_records
  @records.clear
  @already_updated_records.clear
end
more_records?() click to toggle source

There are more records as long as there is at least one record that is persisted

# File lib/activerecord/delay_touching/state.rb, line 37
def more_records?
  @records.each do |_, set|
    set.each { |record| return true if record.persisted? } # will shortcut on first persisted record found
  end

  false # no persisted records found, so no more records to process
end
records_by_attrs_and_class() click to toggle source

Return the records grouped by the attributes that were touched, and by class: [

[
  nil, { Person => [ person1, person2 ], Pet => [ pet1 ] }
],
[
  :neutered_at, { Pet => [ pet1 ] }
],

]

# File lib/activerecord/delay_touching/state.rb, line 32
def records_by_attrs_and_class
  @records.map { |attrs, records| [attrs, records.group_by(&:class)] }
end
updated(attr, records) click to toggle source
# File lib/activerecord/delay_touching/state.rb, line 17
def updated(attr, records)
  @records[attr].subtract records
  @records.delete attr if @records[attr].empty?
  @already_updated_records[attr] += records
end