Dymo.Tag (Dymo v3.0.3) View Source

This module provides functionality dedicated to handling tag data.

It essentially aims at maintaining singleton labels in a tags table and exposes helper functions to ease their creation.

Link to this section Summary

Types

Defines attributes for building this model's changeset

Defines a tag's label, namespaced or not.

Defines a single flat label or a list of labels, namespaced or not.

Defines a namespaced label.

t()

Defines simple tags identified by a unique label.

Functions

Makes a changeset suited to manipulate the Tag model.

This function gets an existing tag using its label. If the tag doesn't exit, it is atomically created. It could be described as a "singleton" helper.

This function gets an existing tag using its label. If the tag doesn't exit, it is atomically created. It could be described as a "singleton" helper.

Casts attributes into a Tag struct.

Link to this section Types

Specs

creation_attrs() :: %{
  optional(:ns) => Dymo.Tag.Ns.t(),
  optional(:assignable) => boolean(),
  :label => String.t()
}

Defines attributes for building this model's changeset

Specs

label() :: String.t() | namespaced_label()

Defines a tag's label, namespaced or not.

Specs

label_or_labels() :: label() | [label()]

Defines a single flat label or a list of labels, namespaced or not.

Specs

namespaced_label() :: {Dymo.Tag.Ns.t(), String.t()}

Defines a namespaced label.

Specs

t() :: %Dymo.Tag{
  __meta__: term(),
  assignable: term(),
  children: term(),
  description: term(),
  id: term(),
  inserted_at: term(),
  label: term(),
  ns: term(),
  parent: term(),
  parent_id: term(),
  updated_at: term()
}

Defines simple tags identified by a unique label.

Link to this section Functions

Link to this function

changeset(struct, attrs)

View Source

Specs

changeset(struct() | t(), creation_attrs()) :: Ecto.Changeset.t()

Makes a changeset suited to manipulate the Tag model.

Examples

iex> "blue"
...>   |> create_changeset()
...>   |> Changeset.apply_changes()
...>   |> Map.take([:ns, :label])
%{ns: Ns.root_namespace(), label: "blue"}

iex> {:paint, "blue"}
...>   |> create_changeset()
...>   |> Changeset.apply_changes()
...>   |> Map.take([:ns, :label])
%{ns: :paint, label: "blue"}

iex> {"car", "blue"}
...>   |> create_changeset()
...>   |> Changeset.apply_changes()
...>   |> Map.take([:ns, :label])
%{ns: :car, label: "blue"}

iex> %{ns: :car, label: "blue"}
...>   |> create_changeset()
...>   |> Changeset.apply_changes()
...>   |> Map.take([:ns, :label])
%{ns: :car, label: "blue"}

iex> {"non existent", "blue"}
...>   |> create_changeset()
...>   |> Map.take([:valid?])
%{valid?: false}

Specs

find_existing(label_or_labels() | t() | [t()]) :: label_or_labels() | nil

This function gets an existing tag using its label. If the tag doesn't exit, it is atomically created. It could be described as a "singleton" helper.

Examples

iex> %{id: id1a} = Tag.find_or_create!("novel")
...> [%{id: id1b}, other] = Tag.find_existing(["novel", "book"])
...> {id1a, nil} == {id1b, other}
true

iex> %{id: id1a} = Tag.find_or_create!({:romance, "novel"})
...> [%{id: id1b}, other] = Tag.find_existing([{:romance, "novel"}, {:scifi, "book"}])
...> {id1a, nil} == {id1b, other}
true

Specs

find_or_create!(label_or_labels() | t()) :: label_or_labels()

This function gets an existing tag using its label. If the tag doesn't exit, it is atomically created. It could be described as a "singleton" helper.

Examples

iex> %{id: id1a} = Tag.find_or_create!("novel")
...> [%{id: id2a}, %{id: id3a}] = Tag.find_or_create!(["article", "book"])
...> [%{id: id1b}, %{id: id2b}, %{id: id3b}] = Tag.find_or_create!(["novel", "article", "book"])
...> {id1a, id2a, id3a} == {id1b, id2b, id3b}
true

iex> %{id: id4a} = Tag.find_or_create!({:romance, "novel"})
...> [%{id: id5a}, %{id: id6a}] = Tag.find_or_create!([{:romance, "article"}, {:scifi, "book"}])
...> [%{id: id4b}, %{id: id5b}, %{id: id6b}] = Tag.find_or_create!([{:romance, "novel"}, {:romance, "article"}, {:scifi, "book"}])
...> {id4a, id5a, id6a} == {id4b, id5b, id6b}
true

Specs

to_struct(label_or_labels() | t() | Ecto.Changeset.t()) :: t()

Casts attributes into a Tag struct.