diff --git a/lib/pinchflat/settings/setting_backup.ex b/lib/pinchflat/settings/setting_backup.ex deleted file mode 100644 index 5e5b43c..0000000 --- a/lib/pinchflat/settings/setting_backup.ex +++ /dev/null @@ -1,24 +0,0 @@ -defmodule Pinchflat.SettingsBackup.SettingBackup do - @moduledoc """ - A Setting is a key-value pair with a datatype used to track user-level settings. - """ - - use Ecto.Schema - import Ecto.Changeset - - schema "settings_backup" do - field :name, :string - field :value, :string - field :datatype, Ecto.Enum, values: ~w(boolean string integer float)a - - timestamps(type: :utc_datetime) - end - - @doc false - def changeset(setting, attrs) do - setting - |> cast(attrs, [:name, :value, :datatype]) - |> validate_required([:name, :value, :datatype]) - |> unique_constraint([:name]) - end -end diff --git a/lib/pinchflat/settings/settings_backup.ex b/lib/pinchflat/settings/settings_backup.ex deleted file mode 100644 index 801758b..0000000 --- a/lib/pinchflat/settings/settings_backup.ex +++ /dev/null @@ -1,95 +0,0 @@ -defmodule Pinchflat.SettingsBackup do - @moduledoc """ - The SettingsBackup context. - """ - - import Ecto.Query, warn: false - alias Pinchflat.Repo - - alias Pinchflat.SettingsBackup.SettingBackup - - @doc """ - Returns the list of settings. - - Returns [%SettingBackup{}, ...] - """ - def list_settings do - Repo.all(SettingBackup) - end - - @doc """ - Creates or updates a setting, returning the parsed value. - Raises if an unsupported datatype is used. Optionally allows - specifying the datatype. - - Returns value in type of `Ecto.Enum.mappings(Setting, :datatype)` - """ - def set!(name, value) do - set!(name, value, infer_datatype(value)) - end - - def set!(name, value, datatype) do - # Only create if doesn't exist - case Repo.get_by(SettingBackup, name: to_string(name)) do - nil -> create_setting!(name, value, datatype) - setting -> update_setting!(setting, value, datatype) - end - end - - @doc """ - Gets the parsed value of a setting. Raises if the setting does not exist. - - Returns value in type of `Ecto.Enum.mappings(Setting, :datatype)` - """ - def get!(name) do - SettingBackup - |> Repo.get_by!(name: to_string(name)) - |> read_setting() - end - - @doc """ - Attempts to find a setting by name or creates a setting with value - if one doesn't exist, returning the parsed value. Optionally allows - specifying the datatype. - - Returns value in type of `Ecto.Enum.mappings(Setting, :datatype)` - """ - def fetch!(name, value) do - fetch!(name, value, infer_datatype(value)) - end - - def fetch!(name, value, datatype) do - case Repo.get_by(SettingBackup, name: to_string(name)) do - nil -> create_setting!(name, value, datatype) - setting -> read_setting(setting) - end - end - - defp change_setting(setting, attrs) do - SettingBackup.changeset(setting, attrs) - end - - defp create_setting!(name, value, datatype) do - %SettingBackup{} - |> change_setting(%{name: to_string(name), value: to_string(value), datatype: datatype}) - |> Repo.insert!() - |> read_setting() - end - - defp update_setting!(setting, value, datatype) do - setting - |> change_setting(%{value: to_string(value), datatype: datatype}) - |> Repo.update!() - |> read_setting() - end - - defp read_setting(%{value: value, datatype: :string}), do: value - defp read_setting(%{value: value, datatype: :boolean}), do: value in ["true", "t", "1"] - defp read_setting(%{value: value, datatype: :integer}), do: String.to_integer(value) - defp read_setting(%{value: value, datatype: :float}), do: String.to_float(value) - - defp infer_datatype(value) when is_boolean(value), do: :boolean - defp infer_datatype(value) when is_integer(value), do: :integer - defp infer_datatype(value) when is_float(value), do: :float - defp infer_datatype(value) when is_binary(value), do: :string -end diff --git a/test/pinchflat/settings_backup_test.exs b/test/pinchflat/settings_backup_test.exs deleted file mode 100644 index d568e97..0000000 --- a/test/pinchflat/settings_backup_test.exs +++ /dev/null @@ -1,108 +0,0 @@ -defmodule Pinchflat.SettingsBackupTest do - use Pinchflat.DataCase - - alias Pinchflat.SettingsBackup - alias Pinchflat.SettingsBackup.SettingBackup - - # NOTE: We're treating some of these tests differently - # than in other modules because certain settings - # are always created on app boot (including in the test env), - # so we can't treat these like a clean slate. - - describe "list_settings/0" do - test "returns all settings" do - SettingsBackup.set!("foo", "bar") - results = SettingsBackup.list_settings() - - assert Enum.all?(results, fn setting -> match?(%SettingBackup{}, setting) end) - end - end - - describe "set/2" do - test "creates a new setting if one does not exist" do - original = Repo.aggregate(SettingBackup, :count, :id) - SettingsBackup.set!("foo", "bar") - assert Repo.aggregate(SettingBackup, :count, :id) == original + 1 - end - - test "updates an existing setting if one exists" do - SettingsBackup.set!("foo", "bar") - original = Repo.aggregate(SettingBackup, :count, :id) - SettingsBackup.set!("foo", "baz") - assert Repo.aggregate(SettingBackup, :count, :id) == original - assert SettingsBackup.get!("foo") == "baz" - end - - test "returns the parsed value" do - assert SettingsBackup.set!("foo", true) == true - assert SettingsBackup.set!("foo", false) == false - assert SettingsBackup.set!("foo", 123) == 123 - assert SettingsBackup.set!("foo", 12.34) == 12.34 - assert SettingsBackup.set!("foo", "bar") == "bar" - end - - test "allows for atom keys" do - assert SettingsBackup.set!(:foo, "bar") == "bar" - end - - test "blows up when an unsupported datatype is used" do - assert_raise FunctionClauseError, fn -> - SettingsBackup.set!("foo", nil) - end - end - end - - describe "set/3" do - test "allows manual specification of datatype" do - assert SettingsBackup.set!("foo", "true", :boolean) == true - assert SettingsBackup.set!("foo", "false", :boolean) == false - assert SettingsBackup.set!("foo", "123", :integer) == 123 - assert SettingsBackup.set!("foo", "12.34", :float) == 12.34 - end - end - - describe "get/1" do - test "returns the value of the setting" do - SettingsBackup.set!("str", "bar") - SettingsBackup.set!("bool", true) - SettingsBackup.set!("int", 123) - SettingsBackup.set!("float", 12.34) - - assert SettingsBackup.get!("str") == "bar" - assert SettingsBackup.get!("bool") == true - assert SettingsBackup.get!("int") == 123 - assert SettingsBackup.get!("float") == 12.34 - end - - test "allows for atom keys" do - SettingsBackup.set!("str", "bar") - assert SettingsBackup.get!(:str) == "bar" - end - - test "blows up when the setting does not exist" do - assert_raise Ecto.NoResultsError, fn -> - SettingsBackup.get!("foo") - end - end - end - - describe "fetch/2" do - test "creates a setting if one doesn't exist" do - original = Repo.aggregate(SettingBackup, :count, :id) - assert SettingsBackup.fetch!("foo", "bar") == "bar" - assert Repo.aggregate(SettingBackup, :count, :id) == original + 1 - end - - test "returns an existing setting if one does exist" do - SettingsBackup.set!("foo", "bar") - - assert SettingsBackup.fetch!("foo", "baz") == "bar" - end - end - - describe "fetch/3" do - test "allows manual specification of datatype" do - assert SettingsBackup.fetch!("foo", "true", :boolean) == true - end - end -end