Class: AbstractImporter::Strategies::InsertStrategy
- Defined in:
- lib/abstract_importer/strategies/insert_strategy.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(collection) ⇒ InsertStrategy
constructor
A new instance of InsertStrategy.
- #process_record(hash) ⇒ Object
Methods inherited from Base
#already_imported?, #prepare_attributes
Constructor Details
#initialize(collection) ⇒ InsertStrategy
Returns a new instance of InsertStrategy.
8 9 10 11 12 |
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 8 def initialize(collection) super @batch = [] @batch_size = 250 end |
Instance Method Details
#flush ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 38 def flush invoke_callback(:before_batch, @batch) begin tries = (tries || 0) + 1 collection.scope.insert_many(@batch) rescue raise if tries > 1 invoke_callback(:rescue_batch, @batch) retry end ids = collection.scope.where(legacy_id: @batch.map { |hash| hash[:legacy_id] }) id_map.merge! collection.table_name, ids summary.created += ids.length @batch = [] end |
#process_record(hash) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/abstract_importer/strategies/insert_strategy.rb', line 15 def process_record(hash) summary.total += 1 if already_imported?(hash) summary.already_imported += 1 return end remap_foreign_keys!(hash) if redundant_record?(hash) summary.redundant += 1 return end @batch << prepare_attributes(hash) flush if @batch.length >= @batch_size rescue ::AbstractImporter::Skip summary.skipped += 1 end |