Reputation: 697
I have a use-case where I want to store module_name
in my DB(Postgres). I am doing this so that later on I can retrieve the name
and call a function
defined inside the module
.
e.g.
defmodule A do
def a() do
end
end
Suppose I have a schema "xyz" I want to keep a field module name in it,
schema "xyz" do
field(:module, Ecto.Atom)
end
For now, I have kept the field to be Ecto.Atom
type.
Is it correct, if not what's the right way to do it?
Upvotes: 0
Views: 404
Reputation: 222358
You can use apply/3
to call a function on a module whose name you have as an atom.
Let's say you have Enum
atom stored in xyz
with id 123
. You can call Enum.map([1, 2, 3], &(&1 * &1))
like this:
xyz = Repo.get(Xyz, 123)
apply(xyz.module, :map, [[1, 2, 3], &(&1 * &1)]) #=> [1, 4, 9]
Upvotes: 1