Skip to content

Instantly share code, notes, and snippets.

@nathan-cruz77
Last active July 8, 2019 13:15
Show Gist options
  • Save nathan-cruz77/653a161f9b6dcf6548a8726912f1e00b to your computer and use it in GitHub Desktop.
Save nathan-cruz77/653a161f9b6dcf6548a8726912f1e00b to your computer and use it in GitHub Desktop.
Dynamic proxy pass in elixir
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