? Define a macro or .hrl file in Elixir for customization

In Erlang, I can use the define macro or .hrl file to save the configuration in one place. What is the best place for this in Elixir.

I could not find an elegant way to do this. Now I am doing something like: -

def get_server_name do
    "TEST"
end

Am I missing something?

+4
source share
1 answer

If you use functions or macros, in the end, it should be too much, but if you are looking for the “save it in one place” part, I would suggest placing it in your own namespace / module

defmodule MyApp.Configuration
  def server_name do
    "foo"
  end

  # or if you prefer having it all on one line
  def host_name, do: "example.com"

  # for complete equivalency, you can use a macro
  defmacro other_config do
    "some value"
  end
end

, , , , , ,

defmodule MyApp.Server do
  alias MyApp.Configuration, as: C
end

defmodule MyApp.Server do
  import MyApp.Configuration
end
+5

All Articles