Filtrex v0.3.0 Filtrex.Condition behaviour

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

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

Functions

param_key_type(configs, key_with_comparator)

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

parse(configs, map)

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)

Specs

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)

Specs

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)

Specs

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

Helper method to validate that a comparator is in list

validate_in(value, list)

Specs

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

Helper method to validate whether a value is in a list

validate_is_binary(value)

Specs

validate_is_binary(any) :: nil | String.t

Helper method to validate whether a value is a binary

Callbacks

comparators()

Specs

comparators :: [String.t]
parse(arg0, %{})

Specs

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

Specs

type :: Atom.t