Thanks guys, I’m really liking where this has gone! So, I’ve tried to implement both of those ideas, and I now have
defmodule API.Const do
@root_dir File.cwd!
@const_dir Path.join(~w(#{@root_dir} lib imsapi common constants))
@const_files Path.wildcard("#{@const_dir}/**/*.json")
for filename <- @const_files do
# Watch for changes and recompile
@external_resource filename
# This will be the new module: API.Const.<group>
group = filename
|> Path.basename(".json")
|> String.capitalize
file_consts = File.read!(filename)
|> Poison.decode!
defmodule Module.concat([API, Const, group]) do
for {key, value} <- file_consts do
def encode(unquote(String.to_atom(key))), do: unquote(value)
def decode(unquote(value)), do: unquote(String.to_atom(key))
end
end
end
end
That makes a much nicer way to access the different files, so thanks for that suggestion @sasajuric! I’m not sure if I’m doing something wrong, but it doesn’t seem to recompile if I modify one of the JSON files?
I’m running it using iex -S mix phoenix.server


















