ChangesetMerger v0.4.2 ChangesetMerger View Source

A library to help you manipulate changes in your changeset with relative ease

Link to this section Summary

Functions

Changesets can run without a "changeset", by passing a tuple containing both the data and the supported types as a tuple instead of a struct

Check for the field in the provided changeset, and if not found then set it ot the it based on the provide function

Derive a field from another field (or fields) based on the provided function. If the source field is not set, then do not do anything

Derive a field from another field (or fields) based on the provided function. only if the target field IS NOT set. If the source field is not set, then do not do anything

Extract the value of a specific field. It could be in the changes, or part of the existing entity. This is useful when you want to make decisions based on the values, but that it doesn't fit into the available functions

Extract the value of a field, this could be provided in the changes, or part of the existing entity. This is useful when you want to make decisions based on the values, but that it doesn't fit into the available functions

Force a field to be a certain value

Validates that at least one of the provided fields are present in the changeset

Link to this section Functions

Changesets can run without a "changeset", by passing a tuple containing both the data and the supported types as a tuple instead of a struct:

A convenience function to generate a changeset without a struct like %User{}.

ChangesetMerger.create(
  %{"first_name" => "Andrew"},
  %{first_name: :string, last_name: :string, email: :string})

If you want to seed the underlying mode, then use the &create/3 function

ChangesetMerger.create(
  %{"first_name" => "Normal Andrew"},
  %{"first_name" => "Super Andrew"},
  %{first_name: :string, last_name: :string, email: :string})
Link to this function

create(record, params, types) View Source

Link to this function

defaulted(changeset, field, default_if_missing) View Source

Check for the field in the provided changeset, and if not found then set it ot the it based on the provide function.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.defaulted(:apples, "blue")
...> |> Map.get(:changes)
%{apples: "blue"}

iex> ChangesetMerger.create(%{apples: "red"}, %{}, %{apples: :string})
...> |> ChangesetMerger.defaulted(:apples, "blue")
...> |> Map.get(:changes)
%{}

iex> ChangesetMerger.create(%{"apples" => "red"}, %{apples: :string})
...> |> ChangesetMerger.defaulted(:apples, "blue")
...> |> Map.get(:changes)
%{apples: "red"}
Link to this function

derive(changeset, field, fun) View Source

Derive a field from another field (or fields) based on the provided function. If the source field is not set, then do not do anything.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.derive(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{}

iex> ChangesetMerger.create(%{"apples" => "green"}, %{apples: :string})
...> |> ChangesetMerger.derive(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{apples: "green", oranges: "neerg"}

iex> ChangesetMerger.create(%{apples: "green"}, %{}, %{apples: :string})
...> |> ChangesetMerger.derive(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{oranges: "neerg"}

iex> ChangesetMerger.create(%{apples: "green", oranges: "neerg"}, %{}, %{apples: :string})
...> |> ChangesetMerger.derive(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{}

iex> ChangesetMerger.create(%{"apples" => "green", "bananas" => "blue"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.derive([:apples, :bananas], :oranges, fn([a,b]) -> a <> b end)
...> |> Map.get(:changes)
%{apples: "green", bananas: "blue", oranges: "greenblue"}

iex> ChangesetMerger.create(%{"apples" => "green"}, %{apples: :string})
...> |> ChangesetMerger.derive(:apples, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{apples: "neerg"}

iex> ChangesetMerger.create(%{"apples" => "green", "oranges" => "blue"}, %{apples: :string, oranges: :string})
...> |> ChangesetMerger.derive(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{apples: "green", oranges: "neerg"}
Link to this function

derive(changeset, from_field_or_fields, to_field, fun) View Source

Link to this function

derive_if_missing(changeset, from_field_or_fields, to_field, fun) View Source

Derive a field from another field (or fields) based on the provided function. only if the target field IS NOT set. If the source field is not set, then do not do anything.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.derive_if_missing(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{}

iex> ChangesetMerger.create(%{"apples" => "green", "bananas" => "blue"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.derive_if_missing([:apples, :bananas], :oranges, fn([a,b]) -> a <> b end)
...> |> Map.get(:changes)
%{apples: "green", bananas: "blue", oranges: "greenblue"}

iex> ChangesetMerger.create(%{"apples" => "green"}, %{apples: :string})
...> |> ChangesetMerger.derive_if_missing(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{apples: "green", oranges: "neerg"}

iex> ChangesetMerger.create(%{"apples" => "green", "oranges" => "blue"}, %{apples: :string, oranges: :string})
...> |> ChangesetMerger.derive_if_missing(:apples, :oranges, fn(x) -> String.reverse(x) end)
...> |> Map.get(:changes)
%{apples: "green", oranges: "blue"}

iex> ChangesetMerger.create(%{"apples" => "green", "bananas" => "blue", "oranges" => "purple"}, %{apples: :string, bananas: :string, oranges: :string})
...> |> ChangesetMerger.derive_if_missing([:apples, :bananas], :oranges, fn([a,b]) -> a <> b end)
...> |> Map.get(:changes)
%{apples: "green", bananas: "blue", oranges: "purple"}
Link to this function

field_value(changeset, from_field) View Source

Extract the value of a specific field. It could be in the changes, or part of the existing entity. This is useful when you want to make decisions based on the values, but that it doesn't fit into the available functions.

If you provide a list, you get back a list. If you provide one field, you get back one field.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.field_value(:apples)
nil

iex> ChangesetMerger.create(%{apples: "a"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.field_values(:apples)
"a"
Link to this function

field_values(changeset, from_fields) View Source

Extract the value of a field, this could be provided in the changes, or part of the existing entity. This is useful when you want to make decisions based on the values, but that it doesn't fit into the available functions.

If you provide a list, you get back a list. If you provide one field, you get back one field.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.field_values([:apples])
[nil]

iex> ChangesetMerger.create(%{apples: "a"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.field_values([:apples])
["a"]

iex> ChangesetMerger.create(%{apples: "a"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.field_values([:apples, :bananas])
["a", nil]

iex> ChangesetMerger.create(%{apples: "a", bananas: "b"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.field_values([:apples, :bananas])
["a", "b"]

iex> ChangesetMerger.create(%{apples: "a"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.field_values(:apples)
"a"
Link to this function

force(changeset, field, val) View Source

Force a field to be a certain value.

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.force(:apples, "blue")
...> |> Map.get(:changes)
%{apples: "blue"}


iex> ChangesetMerger.create(%{"apples" => "green"}, %{apples: :string})
...> |> ChangesetMerger.force(:apples, "blue")
...> |> Map.get(:changes)
%{apples: "blue"}
Link to this function

validate_all_not_empty(changeset, fields, opts \\ []) View Source

Validates that at least one of the provided fields are present in the changeset.

If the value of a field is nil or a string made only of whitespace, the changeset is marked as invalid and an error is added. Note the error won't be added though if the field already has an error.

Options

  • :message - the message on failure, defaults to "at least one of apples can't be blank"
  • :trim - a boolean that sets whether whitespaces are removed before running the validation on binaries/strings, defaults to true

Examples

iex> ChangesetMerger.create(%{}, %{apples: :string})
...> |> ChangesetMerger.validate_all_not_empty([:apples])
...> |> Map.get(:errors)
[apples: {"at least one of apples must be provided", [validation: :all_not_empty]}]

iex> ChangesetMerger.create(%{}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.validate_all_not_empty([:apples, :bananas])
...> |> Map.get(:errors)
[apples: {"at least one of apples, bananas must be provided", [validation: :all_not_empty]}, bananas: {"at least one of apples, bananas must be provided", [validation: :all_not_empty]}]

iex> ChangesetMerger.create(%{apples: "a"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.validate_all_not_empty([:apples, :bananas])
...> |> Map.get(:errors)
[]

iex> ChangesetMerger.create(%{bananas: "b"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.validate_all_not_empty([:apples, :bananas])
...> |> Map.get(:errors)
[]

iex> ChangesetMerger.create(%{apples: "a", bananas: "b"}, %{apples: :string, bananas: :string})
...> |> ChangesetMerger.validate_all_not_empty([:apples, :bananas])
...> |> Map.get(:errors)
[]