time_unit_enum.ex (2038B)
1 # Zenflows is designed to implement the Valueflows vocabulary, 2 # written and maintained by srfsh <info@dyne.org>. 3 # Copyright (C) 2021-2023 Dyne.org foundation <foundation@dyne.org>. 4 # 5 # This program is free software: you can redistribute it and/or modify 6 # it under the terms of the GNU Affero General Public License as published by 7 # the Free Software Foundation, either version 3 of the License, or 8 # (at your option) any later version. 9 # 10 # This program is distributed in the hope that it will be useful, 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 # GNU Affero General Public License for more details. 14 # 15 # You should have received a copy of the GNU Affero General Public License 16 # along with this program. If not, see <https://www.gnu.org/licenses/>. 17 18 defmodule Zenflows.VF.TimeUnitEnum do 19 @moduledoc """ 20 Custom Ecto type for TimeUnit definitions in schemas. 21 """ 22 23 use Ecto.Type 24 25 @type t() :: :year | :month | :week | :day | :hour | :minute | :second 26 27 @spec values() :: [t(), ...] 28 def values() do 29 ~w[year month week day hour minute second]a 30 end 31 32 @impl true 33 def type() do 34 :string 35 end 36 37 @impl true 38 def cast(:year), do: {:ok, :year} 39 def cast(:month), do: {:ok, :month} 40 def cast(:week), do: {:ok, :week} 41 def cast(:day), do: {:ok, :day} 42 def cast(:hour), do: {:ok, :hour} 43 def cast(:minute), do: {:ok, :minute} 44 def cast(:second), do: {:ok, :second} 45 def cast(_), do: :error 46 47 @impl true 48 def dump(:year), do: {:ok, "year"} 49 def dump(:month), do: {:ok, "month"} 50 def dump(:week), do: {:ok, "week"} 51 def dump(:day), do: {:ok, "day"} 52 def dump(:hour), do: {:ok, "hour"} 53 def dump(:minute), do: {:ok, "minute"} 54 def dump(:second), do: {:ok, "second"} 55 def dump(_), do: :error 56 57 @impl true 58 def load("year"), do: {:ok, :year} 59 def load("month"), do: {:ok, :month} 60 def load("week"), do: {:ok, :week} 61 def load("day"), do: {:ok, :day} 62 def load("hour"), do: {:ok, :hour} 63 def load("minute"), do: {:ok, :minute} 64 def load("second"), do: {:ok, :second} 65 def load(_), do: :error 66 end