Permalink
Browse files

Merge pull request #5 from bluegraybox/master

Double-init issue
  • Loading branch information...
2 parents a34ae51 + 0af73f4 commit 4dd02fc4c00118abb03ec12cf212d5e8ef9fdc2b @flashingpumpkin committed Oct 7, 2011
Showing with 15 additions and 10 deletions.
  1. +15 −10 src/spooky.erl
View
@@ -39,19 +39,14 @@ start(Modules) when is_list(Modules)->
start([Module|T], Handlers, Middlewares)->
?LOG_INFO("Starting spooky with ~p", [Module]),
% The first module's options are the ones for misultin
- Opts = apply(Module, init, [[]]),
-
- Opts0 = proplists:delete(handlers, Opts),
- ?LOG_INFO("Options ~p", [Opts]),
- start([Module|T], Handlers, Middlewares, Opts0).
+ {ModuleOpts, ModuleHandlers, ModuleMiddlewares} = init_module(Module),
+ Opts = proplists:delete(handlers, ModuleOpts),
+ ?LOG_INFO("Options ~p", [ModuleOpts]),
+ start(T, Handlers ++ ModuleHandlers, Middlewares ++ ModuleMiddlewares, Opts).
start([Module|T], Handlers, Middlewares, Opts)->
% Accumulate all the handlers and middlewares
- ModuleOpts = apply(Module, init, [[]]),
-
- ModuleHandlers = lookup(handlers, ModuleOpts, [Module]),
- ModuleMiddlewares = lookup(middlewares, ModuleOpts, []),
-
+ {_, ModuleHandlers, ModuleMiddlewares} = init_module(Module),
start(T, Handlers ++ ModuleHandlers, Middlewares ++ ModuleMiddlewares, Opts);
start([], Handlers, Middlewares, Opts)->
@@ -90,6 +85,16 @@ behaviour_info(callbacks)->
behaviour_info(_Other)->
undefined.
+
+%%
+%% Utility methods
+%%
+init_module(Module) ->
+ Opts = apply(Module, init, [[]]),
+ Handlers = lookup(handlers, Opts, [Module]),
+ Middlewares = lookup(middlewares, Opts, []),
+ {Opts, Handlers, Middlewares}.
+
lookup(Key, Opts, Default)->
case proplists:lookup(Key, Opts) of
none ->

0 comments on commit 4dd02fc

Please sign in to comment.