Class: LibGems::StreamUI::VerboseDownloadReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/libgems/user_interaction.rb

Overview

A progress reporter that prints out messages about the current progress.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(out_stream, *args) ⇒ VerboseDownloadReporter

Returns a new instance of VerboseDownloadReporter.



450
451
452
453
# File 'lib/libgems/user_interaction.rb', line 450

def initialize(out_stream, *args)
  @out = out_stream
  @progress = 0
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



448
449
450
# File 'lib/libgems/user_interaction.rb', line 448

def file_name
  @file_name
end

#progressObject (readonly)

Returns the value of attribute progress.



448
449
450
# File 'lib/libgems/user_interaction.rb', line 448

def progress
  @progress
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



448
449
450
# File 'lib/libgems/user_interaction.rb', line 448

def total_bytes
  @total_bytes
end

Instance Method Details

#doneObject



476
477
478
479
# File 'lib/libgems/user_interaction.rb', line 476

def done
  @progress = 100 if @units == '%'
  update_display(true, true)
end

#fetch(file_name, total_bytes) ⇒ Object



455
456
457
458
459
460
461
# File 'lib/libgems/user_interaction.rb', line 455

def fetch(file_name, total_bytes)
  @file_name = file_name
  @total_bytes = total_bytes.to_i
  @units = @total_bytes.zero? ? 'B' : '%'

  update_display(false)
end

#update(bytes) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/libgems/user_interaction.rb', line 463

def update(bytes)
  new_progress = if @units == 'B' then
                   bytes
                 else
                   ((bytes.to_f * 100) / total_bytes.to_f).ceil
                 end

  return if new_progress == @progress

  @progress = new_progress
  update_display
end