-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting sequence starting point (#414)
What changed? ============ We introduce `ExMachina.sequence/3` that allows for passing the `start_at` option to set a starting point for a sequence. This is meant to be used in a factory definition: ```elixir def money_factory do %{ cents: sequence(:cents, &"#{&1}", start_at: 60) } end ``` Then, the following would increment starting at 60: ```elixir build(:money) // => %{cents: "60"} build(:money) // => %{cents: "61"} ```
- Loading branch information
Showing
4 changed files
with
44 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,13 @@ defmodule ExMachina do | |
@before_compile unquote(__MODULE__) | ||
|
||
import ExMachina, | ||
only: [sequence: 1, sequence: 2, merge_attributes: 2, evaluate_lazy_attributes: 1] | ||
only: [ | ||
sequence: 1, | ||
sequence: 2, | ||
sequence: 3, | ||
merge_attributes: 2, | ||
evaluate_lazy_attributes: 1 | ||
] | ||
|
||
def build(factory_name, attrs \\ %{}) do | ||
ExMachina.build(__MODULE__, factory_name, attrs) | ||
|
@@ -142,10 +148,25 @@ defmodule ExMachina do | |
} | ||
end | ||
""" | ||
|
||
@spec sequence(any, (integer -> any) | nonempty_list) :: any | ||
def sequence(name, formatter), do: ExMachina.Sequence.next(name, formatter) | ||
|
||
@doc """ | ||
Similar to `sequence/2` but it allows for passing a `start_at` option | ||
to the sequence generation. | ||
## Examples | ||
def user_factory do | ||
%{ | ||
# Will generate "[email protected]" then "[email protected]", etc. | ||
email: sequence(:email, &"me-\#{&1}@foo.com", start_at: 100), | ||
} | ||
end | ||
""" | ||
@spec sequence(any, (integer -> any) | nonempty_list, start_at: non_neg_integer) :: any | ||
def sequence(name, formatter, opts), do: ExMachina.Sequence.next(name, formatter, opts) | ||
|
||
@doc """ | ||
Builds a single factory. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters