Class: Fortschritt::Meter
Instance Attribute Summary collapse
-
#average_seconds ⇒ Object
Returns the value of attribute average_seconds.
-
#done ⇒ Object
Returns the value of attribute done.
-
#silent ⇒ Object
Returns the value of attribute silent.
-
#total ⇒ Object
Returns the value of attribute total.
-
#updated_at ⇒ Object
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #calculate_average_seconds(value) ⇒ Object
- #completed? ⇒ Boolean
- #debounce? ⇒ Boolean
- #increment ⇒ Object
-
#initialize(total, silent: false) ⇒ Meter
constructor
A new instance of Meter.
- #print! ⇒ Object
- #remaining ⇒ Object
- #remaining_seconds ⇒ Object
- #total_elapsed_seconds ⇒ Object
Constructor Details
#initialize(total, silent: false) ⇒ Meter
Returns a new instance of Meter.
5 6 7 8 9 10 11 12 13 |
# File 'lib/fortschritt/meter.rb', line 5 def initialize(total, silent: false) @total = total @done = 0 @updated_at = Time.now @average_seconds = 0 @started_at = Time.now @silent = silent || !!(Rails.env.test? if defined?(Rails)) @printed_at = Time.now end |
Instance Attribute Details
#average_seconds ⇒ Object
Returns the value of attribute average_seconds.
3 4 5 |
# File 'lib/fortschritt/meter.rb', line 3 def average_seconds @average_seconds end |
#done ⇒ Object
Returns the value of attribute done.
3 4 5 |
# File 'lib/fortschritt/meter.rb', line 3 def done @done end |
#silent ⇒ Object
Returns the value of attribute silent.
3 4 5 |
# File 'lib/fortschritt/meter.rb', line 3 def silent @silent end |
#total ⇒ Object
Returns the value of attribute total.
3 4 5 |
# File 'lib/fortschritt/meter.rb', line 3 def total @total end |
#updated_at ⇒ Object
Returns the value of attribute updated_at.
3 4 5 |
# File 'lib/fortschritt/meter.rb', line 3 def updated_at @updated_at end |
Instance Method Details
#calculate_average_seconds(value) ⇒ Object
36 37 38 39 |
# File 'lib/fortschritt/meter.rb', line 36 def calculate_average_seconds(value) average_seconds.zero? and return value ((average_seconds * done) + value) / (done + 1) end |
#completed? ⇒ Boolean
24 25 26 |
# File 'lib/fortschritt/meter.rb', line 24 def completed? done >= total end |
#debounce? ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/fortschritt/meter.rb', line 49 def debounce? return true if @updated_at - @printed_at < 0.01 && !completed? @printed_at = @updated_at false end |
#increment ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/fortschritt/meter.rb', line 15 def increment @_now = Time.now elapsed_seconds = @_now - updated_at @average_seconds = calculate_average_seconds(elapsed_seconds) @updated_at = @_now @done += 1 print! unless @silent || debounce? end |
#print! ⇒ Object
45 46 47 |
# File 'lib/fortschritt/meter.rb', line 45 def print! Fortschritt.printer.print(self) end |
#remaining ⇒ Object
32 33 34 |
# File 'lib/fortschritt/meter.rb', line 32 def remaining [total - done, 0].max end |
#remaining_seconds ⇒ Object
28 29 30 |
# File 'lib/fortschritt/meter.rb', line 28 def remaining_seconds remaining * average_seconds end |
#total_elapsed_seconds ⇒ Object
41 42 43 |
# File 'lib/fortschritt/meter.rb', line 41 def total_elapsed_seconds Time.now - @started_at end |