Skip to content

Commit 37d5b40

Browse files
committed
adding properties and such to the boss_record_compiler
1 parent f936ce8 commit 37d5b40

File tree

2 files changed

+126
-41
lines changed

2 files changed

+126
-41
lines changed

src/boss_record_compiler.erl

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
-compile(export_all).
77
-endif.
88

9-
-type limit() :: integer()|all.
9+
%-type limit() :: pos_integer()|all.
1010
-type syntaxTree() :: erl_syntax:syntaxTree().
11-
-type name() :: atom()|[byte()].
11+
-type name() :: atom()|[byte(),...].
1212

1313
-export([compile/1, compile/2, edoc_module/1, edoc_module/2, process_tokens/1, trick_out_forms/2]).
1414
-spec compile(binary() | [atom() | [any()] | char()]) -> any().
@@ -34,25 +34,38 @@
3434
-spec save_forms(atom()) -> [{'tree',atom(),{'attr',_,[any()],{_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],{_,_,_}},_},...].
3535
-spec parameter_getter_forms([any()]) -> [{'tree',atom(),{_,_,_,_},_} | {'wrapper',atom(),{_,_,_,_},_}].
3636
-spec deep_get_forms() -> [{'tree',atom(),{'attr',_,[any()],{_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],{_,_,_}},_},...].
37-
-spec get_attributes_forms(atom(),[any()]) -> [{'tree',atom(),{_,_,_,_},_} | {'wrapper',atom(),{_,_,_,_},_},...].
38-
-spec set_attributes_forms(atom(),[any()]) -> [{'tree',atom(),{_,_,_,_},_} | {'wrapper',atom(),{_,_,_,_},_},...].
39-
-spec association_forms(atom(),[any()]) -> [any(),...].
40-
-spec belongs_to_list_forms([any()]) -> [{'tree',atom(),{_,_,_,_},_} | {'wrapper',atom(),{_,_,_,_},_},...].
41-
-spec attribute_names_forms(atom() | string() | number(),[any()]) -> [{'tree',atom(),{_,_,_,_},_} | {'wrapper',atom(),{_,_,_,_},_},...].
42-
-spec has_one_forms(atom() | string(),atom(),[any()]) -> [{'tree',atom(),{'attr',_,[any()],{_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],{_,_,_}},_},...].
43-
-spec has_many_forms(atom(),atom(),limit()|many,[any()]) -> [{'tree',atom(),{'attr',_,[any()],{_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],{_,_,_}},_},...].
44-
-spec first_or_undefined_forms({'tree',atom(),{'attr',0,[],'none'},_}) -> {'tree',atom(),{'attr',0,[],'none'},_}.
45-
-spec has_many_query_forms(atom() | string()) -> {'tree',atom(),{'attr',0,[],'none'},_}.
46-
-spec has_many_query_forms_with_conditions(atom() | string()) -> {'tree',atom(),{'attr',_,[any()],'none' | {_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],'none' | {_,_,_}},_}.
47-
%-spec has_many_application_forms(atom() | string(),
48-
% {'tree',atom(),{'attr',_,[any()],'none' | {_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],'none' | {_,_,_}},_},,atom() | string(),atom() | string(),[any()]) -> {'tree',atom(),{'attr',0,[],'none'},_}.
49-
-spec belongs_to_forms(atom() | string() | number(),atom(),atom()) -> {'tree',atom(),{'attr',_,[any()],{_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],{_,_,_}},_}.
50-
51-
-spec counter_getter_forms([atom()]) -> syntaxTree().
52-
-spec counter_reset_forms([name()]) -> syntaxTree().
53-
-spec counter_incr_forms([name()]) -> syntaxTree().
54-
-spec counter_name_forms(name()) -> syntaxTree().
55-
-spec parameter_to_colname(atom()) -> string().
37+
38+
-spec get_attributes_forms(atom(),[any()]) -> syntaxTree().
39+
-spec set_attributes_forms(atom(),[any()]) -> syntaxTree().
40+
-type assoc() :: {has, {atom(), integer()}} |
41+
{has, {atom(), integer(), [any()]}} |
42+
{belongs_to, atom()}.
43+
-spec association_forms(atom(),[assoc()]) -> [any(),...].
44+
45+
-spec belongs_to_list_forms([{atom(),any()}]) -> syntaxTree().
46+
47+
-spec belongs_to_list_make_list([{atom(),atom()}]) -> syntaxTree().
48+
-spec attribute_names_forms(name(),[atom()]) -> syntaxTree().
49+
-spec has_one_forms(name(),atom(),[any()]) -> syntaxTree().
50+
-spec has_many_forms(atom(),atom(), pos_integer()|all|many, [any()]) -> syntaxTree().
51+
-spec first_or_undefined_forms( syntaxTree()) -> syntaxTree().
52+
-spec has_many_application_forms(name(),{'tree',atom(),{'attr',_,[any()],'none' | {_,_,_}},_} | {'wrapper',atom(),{'attr',_,[any()],'none' | {_,_,_}},_},
53+
pos_integer(),
54+
name(),
55+
name(),
56+
[atom()])
57+
-> syntaxTree().
58+
59+
-spec has_many_query_forms_with_conditions(name()) -> syntaxTree().
60+
-spec belongs_to_forms(atom() | string() | number(),
61+
atom(),
62+
atom()) -> syntaxTree().
63+
-spec has_many_query_forms(name()) -> syntaxTree().
64+
-spec counter_getter_forms([atom()]) -> syntaxTree().
65+
-spec counter_reset_forms([name()]) -> syntaxTree().
66+
-spec counter_incr_forms([name()]) -> syntaxTree().
67+
-spec counter_name_forms(name()) -> syntaxTree().
68+
-spec parameter_to_colname(atom()) -> string().
5669
%% @spec compile( File::string() ) -> {ok, Module} | {error, Reason}
5770
%% @equiv compile(File, [])
5871
compile(File) ->
@@ -425,27 +438,30 @@ belongs_to_list_forms(BelongsToList) ->
425438
erl_syntax:atom(Type)])
426439
end, BelongsToList))])])),
427440

428-
erl_syntax:add_precomments([erl_syntax:comment(
429-
["% @spec belongs_to() -> [{atom(), BossRecord}]",
430-
lists:concat(["% @doc Retrieve all of the `belongs_to' associations at once."])])],
431-
erl_syntax:function(
432-
erl_syntax:atom(belongs_to),
433-
[erl_syntax:clause([], none, [erl_syntax:list(lists:map(
434-
fun({Name, _Type}) -> erl_syntax:tuple([
435-
erl_syntax:atom(Name),
436-
erl_syntax:application(none, erl_syntax:atom(Name), [])])
437-
end, BelongsToList))])]))
441+
erl_syntax:add_precomments([erl_syntax:comment(
442+
["% @spec belongs_to() -> [{atom(), BossRecord}]",
443+
lists:concat(["% @doc Retrieve all of the `belongs_to' associations at once."])])],
444+
erl_syntax:function(
445+
erl_syntax:atom(belongs_to),
446+
[erl_syntax:clause([], none, [erl_syntax:list(belongs_to_list_make_list(BelongsToList))])]))
438447
].
439448

449+
belongs_to_list_make_list(BelongsToList) ->
450+
lists:map(
451+
fun({Name, _Type}) -> erl_syntax:tuple([
452+
erl_syntax:atom(Name),
453+
erl_syntax:application(none, erl_syntax:atom(Name), [])])
454+
end, BelongsToList).
455+
440456
attribute_names_forms(ModuleName, Parameters) ->
441457
[ erl_syntax:add_precomments([erl_syntax:comment(
442-
["% @spec attribute_names() -> [atom()]",
443-
lists:concat(["% @doc A list of the lower-case `", ModuleName, "' parameters."])])],
444-
erl_syntax:function(
445-
erl_syntax:atom(attribute_names),
446-
[erl_syntax:clause([], none, [erl_syntax:list(lists:map(
447-
fun(P) -> erl_syntax:atom(parameter_to_colname(P)) end,
448-
Parameters))])]))].
458+
["% @spec attribute_names() -> [atom()]",
459+
lists:concat(["% @doc A list of the lower-case `", ModuleName, "' parameters."])])],
460+
erl_syntax:function(
461+
erl_syntax:atom(attribute_names),
462+
[erl_syntax:clause([], none, [erl_syntax:list(lists:map(
463+
fun(P) -> erl_syntax:atom(parameter_to_colname(P)) end,
464+
Parameters))])]))].
449465

450466
has_one_forms(HasOne, ModuleName, Opts) ->
451467
Type = proplists:get_value(module, Opts, HasOne),
@@ -542,9 +558,13 @@ has_many_forms(HasMany, ModuleName, Limit, Opts) ->
542558

543559
first_or_undefined_forms(Forms) ->
544560
erl_syntax:case_expr(Forms,
545-
[erl_syntax:clause([erl_syntax:list([erl_syntax:variable(?PREFIX++"Record")])], none,
546-
[erl_syntax:variable(?PREFIX++"Record")]),
547-
erl_syntax:clause([erl_syntax:underscore()], none, [erl_syntax:atom(undefined)])]).
561+
[erl_syntax:clause([erl_syntax:list([erl_syntax:variable(?PREFIX++"Record")])],
562+
none,
563+
[erl_syntax:variable(?PREFIX++"Record")]),
564+
565+
erl_syntax:clause([erl_syntax:underscore()],
566+
none,
567+
[erl_syntax:atom(undefined)])]).
548568

549569
has_many_query_forms(ForeignKey) ->
550570
erl_syntax:list([

test/boss_record_compiler_test.erl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,71 @@
22
-include_lib("proper/include/proper.hrl").
33
-include_lib("eunit/include/eunit.hrl").
44

5+
association_forms_test() ->
6+
?assert(proper:check_spec({boss_record_compiler, association_forms, 2},
7+
[{to_file, user}])),
8+
ok.
9+
10+
11+
%% belongs_to_list_forms_test() ->
12+
%% ?assert(proper:check_spec({boss_record_compiler, belongs_to_list_forms, 1},
13+
%% [{to_file, user}])),
14+
%% ok.
15+
16+
belongs_to_list_make_list_test() ->
17+
?assert(proper:check_spec({boss_record_compiler, belongs_to_list_make_list, 1},
18+
[{to_file, user}])),
19+
ok.
20+
21+
attribute_names_forms_test() ->
22+
?assert(proper:check_spec({boss_record_compiler, attribute_names_forms, 2},
23+
[{to_file, user}])),
24+
ok.
25+
26+
27+
has_one_forms_test() ->
28+
?assert(proper:check_spec({boss_record_compiler, has_one_forms, 3},
29+
[{to_file, user}])),
30+
ok.
31+
32+
33+
has_many_forms_test_test() ->
34+
?assert(proper:check_spec({boss_record_compiler, has_many_forms, 4},
35+
[{to_file, user}])),
36+
ok.
37+
38+
39+
first_or_undefined_forms_test() ->
40+
?assert(proper:check_spec({boss_record_compiler, first_or_undefined_forms, 1},
41+
[{to_file, user}])),
42+
ok.
43+
44+
45+
46+
has_many_application_forms_test() ->
47+
?assert(proper:check_spec({boss_record_compiler, has_many_application_forms, 6},
48+
[{to_file, user}])),
49+
ok.
50+
51+
52+
has_many_query_forms_test() ->
53+
?assert(proper:check_spec({boss_record_compiler, has_many_query_forms, 1},
54+
[{to_file, user}])),
55+
ok.
56+
57+
58+
has_many_query_forms_with_conditions_test() ->
59+
?assert(proper:check_spec({boss_record_compiler, has_many_query_forms_with_conditions, 1},
60+
[{to_file, user}])),
61+
ok.
62+
63+
64+
65+
counter_belongs_to_forms_test() ->
66+
?assert(proper:check_spec({boss_record_compiler, belongs_to_forms, 3},
67+
[{to_file, user}])),
68+
ok.
69+
570

671
counter_getter_forms_test() ->
772
?assertEqual([], boss_record_compiler:counter_getter_forms([])),

0 commit comments

Comments
 (0)