Skip to content

Instantly share code, notes, and snippets.

@cblavier
Last active November 2, 2019 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cblavier/4fcf28c4d8eba3655fc87de02c1db8de to your computer and use it in GitHub Desktop.
Save cblavier/4fcf28c4d8eba3655fc87de02c1db8de 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} ->
case arity do
0 -> defdelegate unquote(fun)(), to: mod
_ ->
values = Enum.map(1..arity, &(Macro.var(:"arg#{&1}", mod)))
defdelegate unquote(fun)(unquote_splicing(values)), to: mod
end
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
@cblavier
Copy link
Author

cblavier commented Mar 8, 2018

Fixed issue with 0-arity functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment