Class: ASIR::AdaptiveValue
- Inherits:
-
Object
- Object
- ASIR::AdaptiveValue
- 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
-
#add ⇒ Object
Returns the value of attribute add.
-
#init ⇒ Object
Returns the value of attribute init.
-
#max ⇒ Object
Returns the value of attribute max.
-
#min ⇒ Object
Returns the value of attribute min.
-
#mult ⇒ Object
Returns the value of attribute mult.
-
#rand_factor ⇒ Object
Returns the value of attribute rand_factor.
Instance Method Summary collapse
-
#adapt! ⇒ Object
Increments value by #add, if #add is set.
-
#initialize(opts = nil) ⇒ AdaptiveValue
constructor
A new instance of AdaptiveValue.
-
#new_value! ⇒ Object
Returns a new cached #value.
-
#reset! ⇒ Object
Resets #value to #init.
- #to_f ⇒ Object
- #to_i ⇒ Object
-
#value ⇒ Object
Returns a new value limited by #min and #max after applying the addition of #rand_factor.
-
#value! ⇒ Object
Returns a cached #value until #reset! or #new_value!.
- #value=(x) ⇒ Object
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
#add ⇒ Object
Returns the value of attribute add.
11 12 13 |
# File 'lib/asir/adaptive_value.rb', line 11 def add @add end |
#init ⇒ Object
Returns the value of attribute init.
11 12 13 |
# File 'lib/asir/adaptive_value.rb', line 11 def init @init end |
#max ⇒ Object
Returns the value of attribute max.
11 12 13 |
# File 'lib/asir/adaptive_value.rb', line 11 def max @max end |
#min ⇒ Object
Returns the value of attribute min.
11 12 13 |
# File 'lib/asir/adaptive_value.rb', line 11 def min @min end |
#mult ⇒ Object
Returns the value of attribute mult.
11 12 13 |
# File 'lib/asir/adaptive_value.rb', line 11 def mult @mult end |
#rand_factor ⇒ Object
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_f ⇒ Object
56 57 58 59 60 |
# File 'lib/asir/adaptive_value.rb', line 56 def to_f x = value.to_f adapt! x end |
#to_i ⇒ Object
50 51 52 53 54 |
# File 'lib/asir/adaptive_value.rb', line 50 def to_i x = value.to_i adapt! x end |
#value ⇒ Object
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 |