Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add environment-values related functions, definitions and tests

  • Loading branch information...
commit 1613908ed8c54cfa288c620a0e131f05fedc34b0 1 parent 1c0d466
@yosukehara yosukehara authored
View
4 include/leo_commons.hrl
@@ -47,7 +47,9 @@
}).
-%% macros.
+%% Environment values
+-define(ETS_ENV_TABLE, 'leo_env_values').
+
-define(env_log_dir(ServerType),
case application:get_env(ServerType, log_dir) of
{ok, EnvLogDir} -> EnvLogDir;
View
34 src/leo_misc.erl
@@ -28,9 +28,11 @@
-author('Yosuke Hara').
-export([node_existence/1, get_value/2, get_value/3,
- binary_tokens/2
+ binary_tokens/2,
+ init_env/0, get_env/2, set_env/3
]).
+-include("leo_commons.hrl").
-include_lib("eunit/include/eunit.hrl").
%% @doc check a node existence.
@@ -69,3 +71,33 @@ binary_tokens(Bin, Delimiter) ->
Tokens
end.
+
+%% @doc Initialize table of env
+%%
+-spec(init_env() ->
+ ok).
+init_env() ->
+ catch ets:new(?ETS_ENV_TABLE,
+ [named_table, set, public, {read_concurrency, true}]),
+ ok.
+
+
+%% @doc Retrieve
+%%
+-spec(get_env(atom(), any()) ->
+ {ok, any()} | undefined).
+get_env(AppName, Key) ->
+ case ets:lookup(?ETS_ENV_TABLE, {env, AppName, Key}) of
+ [{_, Val}] ->
+ {ok, Val};
+ _ ->
+ undefined
+ end.
+
+
+-spec(set_env(atom(), any(), any()) ->
+ ok).
+set_env(AppName, Key, Val) ->
+ _ = ets:insert(?ETS_ENV_TABLE, {{env, AppName, Key}, Val}),
+ ok.
+
View
23 test/leo_misc_tests.erl
@@ -2,7 +2,7 @@
%%
%% Leo Commons
%%
-%% Copyright (c) 2012
+%% Copyright (c) 2012 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
@@ -18,10 +18,6 @@
%% specific language governing permissions and limitations
%% under the License.
%%
-%% -------------------------------------------------------------------
-%% Mnesia Test
-%% @doc
-%% @end
%%====================================================================
-module(leo_misc_tests).
-author('Yosuke Hara').
@@ -36,7 +32,8 @@ misc_test_() ->
[
fun node_existence_/0,
fun get_value_/0,
- fun binary_tokens_/0
+ fun binary_tokens_/0,
+ fun env_table_/0
].
@@ -78,5 +75,19 @@ binary_tokens_() ->
leo_misc:binary_tokens(<<>>, <<"/">>)),
ok.
+
+env_table_() ->
+ AppName = ?MODULE,
+ Key = "TEST_KEY",
+ Val = "TEST_VAL",
+
+ ok = leo_misc:init_env(),
+ undefined = leo_misc:get_env(AppName, Key),
+ ok = leo_misc:set_env(AppName, Key, Val),
+ {ok, Val} = leo_misc:get_env(AppName, Key),
+ ok = leo_misc:init_env(),
+ ok.
+
+
-endif.
Please sign in to comment.
Something went wrong with that request. Please try again.