Skip to content

Commit

Permalink
feat: add default build/2 and start encouring struct!/2 instead of Ma…
Browse files Browse the repository at this point in the history
…p.merge/2
  • Loading branch information
nelsonmestevao committed Jun 22, 2024
1 parent cf23ce6 commit 1262c8f
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/factoid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ defmodule Factoid do
last_name: "Doe",
email_address: "jane-#{unique_integer()}@example.com",
}
|> Map.merge(attrs)
|> struct!(attrs)
end
def build(:user_avatar, attrs) do
%UserAvatar{
user: build(:user)
}
|> Map.merge(attrs)
|> struct!(attrs)
end
end
```
Expand Down Expand Up @@ -118,6 +118,14 @@ defmodule Factoid do
@typedoc false
@type attrs :: map() | keyword()

@doc """
Builds a record with attributes.
"""
@spec build(factory_name(), attrs()) :: record()
def build(factory_name, attrs \\ %{}) do
factory_name |> then(&build(@repo, &1)) |> struct!(attrs)
end

@doc """
Inserts a record with attributes.
"""
Expand All @@ -135,6 +143,8 @@ defmodule Factoid do
Generates a UUID.
"""
def unique_uuid, do: Factoid.unique_uuid()

defoverridable build: 2
end
end

Expand Down

0 comments on commit 1262c8f

Please sign in to comment.