Class: RubyOnAcid::RepeatFactory

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

Overview

Gets values from a source factory and returns the same value the given number of times before getting a fresh value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Factory

#boolean, #choose, #get, #within

Constructor Details

#initialize(source_factory = nil, repeat_count = 2) ⇒ RepeatFactory

Returns a new instance of RepeatFactory.



13
14
15
16
17
18
19
# File 'lib/rubyonacid/factories/repeat.rb', line 13

def initialize(source_factory = nil, repeat_count = 2)
  super
  @source_factory = source_factory
  @repeat_count = repeat_count
  @repeat_counts = {}
  @values = {}
end

Instance Attribute Details

#repeat_countObject

The number of times to repeat a value for a given key.



11
12
13
# File 'lib/rubyonacid/factories/repeat.rb', line 11

def repeat_count
  @repeat_count
end

#source_factoryObject

Factory to get values from.



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

def source_factory
  @source_factory
end

Instance Method Details

#get_unit(key) ⇒ Object

Returns the value of get_unit on the source factory the assigned number of times.



22
23
24
25
26
27
28
29
30
31
# File 'lib/rubyonacid/factories/repeat.rb', line 22

def get_unit(key)
  @repeat_counts[key] ||= 0
  if @repeat_counts[key] >= @repeat_count
    @values[key] = nil 
    @repeat_counts[key] = 0
  end
  @values[key] ||= @source_factory.get_unit(key)
  @repeat_counts[key] += 1
  @values[key]
end