Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sequence.next/1 for quickly creating sequences #84

Merged
merged 1 commit into from
Jan 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/ex_machina.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ defmodule ExMachina do
quote do
@before_compile unquote(__MODULE__)

import ExMachina, only: [sequence: 2, factory: 2]
import ExMachina, only: [sequence: 1, sequence: 2, factory: 2]

def build(factory_name, attrs \\ %{}) do
ExMachina.build(__MODULE__, factory_name, attrs)
Expand Down Expand Up @@ -108,6 +108,22 @@ defmodule ExMachina do
"""
end

@doc """
Shortcut for creating unique values. Similar to sequence/2

For more customization of the generated string, see ExMachina.sequence/2

## Examples

def factory(:comment) do
%{
# Will generate "Comment Title 0" then "Comment Title 1", etc.
title: sequence("Comment Title")
}
end
"""
def sequence(name), do: ExMachina.Sequence.next(name)

@doc """
Create sequences for generating unique values

Expand Down
13 changes: 13 additions & 0 deletions lib/ex_machina/sequence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ defmodule ExMachina.Sequence do
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
end

def next(sequence_name) when is_binary(sequence_name) do
next sequence_name, fn(n)->
sequence_name <> " " <> to_string(n)
end
end

def next(sequence_name) do
raise(
ArgumentError,
"Sequence name must be a string, got #{inspect sequence_name} instead"
)
end

def next(sequence_name, formatter) do
Agent.get_and_update(__MODULE__, fn(sequences) ->
current_value = HashDict.get(sequences, sequence_name, 0)
Expand Down
11 changes: 11 additions & 0 deletions test/ex_machina/sequence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,15 @@ defmodule ExMachina.SequenceTest do
assert 0 == Sequence.next(:month, &(&1))
assert 1 == Sequence.next(:month, &(&1))
end

test "let's you quickly create sequences" do
assert "Comment Body 0" == Sequence.next("Comment Body")
assert "Comment Body 1" == Sequence.next("Comment Body")
end

test "only accepts strings for sequence shortcut" do
assert_raise ArgumentError, ~r/must be a string/, fn ->
Sequence.next(:not_a_string)
end
end
end
11 changes: 11 additions & 0 deletions test/ex_machina_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ defmodule ExMachinaTest do
}
end

def factory(:article) do
%{
title: sequence("Post Title")
}
end

def save_record(record) do
send self, {:custom_save, record}
record
Expand All @@ -37,6 +43,11 @@ defmodule ExMachinaTest do
assert "me-1@foo.com" == Factory.build(:email).email
end

test "sequence/1 shortcut for creating sequences" do
assert "Post Title 0" == Factory.build(:article).title
assert "Post Title 1" == Factory.build(:article).title
end

test "raises a helpful error if the factory is not defined" do
assert_raise ExMachina.UndefinedFactory, fn ->
Factory.build(:foo)
Expand Down