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.

t()

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

Link to this type

attrs()

View Source
attrs() :: %{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.

Link to this type

label_or_labels()

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

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

Link to this type

namespaced_label()

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

Defines a namespaced label.

Link to this type

t()

View Source
t() :: %Dymo.Tag{
  __meta__: term(),
  id: term(),
  inserted_at: term(),
  label: term(),
  ns: term(),
  updated_at: term()
}

Defines simple tags identified by a unique label.

Link to this section Functions

Casts attributes into a Tag struct.

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}
Link to this function

find_existing(tags)

View Source
find_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
Link to this function

find_or_create!(labels)

View Source
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