Class: ASIR::AdaptiveValue

Inherits:
Object
  • Object
show all
Defined in:
lib/asir/adaptive_value.rb

Overview

Adaptive Value. Return a Numeric #value which adapts. Useful for controlling adaptive retry sleep amounts or other values that must be stochastic.

#init must be specified. #rand_factor should be a Float.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ AdaptiveValue

Returns a new instance of AdaptiveValue.



13
14
15
16
17
18
19
# File 'lib/asir/adaptive_value.rb', line 13

def initialize opts = nil
  if opts
    opts.each do | k, v |
      send(:"#{k}=", v)
    end
  end
end

Instance Attribute Details

#addObject

Returns the value of attribute add.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def add
  @add
end

#initObject

Returns the value of attribute init.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def init
  @init
end

#maxObject

Returns the value of attribute max.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def max
  @max
end

#minObject

Returns the value of attribute min.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def min
  @min
end

#multObject

Returns the value of attribute mult.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def mult
  @mult
end

#rand_factorObject

Returns the value of attribute rand_factor.



11
12
13
# File 'lib/asir/adaptive_value.rb', line 11

def rand_factor
  @rand_factor
end

Instance Method Details

#adapt!Object

Increments value by #add, if #add is set. Multiplies value by #mult, if #mult is set. Limits value by #min and #max.



65
66
67
68
69
70
71
72
# File 'lib/asir/adaptive_value.rb', line 65

def adapt!
  @value ||= init_or_error
  @value += @add if @add
  @value *= @mult if @mult
  @value = @min if @min && @value < @min
  @value = @max if @max && @value > @max
  self
end

#new_value!Object

Returns a new cached #value.



39
40
41
42
# File 'lib/asir/adaptive_value.rb', line 39

def new_value!
  @value_ = nil
  value!
end

#reset!Object

Resets #value to #init.



45
46
47
48
# File 'lib/asir/adaptive_value.rb', line 45

def reset!
  @value_ = @value = nil
  self
end

#to_fObject



56
57
58
59
60
# File 'lib/asir/adaptive_value.rb', line 56

def to_f
  x = value.to_f
  adapt!
  x
end

#to_iObject



50
51
52
53
54
# File 'lib/asir/adaptive_value.rb', line 50

def to_i
  x = value.to_i
  adapt!
  x
end

#valueObject

Returns a new value limited by #min and #max after applying the addition of #rand_factor.



22
23
24
25
26
27
28
# File 'lib/asir/adaptive_value.rb', line 22

def value
  v = @value || init_or_error
  v += v * rand(@rand_factor) if @rand_factor
  v = @min if @min && v < @min
  v = @max if @max && v > @max
  v
end

#value!Object

Returns a cached #value until #reset! or #new_value!.



34
35
36
# File 'lib/asir/adaptive_value.rb', line 34

def value!
  @value_ ||= value
end

#value=(x) ⇒ Object



29
30
31
# File 'lib/asir/adaptive_value.rb', line 29

def value= x
  @value = x
end