Packages

An opinionated way of dealing with behaviours.

Current section

Files

Jump to
ex_knigge lib knigge code delegate.ex
Raw

lib/knigge/code/delegate.ex

defmodule Knigge.Code.Delegate do
@moduledoc false
alias Knigge.Implementation
def callback_to_defdelegate({name, arity}, from: module, delegate_at_runtime?: false) do
callback_to_defdelegate({name, arity},
from: module,
to: Implementation.fetch_for!(module)
)
end
def callback_to_defdelegate({name, arity}, from: _module, delegate_at_runtime?: true) do
quote bind_quoted: [name: name, arity: arity] do
args = Macro.generate_arguments(arity, __MODULE__)
def unquote(name)(unquote_splicing(args)) do
apply(__knigge__(:implementation), unquote(name), unquote(args))
end
end
end
def callback_to_defdelegate({name, arity}, from: _module, to: implementation) do
quote bind_quoted: [name: name, arity: arity, implementation: implementation] do
args = Macro.generate_arguments(arity, __MODULE__)
defdelegate unquote(name)(unquote_splicing(args)), to: implementation
end
end
end