Skip to content

Instantly share code, notes, and snippets.

@tamanugi
Created January 31, 2022 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamanugi/5deb02d336553cb8d8a6d17664c90e88 to your computer and use it in GitHub Desktop.
Save tamanugi/5deb02d336553cb8d8a6d17664c90e88 to your computer and use it in GitHub Desktop.
$ iex -S mix
iex(1)> B.new_hoge
:fuga
defmodule A do
@ignore_attributes [:macrocallback, :before_compile, :opaque, :spec, :behaviour, :on_definition,
:type, :dialyzer, :external_resource, :after_compile, :callback,
:derive, :compile, :optional_callbacks, :moduledoc, :typep]
defmacro __using__(_opts \\ []) do
quote do
@before_compile A
end
end
defmacro __before_compile__(env) do
Module.attributes_in(env.module)
|> Enum.reject(&(&1 in @ignore_attributes))
|> Enum.map(fn attr ->
quote do
def unquote(:"new_#{attr}")() do
unquote(Module.get_attribute(env.module, attr))
end
end
end)
end
end
defmodule B do
use A
@hoge :fuga
@moduledoc """
Documentation for `Metatest`.
"""
@doc """
Hello world.
## Examples
iex> Metatest.hello()
:world
"""
def hello do
:world
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment