Sha256: 27e6b7806eeca1c10c174fc0e8b29ce1156e38c1267f65e7f0f29fea5d1567b2

Contents?: true

Size: 920 Bytes

Versions: 1

Compression:

Stored size: 920 Bytes

Contents

require 'rubyonacid/factory'

module RubyOnAcid

#Loops from the minimum value to the maximum and around again.
class LoopFactory < Factory
  
  #An amount between 0 and 1 to increment counters by.
  attr_accessor :interval
  def interval=(value)
    raise "assigned #{value} to interval, must be between -1 and 1" if value < -1 or value > 1
    @interval = value
  end
  
  #Takes a hash with all keys supported by Factory, plus these keys and defaults:
  #  :interval => 0.01
  def initialize(options = {})
    super
    @counters = {}
    self.interval = options[:interval] || 0.01
  end
  
  #Increment counter for key, looping it around to opposite side if it exits boundary.
  def get_unit(key)
    @counters[key] ||= 0
    @counters[key] += @interval
    @counters[key] = @counters[key] - 1.0 if @counters[key] > 1
    @counters[key] = @counters[key] + 1.0 if @counters[key] < 0
    @counters[key]
  end

end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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