From dc5ff3d01f770139c572a728917bd5fa51ba7430 Mon Sep 17 00:00:00 2001 From: Norbert Melzer Date: Fri, 4 Nov 2022 14:30:39 +0100 Subject: [PATCH 1/2] fix elixir 1.14 support --- lib/delx/defdelegate.ex | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/delx/defdelegate.ex b/lib/delx/defdelegate.ex index a3ab599..5c1d5b7 100644 --- a/lib/delx/defdelegate.ex +++ b/lib/delx/defdelegate.ex @@ -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. From 8bfe69ce2235d0f60f0ec9382eb2b7644f7a2704 Mon Sep 17 00:00:00 2001 From: Norbert Melzer Date: Mon, 7 Nov 2022 16:06:00 +0100 Subject: [PATCH 2/2] use Application.compile_env if supported by the elixir version --- lib/delx.ex | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/delx.ex b/lib/delx.ex index 96c52ba..5406be8 100644 --- a/lib/delx.ex +++ b/lib/delx.ex @@ -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} ->