Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Phoenix Router Extension
## Library Code
## Defines DSL to be used by Service Code
## in order to get properly consumed by Consume
defmodule Nectar.RouterExtension do
defmacro __using__(_opts) do
quote do
Module.register_attribute(__MODULE__, :defined_routes, accumulate: true)
import Nectar.RouterExtension, only: [define_route: 1]
@before_compile Nectar.RouterExtension
end
end
# Part of Route Addition DSL
defmacro define_route([do: block]) do
route = Macro.escape(block)
quote bind_quoted: [route: route] do
Module.put_attribute(__MODULE__, :defined_routes, route)
end
end
defmacro __before_compile__(_env) do
quote do
defmacro mount do
@defined_routes
end
end
end
end
## Service Code
defmodule Nectar.ExtendRouter do
## Make Service Code available to be consumed
## through Library Code
use Nectar.RouterExtension
define_route do: get "/favorites", FavoriteProducts.FavoriteController, :index
define_route do: post "/favorites", FavoriteProducts.FavoriteController, :create
end
defmodule Nectar.Router do
...
# Add Route Here
# get "/favorites", FavoriteProducts.FavoriteController, :index
## Service Consumer Code
require Nectar.ExtendRouter
Nectar.ExtendRouter.mount
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment