Skip to content

Instantly share code, notes, and snippets.

@saa
Forked from stolen/gs_up.erl
Created March 13, 2014 12:57
Show Gist options
  • Save saa/9527995 to your computer and use it in GitHub Desktop.
Save saa/9527995 to your computer and use it in GitHub Desktop.
-module(gs_up).
-export([init/1, handle_call/3, terminate/2]).
-record(state, {
own_fields,
field1,
field2,
field3
}).
init([F1, F3]) ->
{ok, #state{own_fields = record_info(fields, state),
field1 = F1, field3 = F3}}.
terminate(_,_) -> ok.
handle_call(Call, From, State) ->
NewState = case element(2, State) == record_info(fields, state) of
true -> State;
false -> migrate(State)
end,
do_handle_call(Call, From, NewState).
do_handle_call(_, _, #state{} = State) ->
{reply, state_to_pl(State), State}.
state_to_pl(State) when is_tuple(State) ->
Fields = element(2, State),
tl(lists:zip(Fields, tl(tuple_to_list(State)))).
state_from_pl(PL) ->
ZeroState = #state{own_fields = record_info(fields, state)},
FieldsToInherit = state_to_pl(ZeroState),
InheritedValues = [proplists:get_value(Field, PL, Default) || {Field, Default} <- FieldsToInherit],
list_to_tuple([element(1, ZeroState), element(2, ZeroState) | InheritedValues]).
migrate(OldState) ->
state_from_pl(state_to_pl(OldState)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment