Filtrex.Condition behaviour (Filtrex v0.5.0)

Copy Markdown View Source

Filtrex.Condition is an abstract module for parsing conditions. To implement your own condition, add use Filtrex.Condition in your module and implement the three callbacks:

  • parse/2 - produce a condition struct from a configuration and attributes
  • type/0 - the description of the condition that must match the underscore version of the module's last namespace
  • comparators/0 - the list of used query comparators for parsing params

Summary

Functions

List out the available condition modules

Parses a params key into the condition type, column, and comparator

Parses a condition by dynamically delegating to modules

Generates an error description for a generic parse error

Generates an error description for a parse error resulting from an invalid value type

Helper method to validate that a comparator is in list

Helper method to validate whether a value is in a list

Helper method to validate whether a value is a binary

Callbacks

comparators()

@callback comparators() :: [String.t()]

parse(t, map)

@callback parse(Filtrex.Type.Config.t(), %{
  inverse: boolean(),
  column: String.t(),
  value: any(),
  comparator: String.t()
}) :: {:ok, any()} | {:error, any()}

type()

@callback type() :: Atom.t()

Functions

condition_modules()

List out the available condition modules

param_key_type(configs, key_with_comparator)

Parses a params key into the condition type, column, and comparator

parse(configs, options)

Parses a condition by dynamically delegating to modules

It delegates based on the type field of the options map (e.g. Filtrex.Condition.Text for the type "text"). Example Input: config:

Filtrex.Condition.parse([
  %Filtrex.Type.Config{type: :text, keys: ~w(title comments)}
], %{
  type: string,
  column: string,
  comparator: string,
  value: string,
  inverse: boolean                   # inverts the comparator logic
})

parse_error(value, type, filter_type)

@spec parse_error(any(), Atom.t(), Atom.t()) :: String.t()

Generates an error description for a generic parse error

parse_value_type_error(column, filter_type)

@spec parse_value_type_error(any(), Atom.t()) :: String.t()

Generates an error description for a parse error resulting from an invalid value type

validate_comparator(type, comparator, comparators)

@spec validate_comparator(atom(), binary(), List.t()) ::
  {:ok, binary()} | {:error, binary()}

Helper method to validate that a comparator is in list

validate_in(value, list)

@spec validate_in(any(), List.t()) :: nil | any()

Helper method to validate whether a value is in a list

validate_is_binary(value)

@spec validate_is_binary(any()) :: nil | String.t()

Helper method to validate whether a value is a binary