Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/delx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,24 @@ defmodule Delx do
"""

defmacro __using__(opts) do
quote bind_quoted: [opts: opts] do
otp_app =
opts[:otp_app] ||
raise ArgumentError, "expected otp_app: to be given as argument"
otp_app =
opts[:otp_app] ||
raise ArgumentError, "expected otp_app: to be given as argument"

config_read =
if Version.match?(System.version(), "~> 1.10") do
quote do
require Application
Application.compile_env(unquote(otp_app), Delx, [])
end
else
quote do
Application.get_env(unquote(otp_app), Delx, [])
end
end

config = Application.get_env(otp_app, Delx, [])
quote do
config = unquote(config_read)

case Keyword.fetch(config, :mock) do
{:ok, true} ->
Expand Down
13 changes: 11 additions & 2 deletions lib/delx/defdelegate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ defmodule Delx.Defdelegate do
defmacro defdelegate(funs, opts) do
funs = Macro.escape(funs, unquote: true)

quote bind_quoted: [funs: funs, opts: opts] do
{m, f} =
cond do
Version.match?(System.version(), ">= 1.14.0") ->
{Kernel.Utils, :defdelegate_each}

true ->
{Kernel.Utils, :defdelegate}
end

quote bind_quoted: [funs: funs, opts: opts, m: m, f: f] do
target =
opts[:to] || raise ArgumentError, "expected to: to be given as argument"

for fun <- List.wrap(funs) do
{name, args, as, as_args} = Kernel.Utils.defdelegate(fun, opts)
{name, args, as, as_args} = apply(m, f, [fun, opts])

# Dialyzer may possibly complain about "No local return". So we tell him
# to stop as we're only delegating here.
Expand Down