设置Elixir软件包的默认应用程序配置 [英] Set default application configs for Elixir packages

查看:51
本文介绍了设置Elixir软件包的默认应用程序配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Elixir软件包,我想指定一个默认的应用程序配置(用户可以通过在其 config.exs 中指定自定义值来覆盖它)。我最初将它们放在项目的 config.exs 中,直到我意识到依赖该库的项目不会加载配置文件。

I'm writing an Elixir package, and I want to specify a default application configuration (that the user can override by specifying custom values in their config.exs). I was originally putting them in my project's config.exs until I realized that the config file won't be loaded for projects that depend on this library.

配置文件本身告诉您:


此配置在任何依赖项之前加载且受限制
这个项目。如果另一个项目依赖于此项目,则该
文件将不会加载,也不会影响父项目。因此,如果
要为
第三方用户提供应用程序的默认值,则应在 mix.exs文件中完成。

This configuration is loaded before any dependency and is restricted to this project. If another project depends on this project, this file won't be loaded nor affect the parent project. For this reason, if you want to provide default values for your application for 3rd-party users, it should be done in your "mix.exs" file.






我一直在努力了解如何在 mix.exs中指定应用程序默认值并使用它们。我当前的解决方案是使用 Application.get_env / 3 具有默认参数,但对我来说似乎不对,因为应用程序默认值会分散在整个代码中。


I've been struggling to understand how to specify application defaults in my mix.exs and use them. My current solution is to use Application.get_env/3 with a default argument but that doesn't seem right to me as the application defaults would be scattered through out the code.

Application.get_env(:my_library, :arg, "default value")

所以,如何在 mix.exs 中指定应用程序默认值?

So, How can I specify application defaults in mix.exs?

推荐答案

您可以在 mix.exs 中为应用程序设置默认配置值-当在另一个项目中用作依赖项时,这些值也将可用。例如:

You can set default config values for your application in mix.exs - these will also be available when used as dependency in another project. For example:

def applications do
  [applications: [:logger, ...],
   mod: {MyLibrary.Application, []},
   env: [arg: "default value"]]
end

这篇关于设置Elixir软件包的默认应用程序配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆