Class: Traductor::BatchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/traductor/batch_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(flat_keys, batch_size:) ⇒ BatchBuilder

Returns a new instance of BatchBuilder.



5
6
7
8
# File 'lib/traductor/batch_builder.rb', line 5

def initialize(flat_keys, batch_size:)
  @flat_keys = flat_keys
  @batch_size = batch_size
end

Instance Method Details

#build ⇒ Object

Groups keys by their top-level namespace, then splits into batches of at most batch_size entries.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/traductor/batch_builder.rb', line 12

def build
  grouped = group_by_namespace
  batches = []

  grouped.each_value do |keys_hash|
    keys_hash.each_slice(@batch_size) do |slice|
      batches << slice.to_h
    end
  end

  batches.reject(&:empty?)
end