Class: ActiveRecordDataLoader::ActiveRecord::UniqueIndexTracker
- Inherits:
-
Object
- Object
- ActiveRecordDataLoader::ActiveRecord::UniqueIndexTracker
- Defined in:
- lib/active_record_data_loader/active_record/unique_index_tracker.rb
Defined Under Namespace
Classes: Index
Instance Method Summary collapse
- #capture_unique_values(row) ⇒ Object
- #contained_in_index?(ar_column) ⇒ Boolean
-
#initialize(model:, connection_factory:) ⇒ UniqueIndexTracker
constructor
A new instance of UniqueIndexTracker.
- #map_indexed_columns(column_list) ⇒ Object
- #repeating_unique_values?(row) ⇒ Boolean
Constructor Details
#initialize(model:, connection_factory:) ⇒ UniqueIndexTracker
Returns a new instance of UniqueIndexTracker.
8 9 10 11 12 13 14 |
# File 'lib/active_record_data_loader/active_record/unique_index_tracker.rb', line 8 def initialize(model:, connection_factory:) @model = model @table = model.table_name @unique_indexes = [] @unique_values_used = {} find_unique_indexes(connection_factory) end |
Instance Method Details
#capture_unique_values(row) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/active_record_data_loader/active_record/unique_index_tracker.rb', line 35 def capture_unique_values(row) return unless row.present? @unique_indexes.each do |index| values = index.column_indexes.map { |i| row[i] } @unique_values_used.fetch(index.name) << values end row end |
#contained_in_index?(ar_column) ⇒ Boolean
45 46 47 48 49 50 51 52 53 |
# File 'lib/active_record_data_loader/active_record/unique_index_tracker.rb', line 45 def contained_in_index?(ar_column) target_column = if @model.reflect_on_association(ar_column.name)&.belongs_to? ar_column.join_foreign_key.to_sym else ar_column.name.to_sym end @raw_unique_indexes.flat_map { |i| i.columns.map(&:to_sym) }.include?(target_column) end |
#map_indexed_columns(column_list) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_record_data_loader/active_record/unique_index_tracker.rb', line 16 def map_indexed_columns(column_list) @unique_indexes = @raw_unique_indexes.map do |index| @unique_values_used[index.name] = Set.new columns = index.columns.map(&:to_sym) Index.new( name: index.name, columns: columns, column_indexes: columns.map { |c| column_list.find_index(c) } ) end end |
#repeating_unique_values?(row) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/active_record_data_loader/active_record/unique_index_tracker.rb', line 28 def repeating_unique_values?(row) @unique_indexes.map do |index| values = index.column_indexes.map { |i| row[i] } @unique_values_used.fetch(index.name).include?(values) end.any? end |