Skip to content

Commit

Permalink
Merge pull request #1 from i22-digitalagentur/ex-1-14
Browse files Browse the repository at this point in the history
Update to Elixir 1.14
  • Loading branch information
Meldanor authored Sep 11, 2023
2 parents 8510f5a + 8bfe69c commit 718609b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
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

0 comments on commit 718609b

Please sign in to comment.