Skip to content

Commit

Permalink
Merge pull request #32 from inaka/dave.28.store_order
Browse files Browse the repository at this point in the history
Add sort by id implementation in trails.erl
  • Loading branch information
Brujo Benavides committed Jul 3, 2015
2 parents 4cadd93 + 46b1b22 commit 4f27363
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
35 changes: 28 additions & 7 deletions src/trails.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ store(Trails) ->
all() ->
case application:get_application(trails) of
{ok, trails} ->
all(ets:select(trails, [{{'$1', '$2'}, [], ['$$']}]), []);
Trails = all(ets:select(trails, [{{'$1', '$2'}, [], ['$$']}]), []),
SortIdFun =
fun(A, B) -> maps:get(trails_id, A) < maps:get(trails_id, B) end,
SortedStoredTrails = lists:sort(SortIdFun, Trails),
lists:map(fun remove_id/1, SortedStoredTrails);
_ ->
throw({not_started, trails})
end.
Expand All @@ -142,13 +146,15 @@ retrieve(Path) ->
case application:get_application(trails) of
{ok, trails} ->
case ets:lookup(trails, Path) of
[{_, Val}] -> Val;
[{_, Val}] -> remove_id(Val);
[] -> notfound
end;
_ ->
throw({not_started, trails})
end.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Private API.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -175,12 +181,27 @@ all([[_, Trail] | T], Acc) ->
%% @private
-spec normalize_store_input([route_path()]) -> trails().
normalize_store_input(RoutesPaths) ->
[normalize(Path) || Path <- RoutesPaths].
normalize_id(normalize_paths(RoutesPaths)).

-spec normalize_id([route_path()]) -> trails().
normalize_id(Trails) ->
Length = length(Trails),
AddIdFun = fun(Trail, Id) -> Trail#{ trails_id => Id} end,
lists:zipwith(AddIdFun, Trails, lists:seq(1, Length)).

%% @private
-spec normalize_paths([route_path()]) -> trails().
normalize_paths(RoutesPaths) ->
[normalize_path(Path) || Path <- RoutesPaths].

%% @private
-spec remove_id(trail()) -> trail().
remove_id(Trail) -> maps:remove(trails_id, Trail).

%% @private
-spec normalize(route_path() | trail()) -> trail().
normalize({PathMatch, ModuleHandler, Options}) ->
-spec normalize_path(route_path() | trail()) -> trail().
normalize_path({PathMatch, ModuleHandler, Options}) ->
trail(PathMatch, ModuleHandler, Options);
normalize({PathMatch, Constraints, ModuleHandler, Options}) ->
normalize_path({PathMatch, Constraints, ModuleHandler, Options}) ->
trail(PathMatch, ModuleHandler, Options, #{}, Constraints);
normalize(Trail) when is_map(Trail) -> Trail.
normalize_path(Trail) when is_map(Trail) -> Trail.
19 changes: 16 additions & 3 deletions test/trails_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ basic_trails_routes(_Config) ->

-spec trails_store(config()) -> {atom(), string()}.
trails_store(_Config) ->
Trails = [
{"/resource/[:id]", trails_test2_handler, []},
TrailsRaw = [
{"/resource/[:id]", trails_test_handler, []},
{"/api/:id/resource", [], trails_test2_handler, [arg0]},
trails:trail("/assets/[...]", cowboy_static, {dir, "www/assets"}),
trails:trail("/such/path", http_basic_route, [], #{}),
Expand All @@ -277,9 +277,11 @@ trails_store(_Config) ->
],
{not_started, trails} = (catch trails:all()),
{not_started, trails} = (catch trails:retrieve("/")),
ok = trails:store(Trails),
ok = trails:store(TrailsRaw),
Trails = normalize_paths(TrailsRaw),
Length = length(Trails),
Length = length(trails:all()),
Trails = trails:all(),
#{path_match := "/assets/[...]"} = trails:retrieve("/assets/[...]"),
#{path_match := "/such/path"} = trails:retrieve("/such/path"),
#{path_match := "/very"} = trails:retrieve("/very"),
Expand All @@ -288,3 +290,14 @@ trails_store(_Config) ->
#{path_match := "/api/:id/resource"} = trails:retrieve("/api/:id/resource"),
notfound = trails:retrieve("/other"),
{comment, ""}.

%% @private
normalize_paths(RoutesPaths) ->
[normalize_path(Path) || Path <- RoutesPaths].

%% @private
normalize_path({PathMatch, ModuleHandler, Options}) ->
trails:trail(PathMatch, ModuleHandler, Options);
normalize_path({PathMatch, Constraints, ModuleHandler, Options}) ->
trails:trail(PathMatch, ModuleHandler, Options, #{}, Constraints);
normalize_path(Trail) when is_map(Trail) -> Trail.

0 comments on commit 4f27363

Please sign in to comment.