move handling of anonymous handlers to gen_json

This commit is contained in:
alisdair sullivan 2011-12-14 20:57:17 -08:00
parent 29175a7845
commit e145220d92
2 changed files with 13 additions and 9 deletions

View file

@ -24,13 +24,18 @@
-module(gen_json).
-export([behaviour_info/1]).
-export([parser/2, parser/3]).
-export([parser/1, parser/2, parser/3]).
-export([handle_event/2, init/1]).
behaviour_info(callbacks) -> [{init, 0}, {handle_event, 2}];
behaviour_info(_) -> undefined.
parser(F) -> parser(F, []).
parser(F, Opts) when is_function(F, 1) -> parser(?MODULE, {F, undefined}, Opts);
parser({F, State}, Opts) when is_function(F, 2) -> parser(?MODULE, {F, State}, Opts);
parser(Mod, Args) -> parser(Mod, Args, []).
parser(Mod, Args, Opts) when is_atom(Mod), is_list(Opts) ->
@ -43,4 +48,10 @@ parser(Mod, Args, Opts) when is_atom(Mod), is_list(Opts) ->
fun(Input) -> (jsx:encoder(Mod, Args, Opts))(Input) end
; decoder ->
fun(Input) -> (jsx:decoder(Mod, Args, Opts))(Input) end
end.
end.
handle_event(Event, {F, undefined}) -> F(Event), {F, undefined};
handle_event(Event, {F, State}) -> {F, F(Event, State)}.
init(State) -> State.

View file

@ -26,7 +26,6 @@
-export([parse_opts/1, extract_opts/1]).
-export([nice_decimal/1]).
-export([json_escape/2]).
-export([handle_event/2, init/1]).
-include("../include/jsx_opts.hrl").
@ -267,12 +266,6 @@ to_hex(X) -> X + 48. %% ascii "1" is [49], "2" is [50], etc...
handle_event(Event, {F, undefined}) -> F(Event), {F, undefined};
handle_event(Event, {F, State}) -> {F, F(Event, State)}.
init(State) -> State.
%% eunit tests
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").