PageRenderTime 50ms CodeModel.GetById 44ms RepoModel.GetById 0ms 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. -include("uce.hrl").
  20. -include_lib("yaws/include/yaws_api.hrl").
  21. -export([now/0,
  22. random/0,
  23. random/1,
  24. get_values/2,
  25. get/2,
  26. get/3,
  27. proplist_merge/2]).
  28. %% Get current timestamp
  29. now() ->
  30. {Mega,Sec,Micro} = erlang:now(),
  31. erlang:round(((Mega*1000000+Sec)*1000000+Micro)/1000).
  32. random() ->
  33. random(32).
  34. random(0) ->
  35. [];
  36. random(Length) ->
  37. [crypto:rand_uniform(48,58)] ++ random(Length - 1).
  38. get(Params, Key) when is_atom(Key) ->
  39. [Result] = get(Params, [Key]),
  40. Result;
  41. get(Params, Keys) ->
  42. get(Params, Keys, none).
  43. get(Params, Keys, Default) when is_atom(Default) ->
  44. get(Params, Keys, lists:map(fun(_Elem) ->
  45. Default
  46. end,
  47. Keys));
  48. get(_Params, [], []) ->
  49. [];
  50. get(Params, [Key|Keys], [Default|Defaults]) ->
  51. ValueList = case lists:keysearch(Key, 1, Params) of
  52. {value, {Key, Value}} ->
  53. [Value];
  54. false ->
  55. [Default]
  56. end,
  57. ValueList ++ get(Params, Keys, Defaults).
  58. get_values(Proplist, Keys) ->
  59. lists:map(fun({Name, Default}) ->
  60. proplists:get_value(Name, Proplist, Default)
  61. end, Keys).
  62. %% Merge two proplist
  63. proplist_merge(ProplistOne, ProplistTwo) ->
  64. lists:foldl(fun(Elem, Acc) ->
  65. case Elem of
  66. {Key, Value} ->
  67. case proplists:is_defined(Key, ProplistOne) of
  68. true ->
  69. Acc;
  70. false -> Acc ++ [{Key, Value}]
  71. end;
  72. Key ->
  73. case proplists:is_defined(Key, ProplistOne) of
  74. true ->
  75. Acc;
  76. false -> Acc ++ [Key]
  77. end
  78. end
  79. end,
  80. ProplistOne, ProplistTwo).
  81. -ifdef(TEST).
  82. -include_lib("eunit/include/eunit.hrl").
  83. get_values_test() ->
  84. ?assertEqual(["chuck", "norris", rocks], get_values([{firstname, "chuck"}, {lastname, "norris"}], [{firstname, ""}, {lastname, ""}, {def, rocks}])).
  85. merge_test() ->
  86. A = [{firstname, "Chuck"}, {lastname, "Norris"}, alive],
  87. B = [{firstname, "Robert"}, {shoes, "santiags"}, {lastname, "Norris2"}],
  88. C = proplist_merge(A, B),
  89. ?assertEqual("Chuck", proplists:get_value(firstname, C)),
  90. ?assertEqual("Chuck", proplists:get_value(firstname, C)),
  91. ?assertEqual("Norris", proplists:get_value(lastname, C)),
  92. ?assertEqual(true, proplists:get_value(alive, C)).
  93. -endif.