-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
calculation.ex
30 lines (23 loc) · 908 Bytes
/
calculation.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
defmodule Ash.Calculation do
@moduledoc "The behaviour for a calculation module"
defmacro __using__(opts) do
type =
opts[:type] ||
raise "Must provide `type` option to `use Ash.Calculation` in #{__CALLER__.module}"
unless Ash.Type.ash_type?(Ash.Type.get_type(opts[:type])) do
raise "Value provided for `type` option must be a valid Ash type"
end
quote do
@behaviour Ash.Calculation
def init(opts), do: {:ok, opts}
def type, do: unquote(type)
def describe(opts), do: inspect({__MODULE__, opts})
defoverridable init: 1, type: 0, describe: 1
end
end
@callback init(Keyword.t()) :: {:ok, Keyword.t()} | {:error, term}
@callback type() :: Ash.Type.t()
@callback describe(Keyword.t()) :: String.t()
@callback calculate([Ash.Resource.record()], Keyword.t(), map) ::
{:ok, [term]} | [term] | {:error, term}
end