2010-06-24 09:45:57 -04:00
|
|
|
module FactoryGirl
|
2008-06-01 13:46:50 -04:00
|
|
|
|
2011-01-26 20:55:06 -05:00
|
|
|
# Sequences are defined using sequence within a FactoryGirl.define block.
|
|
|
|
# Sequence values are generated using next.
|
2008-06-01 13:46:50 -04:00
|
|
|
class Sequence
|
2012-03-29 10:21:55 -04:00
|
|
|
attr_reader :name, :names, :value
|
2011-05-19 10:56:45 -04:00
|
|
|
|
2012-04-01 08:20:19 -04:00
|
|
|
def initialize(name, value = 1, options = {}, &proc) #:nodoc:
|
|
|
|
@value = value
|
|
|
|
if value.kind_of?(Hash)
|
|
|
|
options = value
|
|
|
|
@value = options[:value] || 1
|
|
|
|
end
|
|
|
|
@name = name
|
|
|
|
@names = ([name] + (options[:aliases] || [])).flatten
|
|
|
|
@proc = proc
|
2008-06-01 13:46:50 -04:00
|
|
|
end
|
|
|
|
|
2012-04-01 08:20:19 -04:00
|
|
|
# aliased sequences share the same sequence counter
|
2011-05-19 10:56:45 -04:00
|
|
|
def next
|
2011-01-27 16:25:31 -05:00
|
|
|
@proc ? @proc.call(@value) : @value
|
2010-08-12 22:42:18 -04:00
|
|
|
ensure
|
|
|
|
@value = @value.next
|
2008-06-01 13:46:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|