aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric <[email protected]>2012-09-12 16:17:45 -0500
committerEric <[email protected]>2012-09-12 16:17:45 -0500
commit0c0830ac116a1c85e2db12917d655fcaf34ddf54 (patch)
tree467c28f221fd4b8ac826e6987a428953636adc12 /src
parenta1b7f0001e29faf7925bb2b13b56eeec190adddd (diff)
downloadrelx-0c0830ac116a1c85e2db12917d655fcaf34ddf54.tar.gz
relx-0c0830ac116a1c85e2db12917d655fcaf34ddf54.tar.bz2
relx-0c0830ac116a1c85e2db12917d655fcaf34ddf54.zip
suport a general 'logic' provider as the base of a plugin system
Diffstat (limited to 'src')
-rw-r--r--src/rcl_provider.erl59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/rcl_provider.erl b/src/rcl_provider.erl
new file mode 100644
index 0000000..5c73075
--- /dev/null
+++ b/src/rcl_provider.erl
@@ -0,0 +1,59 @@
+%%%-------------------------------------------------------------------
+%%% @author Eric Merritt <[email protected]>
+%%% @copyright 2011 Erlware, LLC.
+%%% @doc
+%%% A module that supports providing state manipulation services to the system.
+%%% @end
+%%%-------------------------------------------------------------------
+-module(rcl_provider).
+
+%% API
+-export([new/2, do/2, format/1]).
+
+-export_type([t/0]).
+
+%%%===================================================================
+%%% Types
+%%%===================================================================
+
+-opaque t() :: {?MODULE, module()}.
+
+-callback init(rcl_state:t()) -> {ok, rcl_state:t()} | {error, Reason::term()}.
+-callback do(rcl_state:t()) -> {error, Reason::term()} | {ok, rcl_state:t()}.
+-callback format({error, Reason::term()}) -> iolist().
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+
+%% @doc create a new provider object from the specified module. The
+%% module should implement the provider behaviour.
+%%
+%% @param ModuleName The module name.
+%% @param State0 The current state of the system
+-spec new(module(), rcl_state:t()) -> {t(), {ok, rcl_state:t()} | {error, Reason::term()}}.
+new(ModuleName, State0) when is_atom(ModuleName) ->
+ State1 = ModuleName:init(State0),
+ case code:which(ModuleName) of
+ non_existing ->
+ erlang:error({non_existing, ModuleName});
+ _ ->
+ ok
+ end,
+ {{?MODULE, ModuleName}, State1}.
+
+%% @doc Manipulate the state of the system, that new state
+%%
+%% @param Provider the provider object
+%% @param State the current state of the system
+-spec do(Provider::t(), rcl_state:t()) -> rcl_state:t().
+do({?MODULE, Mod}, State) ->
+ Mod:do(State).
+
+%% @doc print the provider module name
+%%
+%% @param T - The provider
+%% @return An iolist describing the provider
+-spec format(t()) -> iolist().
+format({?MODULE, Mod}) ->
+ erlang:atom_to_list(Mod).