Skip to content

Instantly share code, notes, and snippets.

@jakobdamjensen
Created January 10, 2018 14:58
Show Gist options
  • Save jakobdamjensen/d31de5e0291b405cec28f9d5ea15036c to your computer and use it in GitHub Desktop.
Save jakobdamjensen/d31de5e0291b405cec28f9d5ea15036c to your computer and use it in GitHub Desktop.
defmodule MyApp.Facade do
defmacro facade(mod) do
quote bind_quoted: [mod: mod] do
Enum.each apply(mod, :__info__, [:functions]), fn {fun, arity} ->
values = Enum.map(1..arity, &(Macro.var(:"arg#{&1}", mod)))
defdelegate unquote(fun)(unquote_splicing(values)), to: mod
end
end
end
end
defmodule MyApp.Blog do
import MyApp.Facade
facade MyApp.Blog.PostContext
facade MyApp.Blog.CommentContext
end
# MyApp.Blog.create_post/1 -> MyApp.Blog.PostContext.create_post/1
# MyApp.Blog.create_comment/2 -> MyApp.Blog.CommentContext.create_comment/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment