Last active
November 2, 2019 11:38
-
-
Save cblavier/4fcf28c4d8eba3655fc87de02c1db8de to your computer and use it in GitHub Desktop.
This file contains 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} -> | |
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 |
This file contains 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
Fixed issue with 0-arity functions