@@ -12,12 +12,14 @@ defmodule Construct.Hooks.Fasten do
12
12
def __compile__ ( module , fields ) do
13
13
cast_defs =
14
14
Enum . map ( fields , fn ( { name , type , opts } ) ->
15
+ function_name = :"__cast_#{ name } __"
16
+
15
17
{ default_before_clause , default_after_clause } =
16
18
case Keyword . get ( opts , :default ) do
17
19
:__construct_no_default_value__ ->
18
20
clause_after =
19
21
quote do
20
- def unquote ( :"cast_ #{ name } " ) ( _ , _ ) do
22
+ defp unquote ( function_name ) ( _ , _ ) do
21
23
{ :error , % { unquote ( name ) => :missing } }
22
24
end
23
25
end
@@ -27,7 +29,7 @@ defmodule Construct.Hooks.Fasten do
27
29
nil ->
28
30
clause_after =
29
31
quote do
30
- def unquote ( :"cast_ #{ name } " ) ( _ , _ ) do
32
+ defp unquote ( function_name ) ( _ , _ ) do
31
33
{ :error , % { unquote ( name ) => :missing } }
32
34
end
33
35
end
@@ -39,18 +41,18 @@ defmodule Construct.Hooks.Fasten do
39
41
40
42
clause_before =
41
43
quote do
42
- def unquote ( :"cast_ #{ name } " ) ( % { unquote ( to_string ( name ) ) => term } , _opts ) when term == unquote ( term ) do
44
+ defp unquote ( function_name ) ( % { unquote ( to_string ( name ) ) => term } , _opts ) when term == unquote ( term ) do
43
45
{ :ok , unquote ( term ) }
44
46
end
45
47
46
- def unquote ( :"cast_ #{ name } " ) ( % { unquote ( name ) => term } , _opts ) when term == unquote ( term ) do
48
+ defp unquote ( function_name ) ( % { unquote ( name ) => term } , _opts ) when term == unquote ( term ) do
47
49
{ :ok , unquote ( term ) }
48
50
end
49
51
end
50
52
51
53
clause_after =
52
54
quote do
53
- def unquote ( :"cast_ #{ name } " ) ( _ , _ ) do
55
+ defp unquote ( function_name ) ( _ , _ ) do
54
56
{ :ok , unquote ( term ) }
55
57
end
56
58
end
@@ -60,11 +62,11 @@ defmodule Construct.Hooks.Fasten do
60
62
61
63
cast_clause =
62
64
quote do
63
- def unquote ( :"cast_ #{ name } " ) ( % { unquote ( to_string ( name ) ) => term } , opts ) do
65
+ defp unquote ( function_name ) ( % { unquote ( to_string ( name ) ) => term } , opts ) do
64
66
Construct.Type . cast ( unquote ( type ) , term , opts )
65
67
end
66
68
67
- def unquote ( :"cast_ #{ name } " ) ( % { unquote ( name ) => term } , opts ) do
69
+ defp unquote ( function_name ) ( % { unquote ( name ) => term } , opts ) do
68
70
Construct.Type . cast ( unquote ( type ) , term , opts )
69
71
end
70
72
end
@@ -90,7 +92,7 @@ defmodule Construct.Hooks.Fasten do
90
92
with_matches =
91
93
Enum . map ( fields , fn ( { name , _type , _opts } ) ->
92
94
quote do
93
- { :ok , unquote ( Macro . var ( name , nil ) ) } <- unquote ( :"cast_ #{ name } " ) ( params , opts )
95
+ { :ok , unquote ( Macro . var ( name , nil ) ) } <- unquote ( :"__cast_ #{ name } __ " ) ( params , opts )
94
96
end
95
97
end )
96
98
@@ -104,7 +106,6 @@ defmodule Construct.Hooks.Fasten do
104
106
end
105
107
106
108
merge_blocks ( make_ast , cast_defs )
107
- |> tap ( fn ( ast ) -> IO . puts ( Macro . to_string ( ast ) ) end )
108
109
end
109
110
110
111
defp merge_blocks ( a , b ) do
0 commit comments