Module: ProgressBar::Components::Progressable

Included in:
Bar, EstimatedTimer
Defined in:
lib/progress_bar/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

#progressObject

Returns the value of attribute progress.



9
10
11
# File 'lib/progress_bar/components/progressable.rb', line 9

def progress
  @progress
end

#running_averageObject

Returns the value of attribute running_average.



11
12
13
# File 'lib/progress_bar/components/progressable.rb', line 11

def running_average
  @running_average
end

#smoothingObject

Returns the value of attribute smoothing.



12
13
14
# File 'lib/progress_bar/components/progressable.rb', line 12

def smoothing
  @smoothing
end

#starting_positionObject

Returns the value of attribute starting_position.



10
11
12
# File 'lib/progress_bar/components/progressable.rb', line 10

def starting_position
  @starting_position
end

#totalObject

Returns the value of attribute total.



8
9
10
# File 'lib/progress_bar/components/progressable.rb', line 8

def total
  @total
end

Instance Method Details

#decrementObject



36
37
38
# File 'lib/progress_bar/components/progressable.rb', line 36

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

#finishObject



57
58
59
# File 'lib/progress_bar/components/progressable.rb', line 57

def finish
  self.progress = self.total
end

#incrementObject



32
33
34
# File 'lib/progress_bar/components/progressable.rb', line 32

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

#initialize(options = {}) ⇒ Object



14
15
16
17
18
19
# File 'lib/progress_bar/components/progressable.rb', line 14

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

  start :at => DEFAULT_BEGINNING_POSITION
end

#percentage_completedObject



61
62
63
64
65
66
67
68
# File 'lib/progress_bar/components/progressable.rb', line 61

def percentage_completed
  # 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
end

#percentage_completed_with_precisionObject



70
71
72
# File 'lib/progress_bar/components/progressable.rb', line 70

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

#resetObject



40
41
42
# File 'lib/progress_bar/components/progressable.rb', line 40

def reset
  start :at => self.starting_position
end

#start(options = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/progress_bar/components/progressable.rb', line 21

def start(options = {})
  self.running_average   = 0

  self.progress          = \
  self.starting_position = options[:at] || self.progress
end

#started?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/progress_bar/components/progressable.rb', line 28

def started?
  !!self.starting_position
end