Class: Hedra::ProgressTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/hedra/progress_tracker.rb

Overview

Track and display progress for batch operations

Instance Method Summary collapse

Constructor Details

#initialize(total, quiet: false) ⇒ ProgressTracker

Returns a new instance of ProgressTracker.



6
7
8
9
10
11
12
# File 'lib/hedra/progress_tracker.rb', line 6

def initialize(total, quiet: false)
  @total = total
  @current = 0
  @quiet = quiet
  @start_time = Time.now
  @mutex = Mutex.new
end

Instance Method Details

#finishObject



21
22
23
24
25
26
27
# File 'lib/hedra/progress_tracker.rb', line 21

def finish
  return if @quiet

  puts "\n"
  elapsed = Time.now - @start_time
  puts "Completed #{@total} items in #{elapsed.round(2)}s"
end

#incrementObject



14
15
16
17
18
19
# File 'lib/hedra/progress_tracker.rb', line 14

def increment
  @mutex.synchronize do
    @current += 1
    update_display unless @quiet
  end
end