Created
January 10, 2018 14:58
-
-
Save jakobdamjensen/d31de5e0291b405cec28f9d5ea15036c 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 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 |
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 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