Is it possible to generate the module name as an atom within a macro?
defmodule MyProject.Schema do
defmacro __using__(opts) do
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query
def base_query(), do: from(__MODULE__, as: :?????)
# common functions used by most schemas ...
end
end
The base_query()
would ideally resolve to:
def base_query(), do: from(MyProject.Customer, as: :customer)
Thanks!
Not sure I get your goal, do you need a function that takes e.g. MyProject.Customer
as an argument and returns :customer
? And also takes MyApp.Auth.Users
and returns :users
, as another example?
Or are you asking a question about how to get current module name in macros?
defmacro __using__(schema: schema) do # opts
quote do
use Ecto.Schema
import Ecto
import Ecto.Changeset
import Ecto.Query
def base_query(), do: from(__MODULE__, as: unquote(schema))# unquote(opts[:schema]))
end
end
Then you can use it like:
use MyProject.Schema, schema: :some_name
3 Likes
I am searching for a way to get the module name as an atom. For example: :projects
from MyProject.SomeModule.SomeOthermodule.Projects
. I think @kartheek’s solution is probably the most appropriate for my use case however.