Class: TypeBalancer::BatchProcessing

Inherits:
Object
  • Object
show all
Defined in:
lib/type_balancer/batch_processing.rb

Instance Method Summary collapse

Constructor Details

#initialize(batch_size) ⇒ BatchProcessing

Returns a new instance of BatchProcessing.



5
6
7
# File 'lib/type_balancer/batch_processing.rb', line 5

def initialize(batch_size)
  @batch_size = batch_size
end

Instance Method Details

#create_batches(items_by_type, positions_by_type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/type_balancer/batch_processing.rb', line 9

def create_batches(items_by_type, positions_by_type)
  total_items = items_by_type.values.sum(&:size)
  batches = []
  current_batch = []

  (0...total_items).each do |position|
    type = find_type_for_position(position, positions_by_type)
    current_batch << items_by_type[type].shift if type && !items_by_type[type].empty?

    if current_batch.size >= @batch_size || position == total_items - 1
      batches << current_batch unless current_batch.empty?
      current_batch = []
    end
  end

  batches
end