FatEcto.SharedHelper (FatEcto v1.3.0)

View Source

Provides utility functions for FatEcto, including handling pagination limits, skip values, dynamic binding, and preloading associations.

Summary

Functions

Converts a filterable option list to a map format if it's a keyword list.

Retrieves the primary keys for a given query.

Checks if a module implements the given behaviour by inspecting its __info__/1 metadata.

Converts a keyword list to a map with string keys.

Converts a keyword list to a map with string keys and normalizes operator values to uppercase.

Parses an integer from a string or returns the integer if already an integer.

Converts a string to an atom.

Converts a string to an existing atom.

Converts various date/datetime inputs to an Elixir Date struct.

Converts various date/datetime inputs to an Elixir Date struct.

Functions

filterable_opt_to_map(list)

@spec filterable_opt_to_map(maybe_improper_list() | any()) ::
  maybe_improper_list() | map()

Converts a filterable option list to a map format if it's a keyword list.

This function checks if the input is a keyword list and converts it to a map. If the input is not a list or not a keyword list, it returns the input unchanged.

Parameters

  • list - Input that may be a keyword list, regular list, or other type

Examples

iex> FatEcto.SharedHelper.filterable_opt_to_map([name: ["$LIKE"], age: ["$GT"]])
%{"name" => ["$LIKE"], "age" => ["$GT"]}

iex> FatEcto.SharedHelper.filterable_opt_to_map(%{"name" => ["$LIKE"]})
%{"name" => ["$LIKE"]}

get_primary_keys(query)

@spec get_primary_keys(Ecto.Query.t()) :: [atom()] | nil

Retrieves the primary keys for a given query.

Parameters

  • query: The Ecto query.

Examples

iex> FatEcto.SharedHelper.get_primary_keys(from(u in User))
[:id]

implements_behaviour?(module, behaviour)

@spec implements_behaviour?(module(), module()) :: boolean()

Checks if a module implements the given behaviour by inspecting its __info__/1 metadata.

Parameters

  • module: The module to check.
  • behaviour: The behaviour to validate against.

Examples

iex> implements_behaviour?(MyApp.Repo, Ecto.Repo)
true

iex> implements_behaviour?(NotARepo, Ecto.Repo)
false

keyword_list_to_map(list)

@spec keyword_list_to_map(keyword() | map()) :: map()

Converts a keyword list to a map with string keys.

If the input is already a map or not a keyword list, returns it unchanged.

Parameters

  • list - A keyword list or map to convert

Examples

iex> FatEcto.SharedHelper.keyword_list_to_map([name: "John", age: 25])
%{"name" => "John", "age" => 25}

iex> FatEcto.SharedHelper.keyword_list_to_map(%{"name" => "John"})
%{"name" => "John"}

keyword_list_to_map_with_uppercase_operators(list)

@spec keyword_list_to_map_with_uppercase_operators(keyword() | map()) :: map()

Converts a keyword list to a map with string keys and normalizes operator values to uppercase.

This function is specifically designed for processing filterable options where operator values need to be normalized to uppercase format.

Parameters

  • list - A keyword list or map to convert

Examples

iex> FatEcto.SharedHelper.keyword_list_to_map_with_uppercase_operators([name: ["like"], age: ["gt"]])
%{"name" => ["LIKE"], "age" => ["GT"]}

parse_integer!(int_str)

@spec parse_integer!(any()) :: integer() | nil

Parses an integer from a string or returns the integer if already an integer.

Returns the parsed integer on success or nil on failure.

string_to_atom(already_atom)

@spec string_to_atom(String.t() | atom()) :: atom()

Converts a string to an atom.

Parameters

  • str: The string to convert.

Examples

iex> FatEcto.SharedHelper.string_to_atom("example")
:example

string_to_existing_atom(already_atom)

@spec string_to_existing_atom(String.t() | atom()) :: atom()

Converts a string to an existing atom.

Parameters

  • str: The string to convert.

Examples

iex> FatEcto.SharedHelper.string_to_existing_atom("example")
:example

to_date(date)

@spec to_date(String.t() | Date.t() | DateTime.t()) ::
  {:ok, Date.t()} | {:error, atom()}

Converts various date/datetime inputs to an Elixir Date struct.

Accepts:

  • String date (e.g., "2023-12-25")
  • String datetime (e.g., "2023-12-25T10:30:00Z", "2023-12-25 10:30:00")
  • Elixir Date struct
  • Elixir DateTime struct

Returns {:ok, date} on success or {:error, reason} on failure.

Examples

iex> FatEcto.SharedHelper.to_date("2023-12-25")
{:ok, ~D[2023-12-25]}

iex> FatEcto.SharedHelper.to_date("2023-12-25T10:30:00Z")
{:ok, ~D[2023-12-25]}

iex> FatEcto.SharedHelper.to_date(~D[2023-12-25])
{:ok, ~D[2023-12-25]}

iex> FatEcto.SharedHelper.to_date(~U[2023-12-25 10:30:00Z])
{:ok, ~D[2023-12-25]}

to_date!(input)

@spec to_date!(String.t() | Date.t() | DateTime.t()) :: Date.t()

Converts various date/datetime inputs to an Elixir Date struct.

Similar to to_date/1 but raises an exception on failure.

Examples

iex> FatEcto.SharedHelper.to_date!("2023-12-25")
~D[2023-12-25]

iex> FatEcto.SharedHelper.to_date!(~U[2023-12-25 10:30:00Z])
~D[2023-12-25]