Skip to content

Commit

Permalink
Add Sequence.next/1 for quickly creating sequences
Browse files Browse the repository at this point in the history
Closes #66
  • Loading branch information
Paul Smith committed Jan 8, 2016
1 parent ee12e1d commit 716deee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
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
6 changes: 6 additions & 0 deletions lib/ex_machina/sequence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ defmodule ExMachina.Sequence do
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
end

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

def next(sequence_name, formatter) do
Agent.get_and_update(__MODULE__, fn(sequences) ->
current_value = HashDict.get(sequences, sequence_name, 0)
Expand Down
5 changes: 5 additions & 0 deletions test/ex_machina/sequence_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ 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
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

0 comments on commit 716deee

Please sign in to comment.