Class: ActiveRecord::BatchTouching::State
- Inherits:
-
Object
- Object
- ActiveRecord::BatchTouching::State
- 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
-
#records ⇒ Object
readonly
Return the records grouped by class and columns that were touched:.
Instance Method Summary collapse
- #add_record(record, columns) ⇒ Object
- #clear_records! ⇒ Object
-
#initialize ⇒ State
constructor
A new instance of State.
-
#merge!(other_state) ⇒ Object
Merge another state into this one.
- #more_records? ⇒ Boolean
Constructor Details
#initialize ⇒ State
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
#records ⇒ Object (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
27 28 29 |
# File 'lib/activerecord/batch_touching/state.rb', line 27 def more_records? @records.present? end |