Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Configuration in Elixir

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

Configuration in Elixir

Avatar for Lukas Rieder

Lukas Rieder

August 09, 2018
Tweet

More Decks by Lukas Rieder

Other Decks in Education

Transcript

  1. Do not: Read configuration at compile time defmodule Foo do

    @config Application.get_env(:foo_app, :config) end
  2. Do: Read configuration at runtime defmodule Foo do def config()

    do Application.get_env(:foo_app, :config) end end
  3. Stateful: Allow User-modules and define otp_app use Mix.Config config :config_app,

    ConfigApp.Repo,
 adapter: Ecto.Adapters.Postgres,
 # …
  4. Stateful: Allow User-modules and define otp_app defmodule Ecto.Repo do defmacro

    __using__(opts) do quote bind_quoted: [opts: opts] do @otp_app opts[:otp_app]
 # … end end end
  5. use Mix.Config config :app_name, AppName, where: "elixir_ug", who: "lukas"
 


    config :app_name, AppName.Conf, where: "elixir_conf"
  6. GetConf.get_conf(:app_name, AppName, :where)
 # => "elixir_ug"
 GetConf.get_conf(:app_name, AppName.Foo, :where)
 #

    => "elixir_conf"
 GetConf.get_conf(:app_name, AppName.Foo, :who)
 # => "lukas"
  7. defmodule TestModule use GetConf, otp_app: :app_name def where(), do: get_conf(:where)

    end TestModule.where()
 # => "elixir_ug" 
 TestModule.get_conf(:where)
 # => "elixir_ug"