How to use anonymous functions in configuration files?

You may also want to borrow this from a typical Phoenix project.

In mix.exs:

  def project do
    [
      app: :form_demo,
      version: "0.1.0",
      elixir: "~> 1.5",
      elixirc_paths: elixirc_paths(Mix.env()),
      compilers: [:phoenix, :gettext] ++ Mix.compilers(),
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end
]

In particular elixirc_paths. The elixirc_paths/1 function returns the compilation paths based on the mix environment. Further down:

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

So for :test test/support is added to the compilation path. So all .ex files only used for testing go under test/support and those modules are then available for all the .exs testing scripts.

In combination with values set in config.exs and compile time use of Application.fetch_env!/2 the appropriate module can then be selected (example).

1 Like