Class: Translatomatic::StringBatcher

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/string_batcher.rb

Overview

each_slice with a limit on number of strings and also an optional character limit.

Instance Method Summary collapse

Constructor Details

#initialize(strings, max_count:, max_length:) ⇒ StringBatcher

Returns a new instance of StringBatcher.

Parameters:

  • strings (Array<String>)

    A list of strings to return in batches

  • max_count (Number)

    The maximum number of strings to return

  • max_length (Number)

    The maximum total length of strings to return



10
11
12
13
14
15
16
# File 'lib/translatomatic/string_batcher.rb', line 10

def initialize(strings, max_count:, max_length:)
  @strings = strings
  @max_count = max_count
  @max_length = max_length
  @batch = []
  @length = 0
end

Instance Method Details

#each_batchArray<String>

Yields lists of strings within the size constraints given to the constructor.

Returns:

  • (Array<String>)

    List of strings



21
22
23
24
25
26
# File 'lib/translatomatic/string_batcher.rb', line 21

def each_batch
  @strings.each do |string|
    process_string(string) { |batch| yield batch }
  end
  yield_batch { |batch| yield batch } # send remaining strings
end