2019-10-03 16:08:36 +13:00
|
|
|
defmodule Ash do
|
2020-10-10 03:13:44 +13:00
|
|
|
@moduledoc " Types and simple helpers for Ash"
|
2020-06-02 17:47:25 +12:00
|
|
|
alias Ash.Resource.Actions.{Create, Destroy, Read, Update}
|
|
|
|
alias Ash.Resource.Relationships.{BelongsTo, HasMany, HasOne, ManyToMany}
|
2020-01-14 07:16:24 +13:00
|
|
|
|
2020-07-15 17:38:42 +12:00
|
|
|
@type action :: Create.t() | Read.t() | Update.t() | Destroy.t()
|
|
|
|
@type action_type :: :read | :create | :update | :destroy
|
|
|
|
@type actor :: Ash.record()
|
2020-08-25 16:49:07 +12:00
|
|
|
@type aggregate :: Ash.Query.Aggregate.t() | Ash.Resource.Aggregate.t()
|
2020-08-09 05:55:36 +12:00
|
|
|
@type aggregate_kind :: Ash.Query.Aggregate.kind()
|
2020-07-15 17:38:42 +12:00
|
|
|
@type api :: module
|
|
|
|
@type attribute :: Ash.Resource.Attribute.t()
|
2020-08-25 16:49:07 +12:00
|
|
|
@type calculation :: Ash.Resource.Calculation.t()
|
2019-10-07 09:36:06 +13:00
|
|
|
@type cardinality_many_relationship() :: HasMany.t() | ManyToMany.t()
|
2020-07-15 17:38:42 +12:00
|
|
|
@type cardinality_one_relationship() :: HasOne.t() | BelongsTo.t()
|
|
|
|
@type changeset :: Ash.Changeset.t()
|
2019-12-05 20:18:13 +13:00
|
|
|
@type data_layer :: module
|
2020-05-14 03:54:44 +12:00
|
|
|
@type data_layer_query :: struct
|
2019-10-07 09:36:06 +13:00
|
|
|
@type error :: struct
|
2020-08-09 05:55:36 +12:00
|
|
|
@type filter :: Ash.Filter.t()
|
2019-12-23 17:28:40 +13:00
|
|
|
@type params :: Keyword.t()
|
2020-07-17 11:17:01 +12:00
|
|
|
@type primary_key :: record() | map | term
|
2020-05-14 03:54:44 +12:00
|
|
|
@type query :: Ash.Query.t()
|
2020-07-15 17:38:42 +12:00
|
|
|
@type record :: struct
|
|
|
|
@type relationship :: cardinality_one_relationship() | cardinality_many_relationship()
|
|
|
|
@type relationship_cardinality :: :many | :one
|
|
|
|
@type resource :: module
|
2020-08-14 09:49:33 +12:00
|
|
|
@type side_loads :: term
|
2020-10-12 16:55:47 +13:00
|
|
|
@type page :: Ash.Page.Keyset.t() | Ash.Page.Offset.t()
|
2020-12-29 13:18:01 +13:00
|
|
|
@type sort_order ::
|
|
|
|
:asc | :desc | :asc_nils_first | :asc_nils_last | :desc_nils_first | :desc_nils_last
|
|
|
|
@type sort :: list(atom | {atom, sort_order})
|
2020-07-15 17:38:42 +12:00
|
|
|
@type validation :: Ash.Resource.Validation.t()
|
2020-10-15 17:54:02 +13:00
|
|
|
@type notification :: Ash.Notifier.Notification.t()
|
2020-05-21 10:59:58 +12:00
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
require Ash.Dsl.Extension
|
2019-10-04 15:33:55 +13:00
|
|
|
|
2020-06-15 18:40:33 +12:00
|
|
|
def implements_behaviour?(module, behaviour) do
|
2020-06-05 14:43:51 +12:00
|
|
|
:attributes
|
|
|
|
|> module.module_info()
|
2020-06-15 18:40:33 +12:00
|
|
|
|> Enum.flat_map(fn
|
|
|
|
{:behaviour, value} -> List.wrap(value)
|
|
|
|
_ -> []
|
|
|
|
end)
|
|
|
|
|> Enum.any?(&(&1 == behaviour))
|
|
|
|
end
|
|
|
|
|
2020-09-04 16:59:32 +12:00
|
|
|
def uuid do
|
|
|
|
Ecto.UUID.generate()
|
|
|
|
end
|
|
|
|
|
2020-07-15 17:38:42 +12:00
|
|
|
@doc "Returns all extensions of a resource or api"
|
|
|
|
@spec extensions(resource() | api()) :: [module]
|
2020-06-14 18:39:11 +12:00
|
|
|
def extensions(resource) do
|
2021-01-07 08:39:56 +13:00
|
|
|
Ash.Dsl.Extension.get_persisted(resource, :extensions)
|
2019-10-03 20:18:07 +13:00
|
|
|
end
|
2021-01-07 08:39:56 +13:00
|
|
|
|
|
|
|
@spec try_compile(term) :: :ok
|
|
|
|
def try_compile(module) when is_atom(module) do
|
|
|
|
Code.ensure_loaded(module)
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_compiled!(_), do: :ok
|
2019-10-03 16:08:36 +13:00
|
|
|
end
|