Vasiliy Ermolovich
Vasiliy Ermolovich

Reputation: 24627

Elixir's module attributes in Phoenix production

As far as I know module attributes evaluated during compilation time. I was trying to follow this post about mocking API in Elixir:

defmodule Example.User do

  @github_api Application.get_env(:example, :github_api)

  def get(username) when is_binary(username) do
    @github_api.make_request(:get, "/users/#{username}")
  end
end

And I'm wondering if that's gonna work in production at all. As far as I understand when this module is compiled there's no access to the Application. So my question is: can I use module attributes to store some config values that come from Application.get_env?

Upvotes: 0

Views: 197

Answers (1)

Navin Peiris
Navin Peiris

Reputation: 2606

You absolutely can. As long as the application was compiled using MIX_ENV specified to environment you want the application running under, and as long as that call evaluates to what you expect for that environment, it'll all work fine.

For a deeper look at how module attributes are affected by compilation for an almost identical case as what you've described, take a look at this blog post here.

Upvotes: 1

Related Questions