Sha256: c4e22211af54e921c176a1042b26773424a2aecbab5e880adb95936d8fa5a38e
Contents?: true
Size: 1.78 KB
Versions: 136
Compression:
Stored size: 1.78 KB
Contents
-module(example). -export([ balance/1, charge/2, close/1, create/0, deposit/2, withdraw/2, test_version/0 ]). -export([ init/1, handle_call/3, handle_cast/2 ]). -behaviour(gen_server). -record(account, {pid}). test_version() -> 1. %%% Public API create() -> {ok, PID} = gen_server:start(?MODULE, [], []), #account{pid=PID}. balance(#account{pid=PID}) -> gen_server:call(PID, balance, infinity). charge(#account{pid=PID}, Amount) -> gen_server:call(PID, {charge, Amount}, infinity). close(#account{pid=PID}) -> gen_server:call(PID, close, infinity). deposit(#account{pid=PID}, Amount) -> gen_server:call(PID, {deposit, Amount}, infinity). withdraw(#account{pid=PID}, Amount) -> gen_server:call(PID, {withdraw, Amount}, infinity). %%% gen_server API init([]) -> {ok, 0}. handle_call(_, _From, closed) -> {reply, {error, account_closed}, closed}; handle_call(close, _From, Balance) -> {reply, Balance, closed}; handle_call(balance, _From, Balance) -> {reply, Balance, Balance}; handle_call({deposit, Amount}, _From, Balance) when Amount > 0 -> {reply, Amount, Balance + Amount}; handle_call({deposit, _}, _From, Balance) -> {reply, Balance, Balance}; handle_call({withdraw, Amount}, _From, Balance) when Amount > Balance -> {reply, Balance, 0}; handle_call({withdraw, Amount}, _From, Balance) when Amount > 0 -> Balance1 = Balance - Amount, {reply, Amount, Balance1}; handle_call({withdraw, _}, _From, Balance) -> {reply, 0, Balance}; handle_call({charge, Amount}, _From, Balance) when Amount > Balance -> {reply, 0, Balance}; handle_call({charge, Amount}, _From, Balance) when Amount > 0 -> {reply, Amount, Balance - Amount}; handle_call({charge, _}, _From, Balance) -> {reply, 0, Balance}. handle_cast(_, Balance) -> {noreply, Balance}.
Version data entries
136 entries across 136 versions & 1 rubygems