Class: ProgressBar::Projectors::SmoothedAverage

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-progressbar/projectors/smoothed_average.rb

Constant Summary collapse

DEFAULT_STRENGTH =
0.1
DEFAULT_BEGINNING_POSITION =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ SmoothedAverage

Returns a new instance of SmoothedAverage.



11
12
13
14
15
16
17
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 11

def initialize(options = {})
  self.samples    = []
  self.projection = 0.0
  self.strength   = options[:strength] || DEFAULT_STRENGTH

  start(:at => DEFAULT_BEGINNING_POSITION)
end

Instance Attribute Details

#projectionObject

Returns the value of attribute projection.



9
10
11
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 9

def projection
  @projection
end

#samplesObject

Returns the value of attribute samples.



7
8
9
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 7

def samples
  @samples
end

#strengthObject

Returns the value of attribute strength.



7
8
9
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 7

def strength
  @strength
end

Class Method Details

.calculate(current_projection, new_value, rate) ⇒ Object



56
57
58
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 56

def self.calculate(current_projection, new_value, rate)
  (new_value * (1.0 - rate)) + (current_projection * rate)
end

Instance Method Details

#decrementObject



24
25
26
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 24

def decrement
  self.progress -= 1
end

#incrementObject



28
29
30
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 28

def increment
  self.progress += 1
end

#none?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 52

def none?
  projection.zero?
end

#progressObject



32
33
34
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 32

def progress
  samples[1]
end

#progress=(new_progress) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 42

def progress=(new_progress)
  samples[1] = new_progress
  self.projection = \
    self.class.calculate(
      @projection,
      absolute,
      strength
    )
end

#resetObject



38
39
40
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 38

def reset
  start(:at => samples[0])
end

#start(options = {}) ⇒ Object



19
20
21
22
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 19

def start(options = {})
  self.projection = 0.0
  self.progress   = samples[0] = (options[:at] || progress)
end

#total=(_new_total) ⇒ Object



36
# File 'lib/ruby-progressbar/projectors/smoothed_average.rb', line 36

def total=(_new_total); end