Class: Infobar::Counter

Inherits:
Object show all
Defined in:
lib/infobar/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



8
9
10
# File 'lib/infobar/counter.rb', line 8

def initialize
  reset
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



12
13
14
# File 'lib/infobar/counter.rb', line 12

def current
  @current
end

#totalObject (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

Returns:

  • (Boolean)


51
52
53
# File 'lib/infobar/counter.rb', line 51

def done?
  @current >= @total
end

#etaObject



71
72
73
74
75
76
77
# File 'lib/infobar/counter.rb', line 71

def eta
  if @finished
    @finished
  else
    Time.now + time_remaining
  end
end

#finishObject



36
37
38
39
# File 'lib/infobar/counter.rb', line 36

def finish
  @finished = Time.now
  self
end

#finished?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/infobar/counter.rb', line 32

def finished?
  @finished
end

#progress(by: 1) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/infobar/counter.rb', line 41

def progress(by: 1)
  if !finished? && by >= 1
    now = Time.now
    @start ||= now
    @timer.add(now, by)
    @current += by
  end
  self
end

#progressedObject



94
95
96
97
98
99
100
# File 'lib/infobar/counter.rb', line 94

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

Returns:

  • (Boolean)


28
29
30
# File 'lib/infobar/counter.rb', line 28

def started?
  @start
end

#time_elapsedObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/infobar/counter.rb', line 79

def time_elapsed
  case
  when @finished && @start
    @finished - @start
  when @start
    Time.now - @start
  else
    0.0
  end
end

#time_remainingObject



63
64
65
66
67
68
69
# File 'lib/infobar/counter.rb', line 63

def time_remaining
  if @finished
    0.0
  else
    average_time * to_go
  end
end

#to_goObject



55
56
57
# File 'lib/infobar/counter.rb', line 55

def to_go
  [ total - current, 0 ].max
end

#to_go?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/infobar/counter.rb', line 59

def to_go?
  to_go.nonzero?
end

#total_timeObject



90
91
92
# File 'lib/infobar/counter.rb', line 90

def total_time
  time_elapsed + time_remaining
end