Class: RubyOnAcid::FlashFactory

Inherits:
Factory
  • Object
show all
Defined in:
lib/rubyonacid/factories/flash.rb

Overview

Returns 0.0 for a given number of queries, then 1.0 for the same number of queries, then goes back to 0.0 and repeats.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(interval = 3) ⇒ FlashFactory

Returns a new instance of FlashFactory.



11
12
13
14
15
16
# File 'lib/rubyonacid/factories/flash.rb', line 11

def initialize(interval = 3)
  super
  @counters = {}
  @values = {}
  @interval = interval
end

Instance Attribute Details

#intervalObject

The number of times to return a value before switching.



9
10
11
# File 'lib/rubyonacid/factories/flash.rb', line 9

def interval
  @interval
end

Instance Method Details

#get_unit(key) ⇒ Object

If key is over threshold, flip to other value and reset counter.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubyonacid/factories/flash.rb', line 19

def get_unit(key)
  @counters[key] ||= 0
  @values[key] ||= 1.0
  if @counters[key] >= @interval
    @values[key] = (@values[key] == 1.0 ? 0.0 : 1.0)
    @counters[key] = 0
  end
  #Increment counter.
  @counters[key] += 1
  #Return value.
  @values[key]
end