Class: Text::Gen::ResultAccumulator
- Inherits:
-
Object
- Object
- Text::Gen::ResultAccumulator
- Defined in:
- lib/text/gen/result_accumulator.rb
Overview
ResultAccumulator handles accumulating results with attempt tracking
Class Method Summary collapse
Instance Method Summary collapse
- #accumulate(&block) ⇒ Object
-
#initialize(unique:, count:, max_attempts:) ⇒ ResultAccumulator
constructor
A new instance of ResultAccumulator.
Constructor Details
#initialize(unique:, count:, max_attempts:) ⇒ ResultAccumulator
Returns a new instance of ResultAccumulator.
13 14 15 16 17 18 19 |
# File 'lib/text/gen/result_accumulator.rb', line 13 def initialize(unique:, count:, max_attempts:) @unique = unique @count = [count, 1].max @max_attempts = [max_attempts, 1].max @results = unique ? {} : [] @attempts = max_attempts * count end |
Class Method Details
.accumulate(unique:, count:, max_attempts:, &block) ⇒ Object
8 9 10 |
# File 'lib/text/gen/result_accumulator.rb', line 8 def accumulate(unique:, count:, max_attempts:, &block) new(unique: unique, count: count, max_attempts: max_attempts).accumulate(&block) end |
Instance Method Details
#accumulate(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/text/gen/result_accumulator.rb', line 21 def accumulate(&block) while @results.size < @count result = block.call add_result(result) if result @attempts -= 1 raise MaxAttemptsError if @attempts.zero? end @results.map { |_, v| v } end |