class FactoryBot::Sequence

Sequences are defined using sequence within a FactoryBot.define block. Sequence values are generated using next. @api private

Attributes

name[R]

Public Class Methods

new(name, *args, &proc) click to toggle source
# File lib/factory_bot/sequence.rb, line 8
def initialize(name, *args, &proc)
  @name = name
  @proc = proc

  options = args.extract_options!
  @value = args.first || 1
  @aliases = options.fetch(:aliases) { [] }

  unless @value.respond_to?(:peek)
    @value = EnumeratorAdapter.new(@value)
  end
end

Public Instance Methods

names() click to toggle source
# File lib/factory_bot/sequence.rb, line 33
def names
  [@name] + @aliases
end
next(scope = nil) click to toggle source
# File lib/factory_bot/sequence.rb, line 21
def next(scope = nil)
  if @proc && scope
    scope.instance_exec(value, &@proc)
  elsif @proc
    @proc.call(value)
  else
    value
  end
ensure
  increment_value
end
rewind() click to toggle source
# File lib/factory_bot/sequence.rb, line 37
def rewind
  @value.rewind
end

Private Instance Methods

increment_value() click to toggle source
# File lib/factory_bot/sequence.rb, line 47
def increment_value
  @value.next
end
value() click to toggle source
# File lib/factory_bot/sequence.rb, line 43
def value
  @value.peek
end