summaryrefslogtreecommitdiffstats
path: root/src/core/ephraim_config.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/ephraim_config.erl')
-rw-r--r--src/core/ephraim_config.erl59
1 files changed, 31 insertions, 28 deletions
diff --git a/src/core/ephraim_config.erl b/src/core/ephraim_config.erl
index 6c49351..6c556c2 100644
--- a/src/core/ephraim_config.erl
+++ b/src/core/ephraim_config.erl
@@ -1,15 +1,20 @@
-module(ephraim_config).
--compile([debug_info, export_all]).
+-behaviour(gen_server).
--spec init() -> ok.
-init() ->
- register(ephraim_config, self()),
- ephraim ! {self(), started},
-
+-export([start_link/0]).
+-export([get/1]).
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
+
+-spec start_link() -> {ok, pid()}.
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+-spec init(term()) -> {ok, dict()}.
+init(_Args) ->
{ok, Terms} = file:consult(config_path() ++ "/ephraim.cfg"),
Config = dict:from_list(lists:map(fun(Term) -> {element(1, Term), Term} end, Terms)),
- loop(Config).
+ {ok, Config}.
-spec config_path() -> string().
@@ -28,28 +33,26 @@ config_path() ->
end,
Path.
-
-spec get(atom()) -> tuple() | error.
get(Key) ->
+ gen_server:call(?MODULE, {get, Key}).
+
+
+-spec handle_call(term(), {pid(), term()}, dict()) -> {reply, tuple() | error, dict()}.
+handle_call({get, Key}, _From, Config) ->
ephraim_config ! {self(), get, Key},
- receive
- {config, ok, Key, Value} ->
- Value;
- {config, error, Key} ->
- error
- end.
-
--spec loop(dict()) -> ok.
-loop(Config) ->
- receive
- stop ->
- ok;
- {From, get, Key} ->
- case dict:find(Key, Config) of
- {ok, Value} ->
- From ! {config, ok, Key, Value};
- error ->
- From ! {config, error, Key}
- end,
- loop(Config)
+ case dict:find(Key, Config) of
+ {ok, Value} ->
+ {reply, Value, Config};
+ error ->
+ {reply, error, Config}
end.
+
+handle_cast(_Msg, Config) ->
+ {noreply, Config}.
+handle_info(_Msg, Config) ->
+ {noreply, Config}.
+terminate(_Reason, _Config) ->
+ ok.
+code_change(_OldVersion, Config, _Extra) ->
+ {ok, Config}.