Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Created February 17, 2018 03:41
Show Gist options
  • Save elbow-jason/50be3fbc78add0abc0ddd8eb9b6e68c0 to your computer and use it in GitHub Desktop.
Save elbow-jason/50be3fbc78add0abc0ddd8eb9b6e68c0 to your computer and use it in GitHub Desktop.
Breads
defmodule Breadpacket do
use GenServer
def start_link(%{packet_state: state, packet_name: name}) do
GenServer.start_link(__MODULE__, state, name: name)
end
def init(state) do
{:ok, state}
end
def handle_call(:eat_slice, _from, [slice | remaining_slices]) do
{:reply, slice, remaining_slices}
end
def handle_cast({:add_slice, slice}, state) do
{:noreply, [slice | state]}
end
end
defmodule Breadshop do
def create_packet do
{:ok, packet} = Breadpacket.Supervisor.start_link(%{
supervisor_name: :ramu_sup,
packet_name: :ramu,
packet_state: [:slice_one, :slice_two],
})
packet
end
def create_packet(params) do
{:ok, packet} = Breadpacket.Supervisor.start_link(params)
packet
end
def params_one do
%{
supervisor_name: :ramu_sup,
packet_name: :ramu,
packet_state: [:slice_one, :slice_two],
}
end
def params_two do
%{
supervisor_name: :another_sup,
packet_name: :another,
packet_state: [:slice_three, :slice_four],
}
end
end
defmodule Breadpacket.Supervisor do
use Supervisor
def start_link(%{supervisor_name: name} = params) do
Supervisor.start_link(__MODULE__, params, name: name)
end
# callbacks
def init(params) do
children = [
{Breadpacket, params}
]
Supervisor.init(children, strategy: :one_for_one)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment