|
| 1 | +# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule DiffoExample.Access.CableCharacteristic do |
| 6 | + @moduledoc "Typed characteristic for a Cable's physical properties." |
| 7 | + use Ash.Resource, |
| 8 | + fragments: [Diffo.Provider.BaseCharacteristic], |
| 9 | + domain: DiffoExample.Access |
| 10 | + |
| 11 | + alias DiffoExample.Access.CharacteristicChanges |
| 12 | + |
| 13 | + resource do |
| 14 | + description "Typed characteristic carrying cable physical property fields" |
| 15 | + plural_name :cable_characteristics |
| 16 | + end |
| 17 | + |
| 18 | + actions do |
| 19 | + create :create do |
| 20 | + accept [:name, :pairs, :length_amount, :length_unit, :loss_amount, :loss_unit, :technology] |
| 21 | + argument :instance_id, :uuid |
| 22 | + argument :feature_id, :uuid |
| 23 | + change manage_relationship(:instance_id, :instance, type: :append) |
| 24 | + change manage_relationship(:feature_id, :feature, type: :append) |
| 25 | + end |
| 26 | + |
| 27 | + update :update do |
| 28 | + accept [:pairs, :technology, :length_amount, :length_unit, :loss_amount, :loss_unit] |
| 29 | + argument :length, :term, allow_nil?: true |
| 30 | + argument :loss, :term, allow_nil?: true |
| 31 | + |
| 32 | + change fn changeset, _ -> |
| 33 | + changeset |
| 34 | + |> CharacteristicChanges.set_unit(:length, :length_amount, :length_unit) |
| 35 | + |> CharacteristicChanges.set_unit(:loss, :loss_amount, :loss_unit) |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + attributes do |
| 41 | + attribute :pairs, :integer, public?: true |
| 42 | + attribute :length_amount, :integer, public?: true |
| 43 | + attribute :length_unit, :atom, public?: true |
| 44 | + attribute :loss_amount, :float, public?: true |
| 45 | + attribute :loss_unit, :atom, public?: true |
| 46 | + attribute :technology, :atom, public?: true |
| 47 | + end |
| 48 | + |
| 49 | + calculations do |
| 50 | + calculate :value, |
| 51 | + Diffo.Type.CharacteristicValue, |
| 52 | + DiffoExample.Access.CableCharacteristic.ValueCalculation do |
| 53 | + public? true |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + preparations do |
| 58 | + prepare build(load: [:value]) |
| 59 | + end |
| 60 | + |
| 61 | + jason do |
| 62 | + pick [:name, :value] |
| 63 | + compact true |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +defmodule DiffoExample.Access.CableCharacteristic.Value do |
| 68 | + @moduledoc false |
| 69 | + use Ash.TypedStruct, extensions: [AshJason.TypedStruct, AshOutstanding.TypedStruct] |
| 70 | + |
| 71 | + alias DiffoExample.Access.IntegerUnit |
| 72 | + alias DiffoExample.Access.FloatUnit |
| 73 | + |
| 74 | + jason do |
| 75 | + pick [:pairs, :length, :loss, :technology] |
| 76 | + compact true |
| 77 | + end |
| 78 | + |
| 79 | + outstanding do |
| 80 | + expect [:pairs, :loss] |
| 81 | + end |
| 82 | + |
| 83 | + typed_struct do |
| 84 | + field :pairs, :integer |
| 85 | + field :length, IntegerUnit |
| 86 | + field :loss, FloatUnit |
| 87 | + field :technology, :atom |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +defmodule DiffoExample.Access.CableCharacteristic.ValueCalculation do |
| 92 | + @moduledoc false |
| 93 | + use Ash.Resource.Calculation |
| 94 | + |
| 95 | + alias DiffoExample.Access.IntegerUnit |
| 96 | + alias DiffoExample.Access.FloatUnit |
| 97 | + alias DiffoExample.Access.CableCharacteristic |
| 98 | + |
| 99 | + @impl true |
| 100 | + def load(_, _, _), do: [] |
| 101 | + |
| 102 | + @impl true |
| 103 | + def calculate(records, _, _) do |
| 104 | + Enum.map(records, fn r -> |
| 105 | + %CableCharacteristic.Value{ |
| 106 | + pairs: r.pairs, |
| 107 | + length: |
| 108 | + if r.length_amount do |
| 109 | + %IntegerUnit{amount: r.length_amount, unit: r.length_unit} |
| 110 | + end, |
| 111 | + loss: |
| 112 | + if r.loss_amount do |
| 113 | + %FloatUnit{amount: r.loss_amount, unit: r.loss_unit} |
| 114 | + end, |
| 115 | + technology: r.technology |
| 116 | + } |
| 117 | + end) |
| 118 | + end |
| 119 | +end |
0 commit comments