Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow modules to define collections of responders #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/hedwig/robot.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ defmodule Hedwig.Robot do
opts = robot.config(opts)
aka = Keyword.get(opts, :aka)
name = Keyword.get(opts, :name)
{responders, opts} = Keyword.pop(opts, :responders, [])
{responder_list, opts} = Keyword.pop(opts, :responders, [])
responders = collect_responders(responder_list)

unless responders == [] do
GenServer.cast(self(), {:install_responders, responders})
Expand Down Expand Up @@ -199,6 +200,18 @@ defmodule Hedwig.Robot do
{:ok, state}
end

def collect_responders([]), do: []
def collect_responders([{module, list_fun, opts} | responders])
when is_atom(list_fun) do
collection = apply(module, list_fun, [opts])
|> collect_responders()

collection ++ collect_responders(responders)
end
def collect_responders([{module, opts} = responder | responders]) do
[responder] ++ collect_responders(responders)
end

defp log_incorrect_return(atom) do
Logger.warn """
#{inspect atom} return value from `handle_in/2` only works with `%Hedwig.Message{}` structs.
Expand Down
19 changes: 19 additions & 0 deletions test/hedwig/robot_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,25 @@ defmodule Hedwig.RobotTest do
assert [{TestResponder, []}] = Hedwig.Robot.responders(robot)
end

def collection_fill_opts(opts), do: [{A, opts}, {B, opts}]

@tag start_robot: true, responders: [{Hedwig.RobotTest, :collection_fill_opts, [foo: "bar"]}]
test "collect_responders/1 expands responder list with common options", %{robot: robot} do
assert [{A, [foo: "bar"]}, {B, [foo: "bar"]}] = Hedwig.Robot.responders(robot)
end

def collection_empty_opts(_opts), do: [{A, []}, {B, []}]

@tag start_robot: true, responders: [{Hedwig.RobotTest, :collection_empty_opts, [foo: "bar"]}]
test "collect_responders/1 expands responder list", %{robot: robot} do
assert [{A, []}, {B, []}] = Hedwig.Robot.responders(robot)
end

@tag start_robot: true, responders: [{TestResponder, []}, {Hedwig.RobotTest, :collection_empty_opts, []}]
test "collect_responders/1 includes normal responder definition at start", %{robot: robot} do
assert [{TestResponder, []}, {A, []}, {B, []}] = Hedwig.Robot.responders(robot)
end

@tag start_robot: true
test "handle_connect/1", %{robot: robot} do
assert ^robot = :global.whereis_name("hedwig")
Expand Down