Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active December 24, 2018 10:33
Show Gist options
  • Save blackode/ae01f05783182ec3d5ecd762253bcb04 to your computer and use it in GitHub Desktop.
Save blackode/ae01f05783182ec3d5ecd762253bcb04 to your computer and use it in GitHub Desktop.
demo_server version 2
defmodule DemoServer do
  use GenServer
- @vsn "1"
+ @vsn "2"

## Client API
def start_link employee do
      GenServer.start_link __MODULE__, employee, []
  end
 
  def add_money(pid, value) do
    GenServer.call(pid, {:add, value})
  end
## Server API
  def init(employee) do   # initiating state with passed employee details
    {:ok, employee}
  end

  # add the value to the state and returns :ok
- def handle_call({:add, value},_from, %{name: name, money: money} = state) do
+ def handle_call({:add, value},_from, %{name: name, salary: salary} = state) do

- {:reply, "#{value} added to #{name} ", Map.put(state, :money, money+value)}
+ new_state = Map.put(state, :salary, salary+value)
+ {:reply, new_state, new_state}

  end
  
+  def code_change("1", %{name: name, money: money} = old_state, _dontcare) do
+     {:ok, %{name: name, salary: money}}
+  end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment