zf

zenflows testing
git clone https://s.sonu.ch/~srfsh/zf.git
Log | Files | Refs | Submodules | README | LICENSE

scenario_definition.ex (1892B)


      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.ScenarioDefinition do
     19 @moduledoc """
     20 The type definition of one or more scenarios, such as Yearly Budget.
     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 	TimeUnitEnum,
     30 }
     31 
     32 @type t() :: %__MODULE__{
     33 	name: String.t(),
     34 	note: String.t() | nil,
     35 	has_duration_unit_type: TimeUnitEnum.t() | nil,
     36 	has_duration_numeric_duration: Decimal.t() | nil,
     37 	has_duration: Duration.t() | nil,
     38 }
     39 
     40 schema "vf_scenario_definition" do
     41 	field :name, :string
     42 	field :note, :string
     43 	field :has_duration, :map, virtual: true
     44 	field :has_duration_unit_type, TimeUnitEnum
     45 	field :has_duration_numeric_duration, :decimal
     46 	timestamps()
     47 end
     48 
     49 @reqr [:name]
     50 @cast @reqr ++ ~w[note has_duration]a
     51 
     52 @doc false
     53 @spec changeset(Schema.t(), Schema.params()) :: Changeset.t()
     54 def changeset(schema \\ %__MODULE__{}, params) do
     55 	Changeset.cast(schema, params, @cast)
     56 	|> Changeset.validate_required(@reqr)
     57 	|> Validate.name(:name)
     58 	|> Validate.note(:note)
     59 	|> Duration.cast(:has_duration)
     60 end
     61 end