From fb5a9ee28933e171584c7d31b1ce446313094ea5 Mon Sep 17 00:00:00 2001 From: Malte Rohde Date: Wed, 20 Dec 2023 13:46:20 +0100 Subject: [PATCH] rename to cast_all --- CHANGELOG.md | 2 +- lib/bitcrowd_ecto/changeset.ex | 32 +++++++++++++-------------- test/bitcrowd_ecto/changeset_test.exs | 20 ++++++++--------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82e554b..7cc2e54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ ### Added -* Add `BitcrowdEcto.Changeset.auto_cast/3`, a introspection-based automatic cast function. +* Add `BitcrowdEcto.Changeset.cast_all/3`, a introspection-based automatic cast function. * Add `BitcrowdEcto.Assertions.assert_cast_error_on/2`. ### Fixed diff --git a/lib/bitcrowd_ecto/changeset.ex b/lib/bitcrowd_ecto/changeset.ex index efd5b58..13aca7c 100644 --- a/lib/bitcrowd_ecto/changeset.ex +++ b/lib/bitcrowd_ecto/changeset.ex @@ -426,7 +426,7 @@ defmodule BitcrowdEcto.Changeset do end end - @type auto_cast_option :: {:action, atom} + @type cast_all_option :: {:action, atom} @doc """ Introspects a schema and casts all defined fields from a params map. @@ -443,45 +443,45 @@ defmodule BitcrowdEcto.Changeset do ## Examples - iex> changeset = auto_cast(TestEmbeddedSchema, %{some_field: 4}) + iex> changeset = cast_all(TestEmbeddedSchema, %{some_field: 4}) iex> changeset.valid? true - iex> changeset = auto_cast(%TestEmbeddedSchema{}, %{some_field: 4}) + iex> changeset = cast_all(%TestEmbeddedSchema{}, %{some_field: 4}) iex> changeset.valid? true - iex> changeset = auto_cast(change(%TestEmbeddedSchema{}), %{some_field: 4}) + iex> changeset = cast_all(change(%TestEmbeddedSchema{}), %{some_field: 4}) iex> changeset.valid? true - iex> changeset = auto_cast(TestEmbeddedSchema, %{}, required: [:some_field]) + iex> changeset = cast_all(TestEmbeddedSchema, %{}, required: [:some_field]) iex> changeset.errors [some_field: {"can't be blank", [validation: :required]}] - iex> changeset = auto_cast(TestEmbeddedSchema, %{}, optional: [:some_other_field]) + iex> changeset = cast_all(TestEmbeddedSchema, %{}, optional: [:some_other_field]) iex> changeset.errors [some_field: {"can't be blank", [validation: :required]}] """ @doc since: "0.17.0" - @spec auto_cast(module | struct, map) :: Ecto.Changeset.t() - @spec auto_cast(module | struct, map, [auto_cast_option]) :: Ecto.Changeset.t() - def auto_cast(schema_or_struct_or_changeset, params, opts \\ []) + @spec cast_all(module | struct, map) :: Ecto.Changeset.t() + @spec cast_all(module | struct, map, [cast_all_option]) :: Ecto.Changeset.t() + def cast_all(schema_or_struct_or_changeset, params, opts \\ []) - def auto_cast(%Ecto.Changeset{} = changeset, params, opts) do - do_auto_cast(changeset.data.__struct__, changeset, params, opts) + def cast_all(%Ecto.Changeset{} = changeset, params, opts) do + do_cast_all(changeset.data.__struct__, changeset, params, opts) end - def auto_cast(schema, params, opts) when is_atom(schema) do - do_auto_cast(schema, struct!(schema), params, opts) + def cast_all(schema, params, opts) when is_atom(schema) do + do_cast_all(schema, struct!(schema), params, opts) end - def auto_cast(struct, params, opts) when is_struct(struct) do - do_auto_cast(struct.__struct__, struct, params, opts) + def cast_all(struct, params, opts) when is_struct(struct) do + do_cast_all(struct.__struct__, struct, params, opts) end - defp do_auto_cast(schema, struct_or_changeset, params, opts) do + defp do_cast_all(schema, struct_or_changeset, params, opts) do required = required_fields(schema, opts) %{scalars: scalars, embeds: embeds} = grouped_fields(schema) diff --git a/test/bitcrowd_ecto/changeset_test.exs b/test/bitcrowd_ecto/changeset_test.exs index e55dbbb..3ef9052 100644 --- a/test/bitcrowd_ecto/changeset_test.exs +++ b/test/bitcrowd_ecto/changeset_test.exs @@ -699,7 +699,7 @@ defmodule BitcrowdEcto.ChangesetTest do end end - describe "auto_cast/3" do + describe "cast_all/3" do defp params do embedded = %{"some_field" => 12} @@ -713,7 +713,7 @@ defmodule BitcrowdEcto.ChangesetTest do end test "allows to automatically cast all fields of a schema" do - %Ecto.Changeset{} = cs = auto_cast(TestVarietySchema, params()) + %Ecto.Changeset{} = cs = cast_all(TestVarietySchema, params()) assert cs.valid? assert_changes(cs, :some_scalar, 5) assert_changes(cs, :some_enum, :foo) @@ -728,36 +728,36 @@ defmodule BitcrowdEcto.ChangesetTest do end test "accepts structs as input" do - %Ecto.Changeset{} = cs = auto_cast(%TestVarietySchema{}, params()) + %Ecto.Changeset{} = cs = cast_all(%TestVarietySchema{}, params()) assert cs.valid? end test "accepts changesets as input" do - %Ecto.Changeset{} = cs = auto_cast(change(%TestVarietySchema{}), params()) + %Ecto.Changeset{} = cs = cast_all(change(%TestVarietySchema{}), params()) assert cs.valid? end test "accepts params maps with atom keys" do - cs = auto_cast(TestVarietySchema, %{some_scalar: 5}) + cs = cast_all(TestVarietySchema, %{some_scalar: 5}) assert cs.valid? assert_changes(cs, :some_scalar, 5) end test "returns invalid changeset on cast errors" do - cs = auto_cast(TestVarietySchema, %{some_scalar: "foo"}) + cs = cast_all(TestVarietySchema, %{some_scalar: "foo"}) refute cs.valid? assert_cast_error_on(cs, :some_scalar) end test "accepts a list of required fields and validates them" do - cs = auto_cast(TestVarietySchema, %{}, required: [:some_scalar]) + cs = cast_all(TestVarietySchema, %{}, required: [:some_scalar]) refute cs.valid? assert_required_error_on(cs, :some_scalar) refute_errors_on(cs, :some_enum) end test "accepts a list of optional fields and validates them" do - cs = auto_cast(TestVarietySchema, %{}, optional: [:some_scalar]) + cs = cast_all(TestVarietySchema, %{}, optional: [:some_scalar]) refute cs.valid? assert_required_error_on(cs, :some_enum) refute_errors_on(cs, :some_scalar) @@ -765,7 +765,7 @@ defmodule BitcrowdEcto.ChangesetTest do test "required validations work for embeds, too" do cs = - auto_cast( + cast_all( TestVarietySchema, %{ one_embed: nil, @@ -781,7 +781,7 @@ defmodule BitcrowdEcto.ChangesetTest do test ":required and :optional are mutually exclusive" do assert_raise ArgumentError, ~r/options are mutually exclusive/, fn -> - auto_cast(TestVarietySchema, %{}, required: [], optional: []) + cast_all(TestVarietySchema, %{}, required: [], optional: []) end end end