Current section

Files

Jump to
nldoc_spec lib nldoc spec _template.ex
Raw

lib/nldoc/spec/_template.ex

defmodule NLdoc.Spec.Template do
@moduledoc """
This module is a template for creating new NLdoc spec resource schemas.
To create a new resource schema:
1. Copy this file to a new file in the same directory, renaming it to the name of the new resource in snake_case.
2. Replace all occurrences of `Template` in that file with the name of the new resource.
3. Add the fields for this resource to the `typed_embedded_schema`.
4. Update the `changeset` function to cast and validate the new fields.
5. Add the new resource type to the `NLdoc.Spec.Type` enum.
6. Add the `t` type to the `NLdoc.Spec.object` type.
7. Remove this section of the moduledoc up to and including `---`.
8. Now you can use the new resource in other schemas, e.g. add it to the possible children of `NLdoc.Spec.Document`.
---
This module defines the Ecto schema for the NLdoc spec Template object.
"""
use NLdoc.Spec.Schema, type: "https://spec.nldoc.nl/Resource/Template"
typed_embedded_schema null: false do
field :type, :string, default: @resource_type
field(:descriptors, {:array, PolymorphicEmbed},
types: map(NLdoc.Spec.descriptors()),
type_field_name: :type,
on_replace: :delete,
default: [],
array?: true
) :: [NLdoc.Spec.descriptor()]
end
def changeset(object, attrs) do
object
|> cast(attrs, [:type], @cast_opts)
|> cast_polymorphic_embed(:descriptors, @cast_opts)
|> validate_required([:type])
|> validate_inclusion(:type, [@resource_type])
end
end