Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support for path query strings with boss_db:get/1

Also BossRecord:get/1. E.g. "manager.manager.name"
  • Loading branch information...
commit be7b617e1c6ea40b8618ec236ebe40dbff012c95 1 parent 2f4f4e4
@evanmiller evanmiller authored
Showing with 71 additions and 10 deletions.
  1. +13 −1 src/boss_db.erl
  2. +58 −9 src/boss_record_compiler.erl
View
14 src/boss_db.erl
@@ -12,6 +12,7 @@
find_first/3,
find_last/2,
find_last/3,
+ get/1,
count/1,
count/2,
counter/1,
@@ -56,7 +57,7 @@ stop() ->
ok.
db_call(Msg) ->
- case get(boss_db_transaction_info) of
+ case erlang:get(boss_db_transaction_info) of
undefined ->
boss_pool:call(?POOLNAME, Msg, ?DEFAULT_TIMEOUT);
State ->
@@ -72,6 +73,17 @@ find(Key) when is_list(Key) ->
find(_) ->
{error, invalid_id}.
+%% @spec get(Path::string()) -> Value | {error, Reason} | undefined
+%% @doc Find a BossRecord or value described by the dot-separated `Path' (e.g., "employee-42.manager.name").
+get(Path) when is_list(Path) ->
+ [IdToken|Rest] = string:tokens(Path, "."),
+ case find(IdToken) of
+ {error, Reason} ->
+ {error, Reason};
+ BossRecord ->
+ BossRecord:get(string:join(Rest, "."))
+ end.
+
%% @spec find(Type::atom(), Conditions) -> [ BossRecord ]
%% @doc Query for BossRecords. Returns all BossRecords of type
%% `Type' matching all of the given `Conditions'
View
67 src/boss_record_compiler.erl
@@ -78,7 +78,8 @@ trick_out_forms(LeadingForms, Forms, ModuleName, Parameters, TokenInfo) ->
counter_reset_forms(Counters) ++
counter_incr_forms(Counters) ++
association_forms(ModuleName, Attributes) ++
- parameter_getter_forms(Parameters),
+ parameter_getter_forms(Parameters) ++
+ deep_get_forms(),
UserFunctionList = list_functions(UserForms),
GeneratedFunctionList = list_functions(GeneratedForms),
@@ -227,18 +228,66 @@ parameter_getter_forms(Parameters) ->
[erl_syntax:clause([], none, [erl_syntax:variable(P)])]))
end, Parameters).
+deep_get_forms() ->
+ Function = erl_syntax:function(
+ erl_syntax:atom(get),
+ [erl_syntax:clause([erl_syntax:string("")], none,
+ [erl_syntax:variable("THIS")]),
+ erl_syntax:clause([erl_syntax:variable(?PREFIX++"Path")], none,
+ [erl_syntax:application(
+ erl_syntax:atom(lists),
+ erl_syntax:atom(foldl),
+ [erl_syntax:fun_expr([
+ erl_syntax:clause([
+ erl_syntax:variable(?PREFIX++"Token"),
+ erl_syntax:tuple([erl_syntax:atom("ok"), erl_syntax:variable(?PREFIX++"BossRecord")])], none,
+ [erl_syntax:application(
+ erl_syntax:atom(apply),
+ [erl_syntax:variable(?PREFIX++"BossRecord"),
+ erl_syntax:application(
+ erl_syntax:atom(list_to_existing_atom),
+ [erl_syntax:variable(?PREFIX++"Token")]),
+ erl_syntax:list([])])]),
+ erl_syntax:clause([
+ erl_syntax:variable("_"++?PREFIX++"Token"),
+ erl_syntax:atom("undefined")], none,
+ [erl_syntax:atom("undefined")]),
+ erl_syntax:clause([
+ erl_syntax:variable(?PREFIX++"Token"),
+ erl_syntax:variable(?PREFIX++"BossRecord")], none,
+ [erl_syntax:application(
+ erl_syntax:atom(apply),
+ [erl_syntax:variable(?PREFIX++"BossRecord"),
+ erl_syntax:application(
+ erl_syntax:atom(list_to_existing_atom),
+ [erl_syntax:variable(?PREFIX++"Token")]),
+ erl_syntax:list([])])])
+ ]),
+ erl_syntax:variable("THIS"),
+ erl_syntax:application(
+ erl_syntax:atom(string),
+ erl_syntax:atom(tokens),
+ [erl_syntax:variable(?PREFIX++"Path"),
+ erl_syntax:string(".")])])])]),
+
+ [erl_syntax:add_precomments([erl_syntax:comment(
+ [lists:concat(["% @spec get(Path::string()) -> Value"]),
+ lists:concat(["% @doc Returns a deeply nested value described by a dot-separated `Path' ",
+ "(e.g. \"puppy.mother.name\")"])])],
+ Function)].
+
get_attributes_forms(ModuleName, Parameters) ->
+ Function = erl_syntax:function(erl_syntax:atom(attributes),
+ [erl_syntax:clause([], none,
+ [erl_syntax:list(lists:map(fun(P) ->
+ erl_syntax:tuple([
+ erl_syntax:atom(parameter_to_colname(P)),
+ erl_syntax:variable(P)])
+ end, Parameters))])]),
[erl_syntax:add_precomments([erl_syntax:comment(
["% @spec attributes() -> [{Attribute::atom(), Value::string() | undefined}]",
lists:concat(["% @doc A proplist of the `", ModuleName, "' parameters and their values."])])],
- erl_syntax:function(
- erl_syntax:atom(attributes),
- [erl_syntax:clause([], none,
- [erl_syntax:list(lists:map(fun(P) ->
- erl_syntax:tuple([
- erl_syntax:atom(parameter_to_colname(P)),
- erl_syntax:variable(P)])
- end, Parameters))])]))].
+ Function)].
set_attributes_forms(ModuleName, Parameters) ->
[erl_syntax:add_precomments([erl_syntax:comment(
Please sign in to comment.
Something went wrong with that request. Please try again.