[erlang-questions] a little meta-programing help

anders conbere aconbere@REDACTED
Mon Oct 6 00:15:26 CEST 2008


Today I sat down to try to make working with mochiweb a little cleaner
for myself. I wanted a way to specify a list of mappings between url
patterns and methods to be called with the matched variables from
them. I quickly ran into issues with not being able to create patterns
dynamically, and then tried using funs to wrap that functionality but
pretty soon I was back at the same mess I started with.

This is kind of my "if the world were perfect" module (that doesn't
work at all but I hope might show what I was going for).

I was hoping folks might help be think up some ways to handle use
cases like this.

-module(roto_web_routes).
-export([
    call/1
    ]).

-define(VIEWS_MODULE, roto_web_views).

routes() ->
    [
        url(["version"], version),
        url(["register"], register),
        url(["node", NodeName, "messages"], messages),
        url(["node", NodeName, "messages", Id], message),
        url(["node", NodeName, "messages"], subscriptions),
        url(["node", NodeName, "messages", Id], subscriptions)
    ].

url(Pattern, View) ->
    {route, Pattern, View, View}.

url(Pattern, View, Name) ->
    {route, Pattern, View, Name}.

% should take a request, find the matching url config and pass
% the matched variables into the function specified in the url
% config for that entry.
call(Req) ->
    Path = string:tokens(Req:get(path), "/"),
    match_route(Path).

match_route(Path) ->
    %does something fancy with routes() to get to the function
    % I want to call and call it with the matched variables
    ok.

Thanks!
~ Anders



More information about the erlang-questions mailing list