changed
hex_metadata.config
|
@@ -25,4 25,4 @@
|
25
25
|
{<<"optional">>,false},
|
26
26
|
{<<"repository">>,<<"hexpm">>},
|
27
27
|
{<<"requirement">>,<<"~> 3.11">>}]]}.
|
28
|
- {<<"version">>,<<"0.1.0">>}.
|
28
|
{<<"version">>,<<"0.2.0">>}.
|
changed
lib/prefixed_id.ex
|
@@ -18,12 18,17 @@ defmodule PrefixedID do
|
18
18
|
schema = Keyword.fetch!(opts, :schema)
|
19
19
|
|
20
20
|
cond do
|
21
|
- opts[:primary_key] -> %{primary_key: true, prefix: get_pkey_prefix(opts)}
|
22
|
- opts[:foreign_key] -> %{field: field, schema: schema}
|
23
|
- true -> raise "Must be used as either a primary or foreign key."
|
21
|
opts[:foreign_key] -> %{foreign_key: true, field: field, schema: schema}
|
22
|
prefix = opts[:prefix] -> %{prefix: validate_prefix(prefix)}
|
23
|
true -> raise "Must specify :prefix option if not used as a foreign key."
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
defp validate_prefix(prefix) do
|
28
|
if String.contains?(prefix, "_"), do: raise("The prefix must not contain an underscore.")
|
29
|
prefix
|
30
|
end
|
31
|
|
27
32
|
@impl true
|
28
33
|
def cast(nil, _params), do: {:ok, nil}
|
29
34
|
def cast(<<_::128>> = raw_uuid, params), do: load(raw_uuid, nil, params)
|
|
@@ -55,18 60,11 @@ defmodule PrefixedID do
|
55
60
|
|> from_numeric_uuid(params)
|
56
61
|
end
|
57
62
|
|
58
|
- defp get_pkey_prefix(opts) do
|
59
|
- prefix = Keyword.get(opts, :prefix)
|
60
|
- if is_nil(prefix), do: raise("The :prefix option is required for primary keys.")
|
61
|
- if String.contains?(prefix, "_"), do: raise("The prefix must not contain an underscore.")
|
63
|
defp prefix(%{prefix: prefix}) do
|
62
64
|
prefix
|
63
65
|
end
|
64
66
|
|
65
|
- defp prefix(%{primary_key: true, prefix: prefix}) do
|
66
|
- prefix
|
67
|
- end
|
68
|
-
|
69
|
- defp prefix(%{field: field, schema: schema}) do
|
67
|
defp prefix(%{foreign_key: true, field: field, schema: schema}) do
|
70
68
|
%{related: related, related_key: related_key} = schema.__schema__(:association, field)
|
71
69
|
{:parameterized, __MODULE__, %{prefix: prefix}} = related.__schema__(:type, related_key)
|
72
70
|
prefix
|
changed
mix.exs
|
@@ -6,7 6,7 @@ defmodule PrefixedID.MixProject do
|
6
6
|
def project do
|
7
7
|
[
|
8
8
|
app: :prefixed_id,
|
9
|
- version: "0.1.0",
|
9
|
version: "0.2.0",
|
10
10
|
elixir: "~> 1.16",
|
11
11
|
start_permanent: Mix.env() == :prod,
|
12
12
|
description: "UUIDv7-backed human-readable IDs for Ecto",
|