PageRenderTime 143ms CodeModel.GetById 61ms app.highlight 12ms RepoModel.GetById 68ms app.codeStats 0ms

/ucengine/src/core/utils.erl

http://github.com/AF83/ucengine
Erlang | 107 lines | 74 code | 14 blank | 19 comment | 0 complexity | 5cd26ba6556050aa1557d7431415ec0b MD5 | raw file
  1%%
  2%%  U.C.Engine - Unified Collaboration Engine
  3%%  Copyright (C) 2011 af83
  4%%
  5%%  This program is free software: you can redistribute it and/or modify
  6%%  it under the terms of the GNU Affero General Public License as published by
  7%%  the Free Software Foundation, either version 3 of the License, or
  8%%  (at your option) any later version.
  9%%
 10%%  This program is distributed in the hope that it will be useful,
 11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13%%  GNU Affero General Public License for more details.
 14%%
 15%%  You should have received a copy of the GNU Affero General Public License
 16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17%%
 18-module(utils).
 19
 20-include("uce.hrl").
 21-include_lib("yaws/include/yaws_api.hrl").
 22
 23-export([now/0,
 24
 25         random/0,
 26         random/1,
 27         get_values/2,
 28         get/2,
 29         get/3,
 30         proplist_merge/2]).
 31
 32%% Get current timestamp
 33now() ->
 34    {Mega,Sec,Micro} = erlang:now(),
 35    erlang:round(((Mega*1000000+Sec)*1000000+Micro)/1000).
 36
 37random() ->
 38    random(32).
 39random(0) ->
 40    [];
 41random(Length) ->
 42    [crypto:rand_uniform(48,58)] ++ random(Length - 1).
 43
 44get(Params, Key) when is_atom(Key) ->
 45    [Result] = get(Params, [Key]),
 46    Result;
 47get(Params, Keys) ->
 48    get(Params, Keys, none).
 49
 50get(Params, Keys, Default) when is_atom(Default) ->
 51    get(Params, Keys, lists:map(fun(_Elem) ->
 52                                        Default
 53                                end,
 54                                Keys));
 55get(_Params, [], []) ->
 56    [];
 57get(Params, [Key|Keys], [Default|Defaults]) ->
 58    ValueList = case lists:keysearch(Key, 1, Params) of
 59                    {value, {Key, Value}} ->
 60                        [Value];
 61                    false ->
 62                        [Default]
 63                end,
 64    ValueList ++ get(Params, Keys, Defaults).
 65
 66get_values(Proplist, Keys) ->
 67    lists:map(fun({Name, Default}) ->
 68                proplists:get_value(Name, Proplist, Default)
 69              end, Keys).
 70
 71
 72%% Merge two proplist
 73proplist_merge(ProplistOne, ProplistTwo) ->
 74    lists:foldl(fun(Elem, Acc) ->
 75        case Elem of
 76            {Key, Value} ->
 77                case proplists:is_defined(Key, ProplistOne) of
 78                    true ->
 79                        Acc;
 80                    false -> Acc ++ [{Key, Value}]
 81                end;
 82            Key ->
 83                case proplists:is_defined(Key, ProplistOne) of
 84                    true ->
 85                        Acc;
 86                    false -> Acc ++ [Key]
 87                end
 88        end
 89    end,
 90        ProplistOne, ProplistTwo).
 91
 92-ifdef(TEST).
 93-include_lib("eunit/include/eunit.hrl").
 94
 95get_values_test() ->
 96    ?assertEqual(["chuck", "norris", rocks], get_values([{firstname, "chuck"}, {lastname, "norris"}], [{firstname, ""}, {lastname, ""}, {def, rocks}])).
 97
 98merge_test() ->
 99    A = [{firstname, "Chuck"}, {lastname, "Norris"}, alive],
100    B = [{firstname, "Robert"}, {shoes, "santiags"}, {lastname, "Norris2"}],
101    C = proplist_merge(A, B),
102    ?assertEqual("Chuck", proplists:get_value(firstname, C)),
103    ?assertEqual("Chuck", proplists:get_value(firstname, C)),
104    ?assertEqual("Norris", proplists:get_value(lastname, C)),
105    ?assertEqual(true, proplists:get_value(alive, C)).
106-endif.
107