Class: Infobar::Counter
- Extended by:
- Tins::Delegate
- Defined in:
- lib/infobar/counter.rb
Instance Attribute Summary collapse
-
#current ⇒ Object
readonly
Returns the value of attribute current.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
Instance Method Summary collapse
- #done? ⇒ Boolean
- #eta ⇒ Object
- #finish ⇒ Object
- #finished? ⇒ Boolean
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
- #progress(by: 1) ⇒ Object
- #progressed ⇒ Object
- #reset(total: 0, current: 0) ⇒ Object
- #started? ⇒ Boolean
- #time_elapsed ⇒ Object
- #time_remaining ⇒ Object
- #to_go ⇒ Object
- #to_go? ⇒ Boolean
- #total_time ⇒ Object
Constructor Details
#initialize ⇒ Counter
Returns a new instance of Counter.
8 9 10 |
# File 'lib/infobar/counter.rb', line 8 def initialize reset end |
Instance Attribute Details
#current ⇒ Object (readonly)
Returns the value of attribute current.
12 13 14 |
# File 'lib/infobar/counter.rb', line 12 def current @current end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
14 15 16 |
# File 'lib/infobar/counter.rb', line 14 def total @total end |
Instance Method Details
#done? ⇒ Boolean
53 54 55 |
# File 'lib/infobar/counter.rb', line 53 def done? @current >= @total end |
#eta ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/infobar/counter.rb', line 73 def eta if @finished @finished else Time.now + time_remaining end end |
#finish ⇒ Object
38 39 40 41 |
# File 'lib/infobar/counter.rb', line 38 def finish @finished = Time.now self end |
#finished? ⇒ Boolean
34 35 36 |
# File 'lib/infobar/counter.rb', line 34 def finished? @finished end |
#progress(by: 1) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/infobar/counter.rb', line 43 def progress(by: 1) if !finished? && by >= 1 now = Time.now @start ||= now @timer.add(now, by) @current += by end self end |
#progressed ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/infobar/counter.rb', line 96 def progressed if @total.zero? 0.0 else @current / @total.to_f end end |
#reset(total: 0, current: 0) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/infobar/counter.rb', line 16 def reset(total: 0, current: 0) @current = current @total = total @start = nil @finished = false @timer = Infobar::Timer.new end |
#started? ⇒ Boolean
30 31 32 |
# File 'lib/infobar/counter.rb', line 30 def started? @start end |
#time_elapsed ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/infobar/counter.rb', line 81 def time_elapsed case when @finished && @start @finished - @start when @start Time.now - @start else 0.0 end end |
#time_remaining ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/infobar/counter.rb', line 65 def time_remaining if @finished 0.0 else average_time * to_go end end |
#to_go ⇒ Object
57 58 59 |
# File 'lib/infobar/counter.rb', line 57 def to_go [ total - current, 0 ].max end |
#to_go? ⇒ Boolean
61 62 63 |
# File 'lib/infobar/counter.rb', line 61 def to_go? to_go.nonzero? end |
#total_time ⇒ Object
92 93 94 |
# File 'lib/infobar/counter.rb', line 92 def total_time time_elapsed + time_remaining end |