Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Model Support Function Extension
defmodule Nectar.ModelExtension do
defmacro __using__(_opts) do
quote do
Module.register_attribute(__MODULE__, :method_block, accumulate: true)
import Nectar.ModelExtension, only: [include_method: 1]
end
end
defmacro include_method([do: block]) do
support_fn = Macro.escape(block)
quote bind_quoted: [support_fn: support_fn] do
Module.put_attribute(__MODULE__, :method_block, support_fn)
end
end
end
defmodule Nectar.ExtendProduct do
use Nectar.ModelExtension
defmacro __using__(_opts) do
quote do
@before_compile Nectar.ExtendProduct
end
end
include_method do: (def fn_from_outside, do: "support function")
defmacro __before_compile__(_env) do
@method_block
end
end
defmodule Nectar.Product do
...
# Add new support function here
use Nectar.ExtendProduct
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.