Skip to content

fix elixir 1.14 support #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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