Sha256: 9c1be448821b0707bc607f51c7ce67b3891e99542361f82f133f237aa5b5c9e5

Contents?: true

Size: 873 Bytes

Versions: 1

Compression:

Stored size: 873 Bytes

Contents

require 'rubyonacid/factory'

module RubyOnAcid

#Gets values from a source factory and returns the same value the given number of times before getting a fresh value.
class RepeatFactory < Factory
  
  #The number of times to repeat a value for a given key.
  attr_accessor :repeat_count
  
  #Takes a hash with all keys supported by Factory, plus these keys and defaults:
  #  :repeat_count => 2
  def initialize(options = {})
    super
    @repeat_count = options[:repeat_count] || 2
    @repeat_counts = {}
    @values = {}
  end
  
  #Returns the value of get_unit from the source factories the assigned number of times.
  def get_unit(key)
    @repeat_counts[key] ||= 0
    if @repeat_counts[key] >= @repeat_count
      @values[key] = nil 
      @repeat_counts[key] = 0
    end
    @values[key] ||= super
    @repeat_counts[key] += 1
    @values[key]
  end
  
end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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