Dymo v1.0.2 Dymo.Tag 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.
Defines simple tags identified by a unique label.
Functions
Casts attributes into a Tag
struct.
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.
Link to this section Types
attrs()
View Sourceattrs() :: %{optional(:ns) => Dymo.Tag.Ns.t(), :label => String.t()}
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.
namespaced_label()
View Sourcenamespaced_label() :: {Dymo.Tag.Ns.t(), String.t()}
Defines a namespaced label.
Defines simple tags identified by a unique label.
Link to this section Functions
cast(struct)
View Sourcecast(label_or_labels() | t() | Ecto.Changeset.t()) :: t()
Casts attributes into a Tag
struct.
changeset(label)
View Sourcechangeset(label() | attrs()) :: Ecto.Changeset.t()
Makes a changeset suited to manipulate the Tag
model.
Examples
iex> "blue"
...> |> changeset()
...> |> Changeset.apply_changes()
...> |> Map.take([:ns, :label])
%{ns: Ns.root_namespace(), label: "blue"}
iex> {:paint, "blue"}
...> |> changeset()
...> |> Changeset.apply_changes()
...> |> Map.take([:ns, :label])
%{ns: :paint, label: "blue"}
iex> {"car", "blue"}
...> |> changeset()
...> |> Changeset.apply_changes()
...> |> Map.take([:ns, :label])
%{ns: :car, label: "blue"}
iex> %{ns: :car, label: "blue"}
...> |> changeset()
...> |> Changeset.apply_changes()
...> |> Map.take([:ns, :label])
%{ns: :car, label: "blue"}
iex> {"non existent", "blue"}
...> |> changeset()
...> |> Map.take([:valid?])
%{valid?: false}
find_existing(tags)
View Sourcefind_existing(label_or_labels() | t() | [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: 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
find_or_create!(labels)
View Sourcefind_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