Skip to content

Instantly share code, notes, and snippets.

@onlyshk
Created October 25, 2011 09:46
Show Gist options
  • Save onlyshk/1312066 to your computer and use it in GitHub Desktop.
Save onlyshk/1312066 to your computer and use it in GitHub Desktop.
%%%---------------------------------------------------------------------
%%% Module config
%%%---------------------------------------------------------------------
%%% mod_poker plugin's config
%%%---------------------------------------------------------------------
%%% Exports
%%%---------------------------------------------------------------------
%%% create_foobar(Parent, Type)
%%% returns a new foobar object
%%% etc etc etc
%%%---------------------------------------------------------------------
-module(config).
-export([start/0, get_option/1]).
-define(CONFIG, "mod_poker.conf").
-include("ejabberd.hrl").
-record(config, {key, value}).
start() ->
mnesia:create_table(config, [{disc_copies, [node()]},
{attributes, record_info(fields, config)}]),
mnesia:add_table_copy(config, node(), ram_copies),
mnesia:clear_table(config),
load_file(?CONFIG).
load_file(File) ->
?INFO_MSG("Reading configuration file ~s~n", [File]),
case file:consult(File) of
{ok, Terms} ->
F = fun({Key, Value}) ->
Record = #config{key = Key, value = Value},
mnesia:dirty_write(Record)
end,
lists:foreach(F, Terms);
{error, Reason} ->
Msg = file:format_error(Reason),
?ERROR_MSG("Can't load config file ~s: ~s~n", [File, Msg]),
exit(File ++ ": " ++ Msg)
end.
get_option(Opt) ->
case ets:lookup(config, Opt) of
[#config{value = Val}] -> Val;
_ -> undefined
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment