Class: FactoryBot::Sequence Private

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/sequence.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

API:

  • private

Defined Under Namespace

Classes: EnumeratorAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *args, &proc) ⇒ Sequence

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Sequence.

API:

  • private



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/factory_bot/sequence.rb', line 24

def initialize(name, *args, &proc)
  options = args.extract_options!
  @name = name
  @proc = proc
  @aliases = options.fetch(:aliases, []).map(&:to_sym)
  @uri_manager = FactoryBot::UriManager.new(names, paths: options[:uri_paths])
  @value = args.first || 1

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

Instance Attribute Details

#aliasesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



8
9
10
# File 'lib/factory_bot/sequence.rb', line 8

def aliases
  @aliases
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



8
9
10
# File 'lib/factory_bot/sequence.rb', line 8

def name
  @name
end

#uri_managerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



8
9
10
# File 'lib/factory_bot/sequence.rb', line 8

def uri_manager
  @uri_manager
end

Class Method Details

.find(*uri_parts) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



10
11
12
13
14
15
16
# File 'lib/factory_bot/sequence.rb', line 10

def self.find(*uri_parts)
  if uri_parts.empty?
    fail ArgumentError, "wrong number of arguments, expected 1+)"
  else
    find_by_uri FactoryBot::UriManager.build_uri(*uri_parts)
  end
end

.find_by_uri(uri) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



18
19
20
21
22
# File 'lib/factory_bot/sequence.rb', line 18

def self.find_by_uri(uri)
  uri = uri.to_sym
  FactoryBot::Internal.sequences.to_a.find { |seq| seq.has_uri?(uri) } ||
    FactoryBot::Internal.inline_sequences.find { |seq| seq.has_uri?(uri) }
end

Instance Method Details

#for_factory?(test_factory_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



61
62
63
# File 'lib/factory_bot/sequence.rb', line 61

def for_factory?(test_factory_name)
  FactoryBot::Internal.factory_by_name(factory_name).names.include?(test_factory_name.to_sym)
end

#has_name?(test_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



53
54
55
# File 'lib/factory_bot/sequence.rb', line 53

def has_name?(test_name)
  names.include?(test_name.to_sym)
end

#has_uri?(uri) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

API:

  • private



57
58
59
# File 'lib/factory_bot/sequence.rb', line 57

def has_uri?(uri)
  uri_manager.include?(uri)
end

#namesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



49
50
51
# File 'lib/factory_bot/sequence.rb', line 49

def names
  [@name] + @aliases
end

#next(scope = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/factory_bot/sequence.rb', line 37

def next(scope = nil)
  if @proc && scope
    scope.instance_exec(value, &@proc)
  elsif @proc
    @proc.call(value)
  else
    value
  end
ensure
  increment_value
end

#rewindObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



65
66
67
# File 'lib/factory_bot/sequence.rb', line 65

def rewind
  @value.rewind
end

#set_value(new_value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If it’s an Integer based sequence, set the new value directly, else rewind and seek from the beginning until a match is found.

API:

  • private



73
74
75
76
77
78
79
80
81
# File 'lib/factory_bot/sequence.rb', line 73

def set_value(new_value)
  if can_set_value_directly?(new_value)
    @value.set_value(new_value)
  elsif can_set_value_by_index?
    set_value_by_index(new_value)
  else
    seek_value(new_value)
  end
end