I have a module for which I want to create a function rule_ids/0 at compile time. The code collects the rule ids at compile time from the first argument of all clauses of the function condition/3 using the AST.
It does not work because the variable rule_ids_ defined at compile time is not accessible when defining the function with def rule_ids(), do: rule_ids_.
How could I proceed to get it working? Should I use a macro somewhere?
defmodule Rules do
# Collect the rule ids at compile time
{_ast, rule_ids_} = File.read!(__ENV__.file)
|> Code.string_to_quoted()
|> Macro.traverse([],
fn
{:def, _, [{:condition, _, [rule_id, {:v, _, nil}, {:site, _, nil}]} | _]} = x, acc -> {x, [rule_id | acc]}
other, acc -> {other, acc}
end,
fn other, acc -> {other, acc} end
)
# How could I use rule_ids_ here?
def rule_ids(), do: rule_ids_
def condition(rule_id, v, site)
def condition(1018, v, site) do
v[site][7018] < 50 && v[site][7019] < 50 && v[site][7020] < 50
end
def condition(1017, v, site) do
v[site][7027] > 42
end
# ...
end






















