Class: Philiprehberger::Batch::Result
- Inherits:
-
Object
- Object
- Philiprehberger::Batch::Result
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/batch/result.rb
Overview
Holds the outcome of a batch processing run.
Instance Attribute Summary collapse
-
#chunks ⇒ Integer
readonly
Number of chunks processed.
-
#elapsed ⇒ Float
readonly
Elapsed time in seconds.
-
#errors ⇒ Array<Hash>
readonly
Errors with :item and :error keys.
-
#processed ⇒ Integer
readonly
Number of items processed successfully.
-
#results ⇒ Array
readonly
Results collected from each processed item.
-
#total ⇒ Integer
readonly
Total number of items.
Instance Method Summary collapse
-
#counts ⇒ Hash
Count occurrences of each result value.
-
#each {|item_result| ... } ⇒ Enumerator
Iterate over all collected item results.
-
#flat_map {|item_result| ... } ⇒ Array
Map over all collected item results and flatten one level.
-
#group_by {|item_result| ... } ⇒ Hash
Group results by the return value of the block.
-
#halted? ⇒ Boolean
Check if processing was halted early by an error handler returning :halt.
-
#initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: []) ⇒ Result
constructor
A new instance of Result.
-
#success? ⇒ Boolean
Check if all items were processed without errors.
-
#success_rate ⇒ Float
Ratio of successfully processed items to total items.
Constructor Details
#initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: []) ⇒ Result
Returns a new instance of Result.
34 35 36 37 38 39 40 41 42 |
# File 'lib/philiprehberger/batch/result.rb', line 34 def initialize(processed:, errors:, total:, chunks:, elapsed:, halted: false, results: []) @processed = processed @errors = errors @total = total @chunks = chunks @elapsed = elapsed @halted = halted @results = results end |
Instance Attribute Details
#chunks ⇒ Integer (readonly)
Returns number of chunks processed.
19 20 21 |
# File 'lib/philiprehberger/batch/result.rb', line 19 def chunks @chunks end |
#elapsed ⇒ Float (readonly)
Returns elapsed time in seconds.
22 23 24 |
# File 'lib/philiprehberger/batch/result.rb', line 22 def elapsed @elapsed end |
#errors ⇒ Array<Hash> (readonly)
Returns errors with :item and :error keys.
13 14 15 |
# File 'lib/philiprehberger/batch/result.rb', line 13 def errors @errors end |
#processed ⇒ Integer (readonly)
Returns number of items processed successfully.
10 11 12 |
# File 'lib/philiprehberger/batch/result.rb', line 10 def processed @processed end |
#results ⇒ Array (readonly)
Returns results collected from each processed item.
25 26 27 |
# File 'lib/philiprehberger/batch/result.rb', line 25 def results @results end |
#total ⇒ Integer (readonly)
Returns total number of items.
16 17 18 |
# File 'lib/philiprehberger/batch/result.rb', line 16 def total @total end |
Instance Method Details
#counts ⇒ Hash
Count occurrences of each result value.
77 78 79 |
# File 'lib/philiprehberger/batch/result.rb', line 77 def counts @results.tally end |
#each {|item_result| ... } ⇒ Enumerator
Iterate over all collected item results.
62 63 64 |
# File 'lib/philiprehberger/batch/result.rb', line 62 def each(&) @results.each(&) end |
#flat_map {|item_result| ... } ⇒ Array
Map over all collected item results and flatten one level.
70 71 72 |
# File 'lib/philiprehberger/batch/result.rb', line 70 def flat_map(&) @results.flat_map(&) end |
#group_by {|item_result| ... } ⇒ Hash
Group results by the return value of the block.
85 86 87 |
# File 'lib/philiprehberger/batch/result.rb', line 85 def group_by(&) @results.group_by(&) end |
#halted? ⇒ Boolean
Check if processing was halted early by an error handler returning :halt.
54 55 56 |
# File 'lib/philiprehberger/batch/result.rb', line 54 def halted? @halted end |
#success? ⇒ Boolean
Check if all items were processed without errors.
47 48 49 |
# File 'lib/philiprehberger/batch/result.rb', line 47 def success? @errors.empty? end |
#success_rate ⇒ Float
Ratio of successfully processed items to total items.
Returns 1.0 for empty batches (no items to fail).
94 95 96 97 98 |
# File 'lib/philiprehberger/batch/result.rb', line 94 def success_rate return 1.0 if @total.zero? @processed.to_f / @total end |