Packages

A wrapper for `Application.get_config/3` and `System.get_env/1` that makes it easy to swap in process-specific overrides. Among other things, this allows tests to provide async-safe overrides.

Current section

13 Versions

Jump to

Compare versions

5 files changed
+37 additions
-12 deletions
  @@ -15,4 +15,4 @@
15 15 [{<<"GitHub">>,<<"https://github.com/livinginthepast/elixir-gestalt">>}]}.
16 16 {<<"name">>,<<"gestalt">>}.
17 17 {<<"requirements">>,[]}.
18 - {<<"version">>,<<"1.0.0">>}.
18 + {<<"version">>,<<"1.0.1">>}.
  @@ -91,11 +91,9 @@ defmodule Gestalt do
91 91 def copy(from_pid, to_pid, agent \\ __MODULE__)
92 92
93 93 def copy(from_pid, to_pid, agent) when is_pid(from_pid) and is_pid(to_pid) do
94 - case GenServer.whereis(agent) do
95 - nil ->
96 - raise "agent not started, please call start() before changing state"
94 + unless GenServer.whereis(agent),
95 + do: raise("agent not started, please call start() before changing state")
97 96
98 - _ ->
99 97 Agent.get_and_update(agent, fn state ->
100 98 get_in(state, [from_pid])
101 99 |> case do
  @@ -104,7 +102,6 @@ defmodule Gestalt do
104 102 end
105 103 end)
106 104 end
107 - end
108 105
109 106 @doc ~S"""
110 107 Copies Gestalt overrides from one pid to another. If no overrides have been defined,
  @@ -135,6 +132,8 @@ defmodule Gestalt do
135 132 false
136 133
137 134 """
135 + @spec get_config(atom(), any(), pid()) :: any()
136 + @spec get_config(atom(), any(), pid(), module()) :: any()
138 137 def get_config(_module, _key, _pid, _agent \\ __MODULE__)
139 138
140 139 def get_config(module, key, pid, agent) when is_pid(pid) do
  @@ -161,6 +160,8 @@ defmodule Gestalt do
161 160 "no longer from env"
162 161
163 162 """
163 + @spec get_env(String.t(), pid()) :: any()
164 + @spec get_env(String.t(), pid(), module()) :: any()
164 165 def get_env(_variable, _pid, _agent \\ __MODULE__)
165 166
166 167 def get_env(variable, pid, agent) when is_pid(pid) do
  @@ -179,6 +180,8 @@ defmodule Gestalt do
179 180 @doc ~S"""
180 181 Sets an override for the provided pid, effecting the behavior of `get_config/4`.
181 182 """
183 + @spec replace_config(atom(), any(), any(), pid()) :: :ok
184 + @spec replace_config(atom(), any(), any(), pid(), module()) :: :ok
182 185 def replace_config(_module, _key, _value, _pid, _agent \\ __MODULE__)
183 186
184 187 def replace_config(module, key, value, pid, agent) when is_pid(pid) do
  @@ -204,6 +207,8 @@ defmodule Gestalt do
204 207 @doc ~S"""
205 208 Sets an override for the provided pid, effecting the behavior of `get_env/3`.
206 209 """
210 + @spec replace_env(String.t(), any(), pid()) :: :ok
211 + @spec replace_env(String.t(), any(), pid(), module()) :: :ok
207 212 def replace_env(_variable, _value, _pid, _agent \\ __MODULE__)
208 213
209 214 def replace_env(variable, value, pid, agent) when is_pid(pid) do
  @@ -230,7 +235,9 @@ defmodule Gestalt do
230 235
231 236 defp get_agent_config(agent, caller_pid, module, key) do
232 237 Agent.get(agent, fn state ->
233 - case get_in(state, [caller_pid, :configuration]) do
238 + get_in(state, [caller_pid, :configuration])
239 + end)
240 + |> case do
234 241 nil ->
235 242 Application.get_env(module, key)
236 243
  @@ -240,12 +247,13 @@ defmodule Gestalt do
240 247 true -> get_in(override, [module, key])
241 248 end
242 249 end
243 - end)
244 250 end
245 251
246 252 defp get_agent_env(agent, caller_pid, variable) when is_binary(variable) do
247 253 Agent.get(agent, fn state ->
248 - case get_in(state, [caller_pid, :env]) do
254 + get_in(state, [caller_pid, :env])
255 + end)
256 + |> case do
249 257 nil ->
250 258 System.get_env(variable)
251 259
  @@ -255,6 +263,5 @@ defmodule Gestalt do
255 263 true -> override[variable]
256 264 end
257 265 end
258 - end)
259 266 end
260 267 end
  @@ -4,6 +4,7 @@ defmodule Mix.Tasks.Gestalt.Tags.Create do
4 4 use Mix.Task
5 5
6 6 @shortdoc "Creates a git tag"
7 + @impl Mix.Task
7 8 def run([]) do
8 9 start_app!()
  @@ -4,6 +4,7 @@ defmodule Mix.Tasks.Gestalt.Tags.Push do
4 4 use Mix.Task
5 5
6 6 @shortdoc "Pushes all git tags"
7 + @impl Mix.Task
7 8 def run([]) do
8 9 Mix.Shell.IO.cmd(command() |> Enum.join(" "))
9 10 end
  @@ -1,7 +1,7 @@
1 1 defmodule Gestalt.MixProject do
2 2 use Mix.Project
3 3
4 - @version "1.0.0"
4 + @version "1.0.1"
5 5
6 6 def project do
7 7 [
  @@ -9,6 +9,7 @@ defmodule Gestalt.MixProject do
9 9 app: :gestalt,
10 10 deps: deps(),
11 11 description: description(),
12 + dialyzer: dialyzer(),
12 13 docs: [
13 14 extras: extras(),
14 15 source_ref: "v#{@version}",
  @@ -29,12 +30,20 @@ defmodule Gestalt.MixProject do
29 30
30 31 defp aliases do
31 32 [
32 - "hex.publish": ["gestalt.tags.create", "gestalt.tags.push", "hex.publish"]
33 + "hex.publish": [
34 + "credo",
35 + "dialyzer --quiet --format short",
36 + "gestalt.tags.create",
37 + "gestalt.tags.push",
38 + "hex.publish"
39 + ]
33 40 ]
34 41 end
35 42
36 43 defp deps do
37 44 [
45 + {:credo, ">= 0.0.0", only: [:dev, :test], runtime: false},
46 + {:dialyxir, ">= 0.0.0", only: [:dev, :test], runtime: false},
38 47 {:ex_doc, ">= 0.0.0", only: :dev},
39 48 {:junit_formatter, ">= 0.0.0", only: :test}
40 49 ]
  @@ -48,6 +57,13 @@ defmodule Gestalt.MixProject do
48 57 """
49 58 end
50 59
60 + defp dialyzer do
61 + [
62 + plt_add_apps: [:ex_unit, :mix],
63 + plt_add_deps: :app_tree
64 + ]
65 + end
66 +
51 67 defp extras() do
52 68 [
53 69 "pages/overview.md"