You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by be...@apache.org on 2014/02/13 17:35:36 UTC
[03/23] goldrush commit: updated refs/heads/import-master to 71e6321
Add application files and event access module.
Project: http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/commit/3ab6e09c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/tree/3ab6e09c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/diff/3ab6e09c
Branch: refs/heads/import-master
Commit: 3ab6e09c8c4633da8537a023d36aa35925f96f04
Parents: 9abd34b
Author: Magnus Klaar <kl...@ninenines.fr>
Authored: Mon Apr 23 21:40:55 2012 +0200
Committer: Magnus Klaar <kl...@ninenines.fr>
Committed: Mon Apr 23 21:40:55 2012 +0200
----------------------------------------------------------------------
src/goldrush.app.src | 8 ++++
src/gr_app.erl | 27 ++++++++++++++
src/gr_sup.erl | 26 +++++++++++++
src/gre.erl | 95 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 156 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/blob/3ab6e09c/src/goldrush.app.src
----------------------------------------------------------------------
diff --git a/src/goldrush.app.src b/src/goldrush.app.src
new file mode 100644
index 0000000..5c811fb
--- /dev/null
+++ b/src/goldrush.app.src
@@ -0,0 +1,8 @@
+{application, goldrush, [
+ {description, ""},
+ {vsn, "0.1.0"},
+ {registered, []},
+ {applications, [kernel, stdlib, syntax_tools]},
+ {mod, {gr_app, []}},
+ {env, []}
+]}.
http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/blob/3ab6e09c/src/gr_app.erl
----------------------------------------------------------------------
diff --git a/src/gr_app.erl b/src/gr_app.erl
new file mode 100644
index 0000000..20b3f9b
--- /dev/null
+++ b/src/gr_app.erl
@@ -0,0 +1,27 @@
+%% Copyright (c) 2012, Magnus Klaar <kl...@ninenines.eu>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+-module(gr_app).
+-behaviour(application).
+
+-export([
+ start/2,
+ stop/1
+]).
+
+start(_Type, _Args) ->
+ gr_sup:start_link().
+
+stop(_State) ->
+ ok.
http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/blob/3ab6e09c/src/gr_sup.erl
----------------------------------------------------------------------
diff --git a/src/gr_sup.erl b/src/gr_sup.erl
new file mode 100644
index 0000000..f679de0
--- /dev/null
+++ b/src/gr_sup.erl
@@ -0,0 +1,26 @@
+%% Copyright (c) 2012, Magnus Klaar <kl...@ninenines.eu>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+%% @doc Top level supervisor for goldrush.
+-module(gr_sup).
+-behaviour(supervisor).
+
+-export([start_link/0]).
+-export([init/1]).
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+init([]) ->
+ {ok, {{one_for_all, 5, 10}, []}}.
http://git-wip-us.apache.org/repos/asf/couchdb-goldrush/blob/3ab6e09c/src/gre.erl
----------------------------------------------------------------------
diff --git a/src/gre.erl b/src/gre.erl
new file mode 100644
index 0000000..eca4e2c
--- /dev/null
+++ b/src/gre.erl
@@ -0,0 +1,95 @@
+%% Copyright (c) 2012, Magnus Klaar <kl...@ninenines.eu>
+%%
+%% Permission to use, copy, modify, and/or distribute this software for any
+%% purpose with or without fee is hereby granted, provided that the above
+%% copyright notice and this permission notice appear in all copies.
+%%
+%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+%% @doc Accessor function for goldrush event terms.
+-module(gre).
+
+-export([
+ make/2,
+ has/2,
+ fetch/2,
+ find/2,
+ keys/1,
+ pairs/1
+]).
+
+-type event() :: {list, [{atom(), term()}]}.
+-export_types([event/0]).
+
+%% @doc Construct an event term.
+-spec make(term(), [list]) -> event().
+make(Term, [Type]) ->
+ {Type, Term}.
+
+
+%% @doc Check if a field exists in an event.
+-spec has(atom(), event()) -> boolean().
+has(Key, {list, List}) ->
+ lists:keymember(Key, 1, List).
+
+
+%% @doc Get the value of a field in an event.
+%% The field is expected to exist in the event.
+-spec fetch(atom(), event()) -> term().
+fetch(Key, {list, List}=Event) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> Value;
+ false -> erlang:error(badarg, [Key, Event])
+ end.
+
+
+%% @doc Find the value of a field in an event.
+%% This is equivalent to testing if a field exists using {@link has/2}
+%% before accessing the value of the field using {@link fetch/2}.
+-spec find(atom(), event()) -> {true, term()} | false.
+find(Key, {list, List}) ->
+ case lists:keyfind(Key, 1, List) of
+ {_, Value} -> {true, Value};
+ false -> false
+ end.
+
+
+%% @doc Get the names of all fields in an event.
+-spec keys(event()) -> [atom()].
+keys({list, List}) ->
+ kv_keys_(List).
+
+%% @private Get the names of all fields in a key-value list.
+-spec kv_keys_([{atom(), term()}]) -> [atom()].
+kv_keys_([{Key, _}|T]) ->
+ [Key|kv_keys_(T)];
+kv_keys_([]) ->
+ [].
+
+%% @doc Get the name and value of all fields in an event.
+-spec pairs(event()) -> [{atom(), term()}].
+pairs({list, List}) ->
+ lists:sort(List).
+
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+
+gre_test_() ->
+ [?_assert(gre:has(a, gre:make([{a,1}], [list]))),
+ ?_assertNot(gre:has(b, gre:make([{a,1}], [list]))),
+ ?_assertEqual(1, gre:fetch(a, gre:make([{a,1}], [list]))),
+ ?_assertError(badarg, gre:fetch(a, gre:make([], [list]))),
+ ?_assertEqual([], gre:keys(gre:make([], [list]))),
+ ?_assertEqual([a], gre:keys(gre:make([{a,1}], [list]))),
+ ?_assertEqual([a,b], gre:keys(gre:make([{a,1},{b,2}], [list]))),
+ ?_assertEqual([{a,1},{b,2}], gre:pairs(gre:make([{b,2},{a,1}], [list])))
+ ].
+
+-endif.