View Source HypeLib.UseInvoker (HypeLib v2.4.1)
The HypeLib.UseInvoker
module offers a use macro for utility modules.
The macro defines a __using__
macro in the CALLING MODULE where the first argument is
expected to be an atom so we can pass it directly to the apply/3
function as second argument.
The macro supports the following options:
Name:
required_utils
Expected data type:
list(atom)
Description:
A list of function names (atoms) which should always be invoked when the macro is called.
examples
Examples
without-any-arguments
Without any arguments
iex> defmodule MyUtils do
...> use HypeLib.UseInvoker
...>
...> def num() do
...> quote do
...> def add(a, b), do: a + b
...> end
...> end
...> end
...>
...> defmodule MyConsumer do
...> use MyUtils, :num
...>
...> def my_add(a, b), do: add(a, b)
...> end
...>
...> MyConsumer.my_add(1, 2)
3
with-required_utils
With required_utils
iex> defmodule MyUtils do
...> use HypeLib.UseInvoker, required_utils: [:core]
...>
...> def core do
...> quote do
...> def core_fun(), do: "core function"
...> end
...> end
...>
...> def num() do
...> quote do
...> def add(a, b), do: a + b
...> end
...> end
...> end
...>
...> defmodule MyConsumer do
...> use MyUtils, :num
...> end
...>
...> MyConsumer.core_fun()
"core function"