From 253da275bdc8abe372a3b4a3407596f839e5f432 Mon Sep 17 00:00:00 2001 From: href Date: Fri, 30 Nov 2018 18:19:22 +0100 Subject: [PATCH] Pleroma.Config.delete/1 --- lib/pleroma/config.ex | 14 ++++++++++++++ test/config_test.exs | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/pleroma/config.ex b/lib/pleroma/config.ex index 15f771b6e..3876ddf1f 100644 --- a/lib/pleroma/config.ex +++ b/lib/pleroma/config.ex @@ -39,4 +39,18 @@ defmodule Pleroma.Config do def put(key, value) do Application.put_env(:pleroma, key, value) end + + def delete([key]), do: delete(key) + + def delete([parent_key | keys]) do + {_, parent} = + Application.get_env(:pleroma, parent_key) + |> get_and_update_in(keys, fn _ -> :pop end) + + Application.put_env(:pleroma, parent_key, parent) + end + + def delete(key) do + Application.delete_env(:pleroma, key) + end end diff --git a/test/config_test.exs b/test/config_test.exs index 0124544c8..837cbb30c 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -53,4 +53,19 @@ defmodule Pleroma.ConfigTest do assert Pleroma.Config.get([:instance, :config_test]) == true assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true end + + test "delete/1 with a key" do + Pleroma.Config.put([:delete_me], :delete_me) + Pleroma.Config.delete([:delete_me]) + assert Pleroma.Config.get([:delete_me]) == nil + end + + test "delete/2 with a list of keys" do + Pleroma.Config.put([:delete_me], hello: "world", world: "Hello") + Pleroma.Config.delete([:delete_me, :world]) + assert Pleroma.Config.get([:delete_me]) == [hello: "world"] + Pleroma.Config.put([:delete_me, :delete_me], hello: "world", world: "Hello") + Pleroma.Config.delete([:delete_me, :delete_me, :world]) + assert Pleroma.Config.get([:delete_me, :delete_me]) == [hello: "world"] + end end -- 2.45.2