FactoryMan.Sequence (Factory Man v0.4.1)

View Source

Module for generating sequential values.

Use FactoryMan.sequence/1 or FactoryMan.sequence/2 to generate sequential values instead of calling this module directly.

Attribution

This module is adapted from the sequence functionality in ExMachina. The implementation has been copied and modified to work with FactoryMan.

Summary

Functions

Returns a specification to start this module under a supervisor.

Reset all sequences so that the next sequence starts from 0

Reset specific sequences so long as they already exist. The sequences specified will be reset to 0, while others will remain at their current index.

Functions

child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

reset()

@spec reset() :: :ok

Reset all sequences so that the next sequence starts from 0:

Example

FactoryMan.Sequence.next("joe") # "joe0"
FactoryMan.Sequence.next("joe") # "joe1"

FactoryMan.Sequence.reset()

FactoryMan.Sequence.next("joe") # resets so the return value is "joe0"

You can use list as well:

FactoryMan.Sequence.next("alphabet_sequence", ["A", "B"]) # "A"
FactoryMan.Sequence.next("alphabet_sequence", ["A", "B"]) # "B"

FactoryMan.Sequence.reset()

FactoryMan.Sequence.next("alphabet_sequence", ["A", "B"]) # resets so the return value is "A"

If you want to reset sequences at the beginning of every test, put it in a setup block in your test.

setup do
  FactoryMan.Sequence.reset()
end

reset(sequence_names)

@spec reset(any()) :: :ok

Reset specific sequences so long as they already exist. The sequences specified will be reset to 0, while others will remain at their current index.

Examples

You can reset a single sequence:

FactoryMan.Sequence.next(:alphabet, ["A", "B", "C"]) # "A"
FactoryMan.Sequence.next(:alphabet, ["A", "B", "C"]) # "B"

FactoryMan.Sequence.reset(:alphabet)

FactoryMan.Sequence.next(:alphabet, ["A", "B", "C"]) # "A"

You can also reset multiple sequences at once:

FactoryMan.Sequence.next(:numeric, [1, 2, 3]) # 1
FactoryMan.Sequence.next(:numeric, [1, 2, 3]) # 2
FactoryMan.Sequence.next("joe") # "joe0"
FactoryMan.Sequence.next("joe") # "joe1"

FactoryMan.Sequence.reset(["joe", :numeric])

FactoryMan.Sequence.next(:numeric, [1, 2, 3]) # 1
FactoryMan.Sequence.next("joe") # "joe0"