recipe_process.ex (2249B)
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.RecipeProcess do 19 @moduledoc """ 20 Specifies a process in a recipe for use in planning from recipe. 21 """ 22 23 use Zenflows.DB.Schema 24 25 alias Ecto.Changeset 26 alias Zenflows.DB.{Schema, Validate} 27 alias Zenflows.VF.{ 28 Duration, 29 ProcessSpecification, 30 TimeUnitEnum, 31 } 32 33 @type t() :: %__MODULE__{ 34 name: String.t(), 35 note: String.t() | nil, 36 process_conforms_to: ProcessSpecification.t() | nil, 37 process_classified_as: [String.t()] | nil, 38 has_duration: Duration.t() | nil, 39 has_duration_unit_type: TimeUnitEnum.t() | nil, 40 has_duration_numeric_duration: Decimal.t() | nil, 41 } 42 43 schema "vf_recipe_process" do 44 field :name, :string 45 field :note, :string 46 belongs_to :process_conforms_to, ProcessSpecification 47 field :process_classified_as, {:array, :string} 48 field :has_duration, :map, virtual: true 49 field :has_duration_unit_type, TimeUnitEnum 50 field :has_duration_numeric_duration, :decimal 51 timestamps() 52 end 53 54 @reqr ~w[name process_conforms_to_id]a 55 @cast @reqr ++ ~w[process_classified_as note has_duration]a 56 57 @doc false 58 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t() 59 def changeset(schema \\ %__MODULE__{}, params) do 60 schema 61 |> Changeset.cast(params, @cast) 62 |> Changeset.validate_required(@reqr) 63 |> Validate.name(:name) 64 |> Validate.note(:note) 65 |> Validate.class(:process_classified_as) 66 |> Duration.cast(:has_duration) 67 |> Changeset.assoc_constraint(:process_conforms_to) 68 end 69 end