Module: SeedDump::DumpMethods::Enumeration
- Included in:
- SeedDump::DumpMethods
- Defined in:
- lib/seed_dump/dump_methods/enumeration.rb
Instance Method Summary collapse
- #active_record_enumeration(records, io, options) ⇒ Object
- #batch_params_from(records, options) ⇒ Object
- #batch_size_from(records, options) ⇒ Object
- #enumerable_enumeration(records, io, options) ⇒ Object
Instance Method Details
#active_record_enumeration(records, io, options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/seed_dump/dump_methods/enumeration.rb', line 4 def active_record_enumeration(records, io, ) # If the records don't already have an order, # order them by primary key ascending. if !records.respond_to?(:arel) || records.arel.orders.blank? records.order("#{records.quoted_table_name}.#{records.quoted_primary_key} ASC") end num_of_batches, batch_size, last_batch_size = batch_params_from(records, ) # Loop through each batch (1..num_of_batches).each do |batch_number| record_strings = [] last_batch = (batch_number == num_of_batches) cur_batch_size = if last_batch last_batch_size else batch_size end # Loop through the records of the current batch records.offset((batch_number - 1) * batch_size).limit(cur_batch_size).each do |record| record_strings << dump_record(record, ) end yield record_strings, last_batch end end |
#batch_params_from(records, options) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/seed_dump/dump_methods/enumeration.rb', line 56 def batch_params_from(records, ) batch_size = batch_size_from(records, ) count = records.count remainder = count % batch_size [((count.to_f / batch_size).ceil), batch_size, (remainder == 0 ? batch_size : remainder)] end |
#batch_size_from(records, options) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/seed_dump/dump_methods/enumeration.rb', line 66 def batch_size_from(records, ) if [:batch_size].present? [:batch_size].to_i else 1000 end end |
#enumerable_enumeration(records, io, options) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/seed_dump/dump_methods/enumeration.rb', line 35 def enumerable_enumeration(records, io, ) num_of_batches, batch_size = batch_params_from(records, ) record_strings = [] batch_number = 1 records.each_with_index do |record, i| record_strings << dump_record(record, ) last_batch = (i == records.length - 1) if (record_strings.length == batch_size) || last_batch yield record_strings, last_batch record_strings = [] batch_number += 1 end end end |