Erlang: is there a way to export some other modules from my module?

I am writing several resources for webmachine that have many of the same features. Therefore, I wonder if it is possible to write common functions in a separate module, then somehow enable it, and export its export using a resource (without having to export them from each resource explicitly).

+3
source share
4 answers

Yes, you can use the mixer library, https://github.com/opscode/mixer , this is a compiler syntax conversion that will do exactly what you want. I use it everywhere for webmachine callbacks

+1
source

-, Webgear , . , . - .

-, :

[{"/some/path", webgear_wrapper, {actual_resource, ["Some", extra, "Args"]}}].

, :

-record(context, {module, context, exports}).

init({Mod, Args}) ->
    {ok, Context} = Mod:init(Args),
    {ok, #context{module = Mod, context = Context, exports = exports(Mod)}}.

exports(Mod) ->
    dict:from_list(Mod:module_info(exports)).

Webgear, .

- ( , , ), , , :

call(#context{module = Mod, context = Cxt, exports = Exports},
     Func, Req, Default) ->
    case dict:is_key(Func, Exports) of
        true  -> Mod:Func(Req, Cxt);
        false -> {Default, Req, Cxt}
    end.

call/4, , :

malformed_request(Req, Cxt) ->
    % false here is the default value to return if the callback is missing
    {Res, NewReq, NewCxt} = call(Cxt, malformed_request, Req, false),
    % Now we must update the state accordingly
    {Res, NewReq, Cxt#context{context = NewCxt}}.

, ( ). , , ( , ).

+3

No, It is Immpossible. What I did was implement all the callbacks needed by my application for a common function. Another way would be to fix webmachine to extract cb from the function instead of looking at export.

0
source

Source: https://habr.com/ru/post/1793248/


All Articles