Last active
July 4, 2016 15:00
-
-
Save blatyo/09fe9362e17063972dde20067807f150 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MessageRouter.DSL do | |
alias MessageRouter.Route | |
alias MessageRouter.QueueHandler | |
alias MessageRouter.Supervisor | |
defmacro __using__(_args) do | |
quote do | |
import MessageRouter.DSL | |
Module.register_attribute(__MODULE__, :routes, accumulate: true) | |
Module.register_attribute(__MODULE__, :queue_handlers, accumulate: true) | |
def routes do | |
@routes |> Enum.group_by(&(&1.queue)) | |
end | |
def queue_handlers do | |
@queue_handlers | |
end | |
end | |
end | |
defmacro route(options) do | |
key = Keyword.fetch!(options, :key) | |
queue = Keyword.fetch!(options, :to) | |
route = %Route{key: key, queue: queue} | |
quote do | |
Module.put_attribute __MODULE__, :routes, unquote(Macro.escape(route)) | |
end | |
end | |
defmacro handle(options) do | |
queue = Keyword.fetch!(options, :queue) | |
handler = Keyword.fetch!(options, :with) | |
queue_handler = %QueueHandler{queue: queue, handler: handler} | |
quote do | |
@queue_handlers unquote(Macro.escape(queue_handler)) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MessageRouter.DSLTest do | |
use ExUnit.Case, async: true | |
defmodule Router do | |
use MessageRouter.DSL | |
route(key: "source.event", to: "destination.event") | |
route(key: "other_source.event", to: "destination.event") | |
handle(queue: "destination.queue", with: Handler) | |
end | |
test ".routes" do | |
# Should be a map of queue name to routes | |
assert Router.routes == %{} | |
end | |
test ".queue_handlers" do | |
# Should be a list of handlers | |
assert Router.queue_handlers == [] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment