Module: SeedDumpling::DumpMethods::Enumeration
- Included in:
- SeedDumpling::DumpMethods
- Defined in:
- lib/seed_dumpling/dump_methods/enumeration.rb
Overview
Helpermethods for enumerating active records
Instance Method Summary collapse
-
#active_record_enumeration(records, options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
- #batch_params_from(records, options) ⇒ Object
- #batch_size_from(options) ⇒ Object
- #enumerable_enumeration(records, options) ⇒ Object
Instance Method Details
#active_record_enumeration(records, options) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/seed_dumpling/dump_methods/enumeration.rb', line 9 def active_record_enumeration(records, ) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength # Ensure records are ordered by primary key if not already ordered unless records.respond_to?(:arel) && records.arel.orders.present? records = records.order("#{records.quoted_table_name}.#{records.quoted_primary_key} ASC") end num_of_batches, batch_size, last_batch_size = batch_params_from(records, ) # Iterate over each batch (1..num_of_batches).each do |batch_number| last_batch = (batch_number == num_of_batches) current_batch_size = last_batch ? last_batch_size : batch_size # Fetch and process records for the current batch record_strings = records .offset((batch_number - 1) * batch_size) .limit(current_batch_size) .map { |record| dump_record(record, ) } yield record_strings, last_batch end end |
#batch_params_from(records, options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/seed_dumpling/dump_methods/enumeration.rb', line 47 def batch_params_from(records, ) batch_size = batch_size_from() total_count = records.count num_of_batches = (total_count.to_f / batch_size).ceil remainder = total_count % batch_size last_batch_size = remainder.zero? ? batch_size : remainder [num_of_batches, batch_size, last_batch_size] end |
#batch_size_from(options) ⇒ Object
58 59 60 |
# File 'lib/seed_dumpling/dump_methods/enumeration.rb', line 58 def batch_size_from() [:batch_size]&.to_i || 1000 end |
#enumerable_enumeration(records, options) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/seed_dumpling/dump_methods/enumeration.rb', line 32 def enumerable_enumeration(records, ) _, batch_size = batch_params_from(records, ) record_strings = [] records.each_with_index do |record, index| record_strings << dump_record(record, ) last_batch = (index == records.size - 1) if record_strings.size == batch_size || last_batch yield record_strings, last_batch record_strings = [] end end end |