Compare commits

...

2 Commits

Author SHA1 Message Date
Liru 0425add7f7 Turn Aife into an application
continuous-integration/drone/push Build is passing Details
2019-05-06 15:36:04 -04:00
Liru 213587a889 Add multiple drone functionalities 2019-05-06 15:35:12 -04:00
7 changed files with 106 additions and 8 deletions

15
lib/aife/application.ex Normal file
View File

@ -0,0 +1,15 @@
defmodule Aife.Application do
@moduledoc false
use Application
def start(_, _) do
children = [
Aife.Drone.Supervisor
]
opts = [strategy: :one_for_one, name: Aife.Supervisor]
Supervisor.start_link(children, opts)
end
end

View File

@ -16,6 +16,12 @@ defmodule Aife.Drone do
"""
@moduledoc since: "0.1.0"
alias Aife.Drone
use GenServer
defstruct enabled?: false, human: %{traits: %{}}, drone: %{traits: %{}}, values: %{}
@doc """
Sends a command to a drone.
@ -23,6 +29,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def command(drone, cmd) do
GenServer.cast(drone, {:command, cmd})
end
@doc """
@ -34,6 +41,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def enable(drone) do
GenServer.cast(drone, :enable)
end
@doc """
@ -43,6 +51,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def disable(drone) do
GenServer.cast(drone, :disable)
end
@doc """
@ -50,6 +59,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def traits(drone) do
GenServer.call(drone, :traits)
end
@doc """
@ -69,6 +79,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def alter_traits(drone, form \\ :current, values \\ []) do
GenServer.cast(drone, {:alter, form, values})
end
@doc """
@ -82,10 +93,11 @@ defmodule Aife.Drone do
Aife.Drone.get_values(drone, :fetishes)
#=> [fetishes: [:maid, :latex, :hypnosis]]
Aife.Drone.insert_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :haigure, :hypnosis]]}
#=> [fetishes: [:maid, :latex, :haigure, :hypnosis]]
"""
@doc since: "0.1.0"
def insert_value(drone, key, value) do
GenServer.call(drone, {:insert_value, key, value})
end
@doc """
@ -101,12 +113,13 @@ defmodule Aife.Drone do
Aife.Drone.get_values(drone, :fetishes)
#=> [fetishes: [:maid, :latex, :haigure, :hypnosis]]
Aife.Drone.remove_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :hypnosis]]}
#=> [fetishes: [:maid, :latex, :hypnosis]]
Aife.Drone.remove_value(drone, :fetishes, :haigure)
#=> {:ok, [fetishes: [:maid, :latex, :hypnosis]]}
#=> [fetishes: [:maid, :latex, :hypnosis]]
"""
@doc since: "0.1.0"
def remove_value(drone, key, value) do
GenServer.call(drone, {:remove_value, key, value})
end
@doc """
@ -116,6 +129,7 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def say(drone, text) do
GenServer.cast(drone, {:say, text})
end
@doc """
@ -132,5 +146,45 @@ defmodule Aife.Drone do
"""
@doc since: "0.1.0"
def think(drone, thought) do
GenServer.cast(drone, {:think, thought})
end
## GenServer calls.
def start_link() do
GenServer.start_link(__MODULE__, [])
end
@impl GenServer
def init(_) do
{:ok, %Drone{}}
end
@impl GenServer
def handle_call(:traits, _from, state) do
form =
case state.enabled? do
true -> :drone
false -> :human
end
{:reply, state[form].traits, state}
end
def handle_call({:insert_value, key, value}, _from, state) do
# TODO
{:reply, state, state}
end
def handle_call({:remove_value, key, value}, _from, state) do
# TODO
{:reply, state, state}
end
@impl GenServer
def handle_cast({:command, _cmd}, state), do: {:noreply, state}
def handle_cast(:disable, state), do: {:noreply, %{state | enabled?: false}}
def handle_cast(:enable, state), do: {:noreply, %{state | enabled?: true}}
def handle_cast({:say, _text}, state), do: {:noreply, state}
def handle_cast({:think, _thought}, state), do: {:noreply, state}
end

View File

@ -0,0 +1,12 @@
defmodule Aife.Drone.DynamicSupervisor do
use DynamicSupervisor
def start_link(_) do
DynamicSupervisor.start_link(__MODULE__, nil, name: __MODULE__)
end
@impl DynamicSupervisor
def init(_init_arg) do
DynamicSupervisor.init(strategy: :one_for_one)
end
end

View File

@ -0,0 +1,16 @@
defmodule Aife.Drone.Supervisor do
use Supervisor
def start_link(_) do
Supervisor.start_link(__MODULE__, [])
end
def init(_) do
children = [
{Registry, name: Aife.Drone.Registry, keys: :unique},
{DynamicSupervisor, name: Aife.Drone.DynamicSupervisor, strategy: :one_for_one}
]
Supervisor.init(children, strategy: :one_for_one)
end
end

View File

@ -26,7 +26,8 @@ defmodule Aife.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
extra_applications: [:logger],
mod: {Aife.Application, []}
]
end

4
test/aife/drone_test.exs Normal file
View File

@ -0,0 +1,4 @@
defmodule Aife.DroneTest do
use ExUnit.Case
doctest Aife.Drone
end

View File

@ -1,8 +1,4 @@
defmodule AifeTest do
use ExUnit.Case
doctest Aife
test "greets the world" do
assert Aife.hello() == :world
end
end