Sha256: 6a1d69713d21136ddc87c1b37fef88dc5fc68d9f25a54613cc47c1850d73a6fa

Contents?: true

Size: 877 Bytes

Versions: 1

Compression:

Stored size: 877 Bytes

Contents

require 'rubyonacid/factory'

module RubyOnAcid

#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.
class FlashFactory < Factory
  
  #The number of times to return a value before switching.
  attr_accessor :interval
  
  #Takes a hash with all keys supported by Factory, plus these keys and defaults:
  #  :interval => 3
  def initialize(options = {})
    super
    @counters = {}
    @values = {}
    @interval = options[:interval] || 3
  end
  
  #If key is over threshold, flip to other value and reset counter.
  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

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyonacid-0.4.0 lib/rubyonacid/factories/flash.rb