Last active
July 8, 2019 13:15
-
-
Save nathan-cruz77/653a161f9b6dcf6548a8726912f1e00b to your computer and use it in GitHub Desktop.
Dynamic proxy pass in elixir
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 ProxyPass do | |
@docs """ | |
Dynamically delegate function calls to another module. | |
Usage: | |
defmodule RepoA do | |
def insert(), do: IO.puts("Called from RepoA") | |
end | |
defmodule RepoB do | |
def insert(), do: IO.puts("Called from RepoB") | |
end | |
defmodule Repo do | |
use ProxyPass, to: get_module() | |
def get_module do | |
if System.get_env("USE_MODULE_A") == "true", do: RepoA, else: RepoB | |
end | |
end | |
Repo.insert() | |
""" | |
defmacro __using__(to: target) do | |
quote do | |
def unquote(:"$handle_undefined_function")(func, args) do | |
apply(unquote(target), func, args) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment