1 %% -*- mode: Erlang; fill-column: 80; comment-column: 75; -*-
2 %%%---------------------------------------------------------------------------
3 %%% @author Eric Merritt <ericbmerritt@gmail.com>
4 %%% @copyright 2006, 2011 Erlware
6 %%% An abstract storage and management piece for build related state
9 %%%----------------------------------------------------------------------------
19 project_app_by_name
/2,
31 -include_lib("eunit/include/eunit.hrl").
32 -include_lib("sinan/include/sinan.hrl").
34 %%====================================================================
36 %%====================================================================
38 -type
key() :: term().
39 -type
value() :: term().
40 -opaque
state() :: any().
42 %%====================================================================
44 %%====================================================================
46 %% @doc Create a new empty config
47 -spec
new() -> state().
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()) ->
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
70 {ok
, Value
} when is_binary(Value
) ->
71 binary_to_list(Value
);
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
87 %% @doc Delete a value from the state.
88 -spec
delete(key(), state()) -> 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
108 %% @doc Get the complete state as key,value pairs
109 -spec
get_pairs(state()) -> [{key(), value()}].
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()) ->
138 format_exception(Exception
) ->
139 sin_exceptions:format_exception(Exception
).
141 %%====================================================================
142 %% Internal Functions
143 %%====================================================================