-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathpow_assent.ex
More file actions
54 lines (42 loc) · 1.63 KB
/
pow_assent.ex
File metadata and controls
54 lines (42 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
defmodule Mix.PowAssent do
@moduledoc """
Utilities module for mix tasks.
"""
@doc false
@spec validate_schema_args!([binary()], binary()) :: map() | no_return
def validate_schema_args!([schema, plural | _rest] = args, task) do
cond do
not schema_valid?(schema) ->
raise_invalid_schema_args_error!(
"Expected the schema argument, #{inspect(schema)}, to be a valid module name",
task
)
not plural_valid?(plural) ->
raise_invalid_schema_args_error!(
"Expected the plural argument, #{inspect(plural)}, to be all lowercase using snake_case convention",
task
)
true ->
schema_options_from_args(args)
end
end
def validate_schema_args!([_schema | _rest], task) do
raise_invalid_schema_args_error!("Invalid arguments", task)
end
def validate_schema_args!([], _task), do: schema_options_from_args()
defp schema_valid?(schema), do: schema =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/
defp plural_valid?(plural), do: plural =~ ~r/^[a-z\_]*$/
@spec raise_invalid_schema_args_error!(binary(), binary()) :: no_return()
defp raise_invalid_schema_args_error!(msg, task) do
Mix.raise("""
#{msg}
mix #{task} accepts both a module name and the plural of the resource:
mix #{task} UserIdentities.UserIdentity user_identities
""")
end
defp schema_options_from_args(_opts \\ [])
defp schema_options_from_args([schema, plural | _rest]),
do: %{schema_name: schema, schema_plural: plural}
defp schema_options_from_args(_any),
do: %{schema_name: "UserIdentities.UserIdentity", schema_plural: "user_identities"}
end