Module: ProgressBar::Components::Progressable

Included in:
Bar, EstimatedTimer, Rate
Defined in:
lib/ruby-progressbar/components/progressable.rb

Constant Summary collapse

DEFAULT_TOTAL =
100
DEFAULT_BEGINNING_POSITION =
0
DEFAULT_SMOOTHING =
0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#autofinishObject

Returns the value of attribute autofinish.



15
16
17
# File 'lib/ruby-progressbar/components/progressable.rb', line 15

def autofinish
  @autofinish
end

#finishedObject

Returns the value of attribute finished.



16
17
18
# File 'lib/ruby-progressbar/components/progressable.rb', line 16

def finished
  @finished
end

#progressObject

Returns the value of attribute progress.



11
12
13
# File 'lib/ruby-progressbar/components/progressable.rb', line 11

def progress
  @progress
end

#running_averageObject

Returns the value of attribute running_average.



13
14
15
# File 'lib/ruby-progressbar/components/progressable.rb', line 13

def running_average
  @running_average
end

#smoothingObject

Returns the value of attribute smoothing.



14
15
16
# File 'lib/ruby-progressbar/components/progressable.rb', line 14

def smoothing
  @smoothing
end

#starting_positionObject

Returns the value of attribute starting_position.



12
13
14
# File 'lib/ruby-progressbar/components/progressable.rb', line 12

def starting_position
  @starting_position
end

#totalObject

Returns the value of attribute total.



10
11
12
# File 'lib/ruby-progressbar/components/progressable.rb', line 10

def total
  @total
end

Instance Method Details

#decrementObject



47
48
49
50
51
# File 'lib/ruby-progressbar/components/progressable.rb', line 47

def decrement
  warn "WARNING: Your progress bar is currently at #{progress} out of #{total} and cannot be decremented. In v2.0.0 this will become a ProgressBar::InvalidProgressError." if progress == 0

  self.progress -= 1 unless progress == 0
end

#finishObject



70
71
72
73
# File 'lib/ruby-progressbar/components/progressable.rb', line 70

def finish
  self.finished = true
  self.progress = total
end

#finished?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ruby-progressbar/components/progressable.rb', line 37

def finished?
  finished || (autofinish && progress == total)
end

#incrementObject



41
42
43
44
45
# File 'lib/ruby-progressbar/components/progressable.rb', line 41

def increment
  warn "WARNING: Your progress bar is currently at #{progress} out of #{total} and cannot be incremented. In v2.0.0 this will become a ProgressBar::InvalidProgressError." if progress == total

  self.progress += 1 unless progress == total
end

#initialize(options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/ruby-progressbar/components/progressable.rb', line 18

def initialize(options = {})
  self.total           = options.fetch(:total, DEFAULT_TOTAL)
  self.smoothing       = options[:smoothing] || DEFAULT_SMOOTHING
  self.autofinish      = options.fetch(:autofinish, true)

  start :at => DEFAULT_BEGINNING_POSITION
end

#percentage_completedObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby-progressbar/components/progressable.rb', line 75

def percentage_completed
  return 100 if total == 0
  return 0   if total.nil?

  # progress / total * 100
  #
  # Doing this way so we can avoid converting each
  # number to a float and then back to an integer.
  #
  (self.progress * 100 / total).to_i
end

#percentage_completed_with_precisionObject



87
88
89
90
91
92
# File 'lib/ruby-progressbar/components/progressable.rb', line 87

def percentage_completed_with_precision
  return 100.0  if total == 0
  return 0.0    if total.nil?

  format('%5.2f', (progress.to_f * 100.0 / total * 100.0).floor / 100.0)
end

#progress_madeObject



94
95
96
# File 'lib/ruby-progressbar/components/progressable.rb', line 94

def progress_made
  started? ? self.progress - self.starting_position : 0
end

#resetObject



53
54
55
# File 'lib/ruby-progressbar/components/progressable.rb', line 53

def reset
  start :at => self.starting_position
end

#start(options = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/ruby-progressbar/components/progressable.rb', line 26

def start(options = {})
  self.finished          = false
  self.running_average   = 0
  self.progress          = \
  self.starting_position = options[:at] || self.progress
end

#started?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ruby-progressbar/components/progressable.rb', line 33

def started?
  !!self.starting_position
end