Skip to content

Instantly share code, notes, and snippets.

@Sleepful
Created November 14, 2022 18:10
Show Gist options
  • Save Sleepful/71e665d5081456bccc4c7261675c5c98 to your computer and use it in GitHub Desktop.
Save Sleepful/71e665d5081456bccc4c7261675c5c98 to your computer and use it in GitHub Desktop.
# the join tables, many_to_many between:
#
# - categories_type(s)
# - filter
#
# - categories_type(s)
# - word
#
# type(s) refer to: bool, text, multi, datetime
#
# these also has_many for:
#
# - word_categories_type(s)
# - filter_categories_type(s)
defmodule WordBase.Classifier.Join.Compile do
@kinds [
%{string: "filters", atom: :Filter},
%{string: "words", atom: :Word}
]
@types [
%{table: "bool", module: :Bool},
%{table: "multi", module: :Multi},
%{table: "text", module: :Text},
%{table: "datetime", module: :Datetime}
]
def all_types() do
Enum.flat_map(@types, fn t ->
Enum.map(@kinds, fn k ->
Map.put(t, :kind, k)
end)
end)
end
def module_name(module, kind) do
String.to_atom("#{module}.#{kind}")
end
def atom(string), do: String.to_atom(string)
end
defmodule WordBase.Classifier.Join do
import __MODULE__.Compile
for %{table: table, module: module, kind: %{string: kind_string, atom: kind_atom}} <-
all_types() do
# e.g. __MODULE__.Bool.Filter
defmodule atom("#{__MODULE__}.#{module}.#{kind_atom}") do
use Ecto.Schema
import Ecto.Changeset
schema "#{kind_string}_categories_join_#{table}" do
# has_many atom("#{kind_string}_categories_#{table}"), module_name(WordBase.Classifier.Value, module)
belongs_to atom("#{kind_string}"), WordBase.Modifier.Filter
belongs_to atom("categories_#{kind_string}"),
module_name(WordBase.Classifier.Type, module)
timestamps()
end
def changeset(_schema, _attrs) do
end
IO.puts("Module declared:") # this prints during compilation
IO.inspect(__MODULE__)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment