LoginSignup
14
2

More than 1 year has passed since last update.

Elixirコードからmix.exsの中身を取得する方法

Last updated at Posted at 2022-11-18

設定を取得したいことはないでしょうか?
Elixirコードはコンパイルされるのでコンパイル時の設定と実行時の設定とを区別して考えることが大切なようです。

コンパイル時の設定

Mix.Project.config()

ドキュメントをよく読んでみると、いろいろ注意事項が書かれています。Mix.Project.config/0はコンパイル時の設定のみに使用すると考えておいた方が良さそうです。

This module contains many functions that return project information and metadata. However, since Mix is not included nor configured during releases, we recommend using the functions in this module only inside Mix tasks. If you need to configure your own app, consider using the application environment instead.

Do not use Mix.Project.config/0 to find the runtime configuration. Use it only to configure aspects of your project (like compilation directories) and not your application runtime.

  • Mixはリリース時には含まれておらず、設定もされていない
  • Mixタスクの中だけで使用することが推奨されている

実行時の設定

実行時に設定を取得する場合はApplicationモジュールの関数を利用します。

実行時に設定を読み込み

defmodule MyApp do
  def some_config do
    Application.get_env(:my_app, :some_config)
  end
end

コンパイル時に読み込み、結果をモジュール変数に書き込む

defmodule MyApp do
  @some_config Application.compile_env(:my_app, :some_config)
end

ご参考までに

14
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
2