Class: ActiveRecord::BatchTouching::State

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/batch_touching/state.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



19
20
21
# File 'lib/activerecord/batch_touching/state.rb', line 19

def initialize
  @records = Hash.new { Set.new }
end

Instance Attribute Details

#recordsObject (readonly)

Return the records grouped by class and columns that were touched:

{
  [Owner, [:updated_at]]               => Set.new([owner1, owner2]),
  [Pet,   [:neutered_at, :updated_at]] => Set.new([pet1]),
  [Pet,   [:updated_at]]               => Set.new([pet2])
}


17
18
19
# File 'lib/activerecord/batch_touching/state.rb', line 17

def records
  @records
end

Instance Method Details

#add_record(record, columns) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/activerecord/batch_touching/state.rb', line 31

def add_record(record, columns)
  # Include the standard updated_at column and any additional specified columns
  columns += record.send(:timestamp_attributes_for_update_in_model)
  columns = columns.map(&:to_sym).sort

  key = [record.class, columns]
  @records[key] += [record]
end

#clear_records!Object



23
24
25
# File 'lib/activerecord/batch_touching/state.rb', line 23

def clear_records!
  @records = Hash.new { Set.new }
end

#merge!(other_state) ⇒ Object

Merge another state into this one



41
42
43
# File 'lib/activerecord/batch_touching/state.rb', line 41

def merge!(other_state)
  merge_records!(@records, other_state.records)
end

#more_records?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/activerecord/batch_touching/state.rb', line 27

def more_records?
  @records.present?
end