Merge remote-tracking branch 'canonical/next'
[sinan.git] / src / sin_state.erl
blob910c8e1ec613ef7235b0d08b5eb0926aa6a5d88a
1 %% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
2 %%%---------------------------------------------------------------------------
3 %%% @author Eric Merritt <ericbmerritt@gmail.com>
4 %%% @copyright 2006, 2011 Erlware
5 %%% @doc
6 %%% An abstract storage and management piece for build related state
7 %%% pairs.
8 %%% @end
9 %%%----------------------------------------------------------------------------
10 -module(sin_state).
12 %% API
13 -export([new/0,
14 store/2,
15 store/3,
16 get_value/2,
17 get_value/3,
18 get_pairs/1,
19 project_app_by_name/2,
20 delete/2,
21 add_run_error/3,
22 get_run_errors/1,
23 add_run_warning/3,
24 get_run_warnings/1,
25 format_exception/1]).
27 -export_type([key/0,
28 value/0,
29 state/0]).
31 -include_lib("eunit/include/eunit.hrl").
32 -include_lib("sinan/include/sinan.hrl").
34 %%====================================================================
35 %% Types
36 %%====================================================================
38 -type key() :: term().
39 -type value() :: term().
40 -opaque state() :: any().
42 %%====================================================================
43 %% API
44 %%====================================================================
46 %% @doc Create a new empty config
47 -spec new() -> state().
48 new() ->
49 dict:new().
51 %% @doc Add a key to the state.
52 -spec store(key(), value(), state()) -> state().
53 store(Key, Value, State) ->
54 dict:store(Key, Value, State).
56 %% @doc Store a list of key value pairs into the state
57 -spec store(KeyValuePairs::[{string(), term()}], state()) ->
58 state().
59 store(KeyValuePairs, State) when is_list(KeyValuePairs) ->
60 lists:foldl(fun ({Key, Value}, Dict) ->
61 dict:store(Key, Value, Dict)
62 end, State, KeyValuePairs).
64 %% @doc Get a value from the state.
65 -spec get_value(key(), state()) -> value() | undefined.
66 get_value(Key, State) ->
67 case dict:find(Key, State) of
68 error ->
69 undefined;
70 {ok, Value} when is_binary(Value) ->
71 binary_to_list(Value);
72 {ok, Value} ->
73 Value
74 end.
76 %% @doc Attempts to get the specified key. If the key doesn't exist it
77 %% returns the requested default instead of just undefined.
78 -spec get_value(key(), value(), state()) -> value().
79 get_value(Key, DefaultValue, State) ->
80 case get_value(Key, State) of
81 undefined ->
82 DefaultValue;
83 Value ->
84 Value
85 end.
87 %% @doc Delete a value from the state.
88 -spec delete(key(), state()) -> state().
89 delete(Key, State) ->
90 dict:erase(Key, State).
92 project_app_by_name(AppName, State) ->
93 ProjectApps = sin_state:get_value(project_apps, State),
94 case ec_lists:search(fun(App=#app{name=Name}) ->
95 case Name == AppName of
96 true ->
97 {ok, App};
98 _ ->
99 not_found
101 end, ProjectApps) of
102 {ok, ProjApp, _} ->
103 ProjApp;
104 _ ->
105 not_found
106 end.
108 %% @doc Get the complete state as key,value pairs
109 -spec get_pairs(state()) -> [{key(), value()}].
110 get_pairs(State) ->
111 dict:to_list(State).
113 %% @doc Add a run time error occurance to the state
114 -spec add_run_error(atom(), term(), state()) -> state().
115 add_run_error(Task, Error, State) ->
116 CurrentErrors = get_value(run_errors, [], State),
117 store(run_errors, [{Task, Error} | CurrentErrors], State).
119 %% @doc return the list of run errors in the state
120 -spec get_run_errors(state()) -> [term()].
121 get_run_errors(State) ->
122 get_value(run_errors, [], State).
124 %% @doc Add a run time warning occurance to the state
125 -spec add_run_warning(atom(), term(), state()) -> state().
126 add_run_warning(Task, Warning, State) ->
127 CurrentWarnings = get_value(run_warnings, [], State),
128 store(run_warnings, [{Task, Warning} | CurrentWarnings], State).
130 %% @doc return the list of run warnings in the state
131 -spec get_run_warnings(state()) -> [term()].
132 get_run_warnings(State) ->
133 get_value(run_warnings, [], State).
135 %% @doc Format an exception thrown by this module
136 -spec format_exception(sin_exceptions:exception()) ->
137 string().
138 format_exception(Exception) ->
139 sin_exceptions:format_exception(Exception).
141 %%====================================================================
142 %% Internal Functions
143 %%====================================================================