Class: Riak::Client::Decaying

Inherits:
Object
  • Object
show all
Defined in:
lib/riak/client/decaying.rb

Overview

A float value which decays exponentially toward 0 over time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Decaying

Returns a new instance of Decaying.

Parameters:

  • opts (Hash) (defaults to: {})

    options

  • options (Hash)

    a customizable set of options



14
15
16
17
18
19
# File 'lib/riak/client/decaying.rb', line 14

def initialize(opts = {})
  @p = opts[:p] || 0.0
  @e = opts[:e] || Math::E
  @r = opts[:r] || Math.log(0.5) / 10
  @t0 = Time.now
end

Instance Attribute Details

#eObject

Returns the value of attribute e.



6
7
8
# File 'lib/riak/client/decaying.rb', line 6

def e
  @e
end

#pObject

Returns the value of attribute p.



7
8
9
# File 'lib/riak/client/decaying.rb', line 7

def p
  @p
end

Instance Method Details

#<<(d) ⇒ Object

Add to current value.

Parameters:

  • d (Float)

    the value to add



23
24
25
# File 'lib/riak/client/decaying.rb', line 23

def <<(d)
  @p = value + d
end

#valueFloat

Returns the current value (adjusted for the time decay).

Returns:

  • (Float)

    the current value (adjusted for the time decay)



28
29
30
31
32
33
# File 'lib/riak/client/decaying.rb', line 28

def value
  now = Time.now
  dt = now - @t0
  @t0 = now
  @p = @p * (@e ** (@r * dt))
end