Class: Fortschritt::Meter

Inherits:
Object show all
Defined in:
lib/fortschritt/meter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_secondsObject

Returns the value of attribute average_seconds.



3
4
5
# File 'lib/fortschritt/meter.rb', line 3

def average_seconds
  @average_seconds
end

#doneObject

Returns the value of attribute done.



3
4
5
# File 'lib/fortschritt/meter.rb', line 3

def done
  @done
end

#silentObject

Returns the value of attribute silent.



3
4
5
# File 'lib/fortschritt/meter.rb', line 3

def silent
  @silent
end

#totalObject

Returns the value of attribute total.



3
4
5
# File 'lib/fortschritt/meter.rb', line 3

def total
  @total
end

#updated_atObject

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/fortschritt/meter.rb', line 24

def completed?
  done >= total
end

#debounce?Boolean

Returns:

  • (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

#incrementObject



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

#remainingObject



32
33
34
# File 'lib/fortschritt/meter.rb', line 32

def remaining
  [total - done, 0].max
end

#remaining_secondsObject



28
29
30
# File 'lib/fortschritt/meter.rb', line 28

def remaining_seconds
  remaining * average_seconds
end

#total_elapsed_secondsObject



41
42
43
# File 'lib/fortschritt/meter.rb', line 41

def total_elapsed_seconds
  Time.now - @started_at
end