Dymo v2.0.0 Dymo.TaggerImpl View Source
This tagger helps with tagging objects using an ecto-backed repo.
Link to this section Summary
Functions
Adds labels to a given instance of a model.
Generates query for retrieving labels associated with a schema.
Queries models that are tagged with the given labels.
Generates query for retrieving labels associated with a schema's instance.
Removes labels from a given instance of a model.
Sets the labels associated with an instance of a model, for the given namespace.
Link to this section Types
Specs
join_key() :: Dymo.Tagger.join_key()
Specs
join_table() :: Dymo.Tagger.join_table()
Link to this section Functions
Specs
add_labels(Dymo.Taggable.t(), Dymo.Tag.label_or_labels(), keyword()) :: Ecto.Schema.t()
Adds labels to a given instance of a model.
Examples
iex> %Dymo.Post{title: "Hey"}
...> |> Dymo.repo().insert!
...> |> TaggerImpl.set_labels([{:number, "three"}, {:number, "four"}], create_missing: true)
...> |> TaggerImpl.add_labels({:number, "five"}, create_missing: true)
...> |> Map.get(:tags)
...> |> Enum.map(& &1.label)
...> |> Enum.sort()
~w(five four three)
Specs
all_labels(join_table(), join_key(), keyword()) :: Ecto.Query.t()
Generates query for retrieving labels associated with a schema.
Examples
iex> %Dymo.Post{title: "Hey"}
...> |> Dymo.repo().insert!
...> |> TaggerImpl.set_labels([{:number, "eight"}, {:number, "nine"}], create_missing: true)
iex> "taggings"
...> |> TaggerImpl.all_labels(:post_id, ns: :number)
...> |> Dymo.repo().all()
["eight", "nine"]
Specs
labeled_with( module(), Dymo.Tag.label_or_labels(), join_table(), join_key(), keyword() ) :: Ecto.Query.t()
Queries models that are tagged with the given labels.
Examples
iex> %{id: id} = %Dymo.Post{title: "Hey"}
...> |> Dymo.repo().insert!
...> |> TaggerImpl.set_labels([{:number, "ten"}, {:number, "eleven"}], create_missing: true)
iex> id == Dymo.Post
...> |> TaggerImpl.labeled_with({:number, "ten"}, "taggings", :post_id)
...> |> Dymo.repo().all()
...> |> hd
...> |> Map.get(:id)
true
iex> Dymo.Post
...> |> TaggerImpl.labeled_with({:unknown, "nothing"}, "taggings", :post_id)
...> |> Dymo.repo().all()
[]
Specs
labels(Dymo.Taggable.t(), join_table(), join_key(), keyword()) :: Ecto.Query.t()
Generates query for retrieving labels associated with a schema's instance.
Specs
remove_labels(Dymo.Taggable.t(), Dymo.Tag.label_or_labels(), keyword()) :: Ecto.Schema.t()
Removes labels from a given instance of a model.
Examples
iex> %{tags: tags} = %Dymo.Post{title: "Hey"}
...> |> Dymo.repo().insert!
...> |> TaggerImpl.set_labels([{:number, "six"}, {:number, "seven"}], create_missing: true)
...> |> TaggerImpl.remove_labels({:number, "six"})
iex> Enum.map(tags, & &1.label)
["seven"]
Specs
set_labels(Dymo.Taggable.t(), Dymo.Tag.label_or_labels(), keyword()) :: Ecto.Schema.t()
Sets the labels associated with an instance of a model, for the given namespace.
If any other labels are associated to the given model and namespace, they are discarded if they are not part of the list of passed new labels.
Note that only tags with the :assignable
boolean set to true
can be
set. If a tag which :assignable
flag is false is provided, it won't be
assigned to the target object but no error will be returned.
Examples
iex> post =
...> %Dymo.Post{title: "Hey"}
...> |> Dymo.repo().insert!
iex> %{ns: :special, label: "nope", assignable: false}
...> |> Dymo.Tag.changeset()
...> |> Dymo.repo().insert!
iex> post
...> |> TaggerImpl.set_labels([{:rank, "one"}, {:rank, "two"}, {:special, "nope"}], create_missing: true)
...> |> Map.get(:tags)
...> |> Enum.map(& {&1.ns, &1.label})
[{:rank, "one"}, {:rank, "two"}]
iex> post
...> |> TaggerImpl.set_labels({:rank, "officer"}, create_missing: true)
...> |> Map.get(:tags)
...> |> Enum.map(& {&1.ns, &1.label})
[{:rank, "officer"}]