Sha256: 93a3b290fe3e122f9218d5e3c491f81e62d5763e9e13a03100001750544b6b69

Contents?: true

Size: 1.17 KB

Versions: 19

Compression:

Stored size: 1.17 KB

Contents

module FactoryBot
  # Sequences are defined using sequence within a FactoryBot.define block.
  # Sequence values are generated using next.
  # @api private
  class Sequence
    attr_reader :name

    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

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

    def names
      [@name] + @aliases
    end

    def rewind
      @value.rewind
    end

    private

    def value
      @value.peek
    end

    def increment_value
      @value.next
    end

    class EnumeratorAdapter
      def initialize(value)
        @first_value = value
        @value = value
      end

      def peek
        @value
      end

      def next
        @value = @value.next
      end

      def rewind
        @value = @first_value
      end
    end
  end
end

Version data entries

19 entries across 18 versions & 3 rubygems

Version Path
factory_bot-6.5.1 lib/factory_bot/sequence.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/factory_bot-6.5.0/lib/factory_bot/sequence.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/factory_bot-6.5.0/lib/factory_bot/sequence.rb
factory_bot-6.5.0 lib/factory_bot/sequence.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/factory_bot-6.4.6/lib/factory_bot/sequence.rb
factory_bot-6.4.6 lib/factory_bot/sequence.rb
factory_bot-6.4.5 lib/factory_bot/sequence.rb
factory_bot-6.4.4 lib/factory_bot/sequence.rb
factory_bot-6.4.3 lib/factory_bot/sequence.rb
factory_bot-6.4.2 lib/factory_bot/sequence.rb
factory_bot-6.4.1 lib/factory_bot/sequence.rb
factory_bot-6.4.0 lib/factory_bot/sequence.rb
factory_bot-6.3.0 lib/factory_bot/sequence.rb
factory_bot-6.2.1 lib/factory_bot/sequence.rb
factory_bot-6.2.0 lib/factory_bot/sequence.rb
factory_bot-6.1.0 lib/factory_bot/sequence.rb
factory_bot-6.0.2 lib/factory_bot/sequence.rb
factory_bot-6.0.1 lib/factory_bot/sequence.rb
factory_bot-6.0.0 lib/factory_bot/sequence.rb