LiveFilter.FilterGroup (LiveFilter v0.1.0)

View Source

Represents a group of filters with a conjunction (AND/OR) and can contain nested groups.

Summary

Functions

Adds a filter to the group.

Adds a nested group.

Counts total filters including nested groups.

Checks if the group has any active filters.

Creates a new filter group.

Removes a filter from the group by index.

Updates a filter in the group by index.

Types

t()

@type t() :: %LiveFilter.FilterGroup{
  conjunction: :and | :or,
  filters: [LiveFilter.Filter.t()],
  groups: [t()]
}

Functions

add_filter(group, filter)

@spec add_filter(t(), LiveFilter.Filter.t()) :: t()

Adds a filter to the group.

Examples

iex> group = %FilterGroup{}
iex> filter = %Filter{field: :title, operator: :contains, value: "test", type: :string}
iex> FilterGroup.add_filter(group, filter)
%FilterGroup{filters: [%Filter{field: :title, operator: :contains, value: "test", type: :string}]}

add_group(group, nested_group)

@spec add_group(t(), t()) :: t()

Adds a nested group.

Examples

iex> parent = %FilterGroup{conjunction: :and}
iex> child = %FilterGroup{conjunction: :or, filters: [%Filter{field: :status, operator: :equals, value: "active", type: :enum}]}
iex> FilterGroup.add_group(parent, child)
%FilterGroup{conjunction: :and, groups: [%FilterGroup{conjunction: :or, filters: [...]}]}

count_filters(group)

@spec count_filters(t()) :: non_neg_integer()

Counts total filters including nested groups.

Examples

iex> FilterGroup.count_filters(%FilterGroup{})
0

iex> filter = %Filter{field: :title, operator: :contains, value: "test", type: :string}
iex> FilterGroup.count_filters(%FilterGroup{filters: [filter]})
1

has_filters?(group)

@spec has_filters?(t()) :: boolean()

Checks if the group has any active filters.

Recursively checks nested groups for filters.

Examples

iex> FilterGroup.has_filters?(%FilterGroup{})
false

iex> filter = %Filter{field: :title, operator: :contains, value: "test", type: :string}
iex> FilterGroup.has_filters?(%FilterGroup{filters: [filter]})
true

new(attrs \\ %{})

@spec new(map() | keyword()) :: t()

Creates a new filter group.

Parameters

  • attrs - A map or keyword list of filter group attributes

Examples

iex> FilterGroup.new()
%FilterGroup{filters: [], groups: [], conjunction: :and}

iex> FilterGroup.new(%{conjunction: :or})
%FilterGroup{filters: [], groups: [], conjunction: :or}

remove_filter(group, index)

@spec remove_filter(t(), non_neg_integer()) :: t()

Removes a filter from the group by index.

Examples

iex> filter = %Filter{field: :title, operator: :contains, value: "test", type: :string}
iex> group = %FilterGroup{filters: [filter]}
iex> FilterGroup.remove_filter(group, 0)
%FilterGroup{filters: []}

update_filter(group, index, filter)

@spec update_filter(t(), non_neg_integer(), LiveFilter.Filter.t()) :: t()

Updates a filter in the group by index.

Examples

iex> old_filter = %Filter{field: :title, operator: :contains, value: "test", type: :string}
iex> new_filter = %Filter{field: :title, operator: :equals, value: "exact", type: :string}
iex> group = %FilterGroup{filters: [old_filter]}
iex> FilterGroup.update_filter(group, 0, new_filter)
%FilterGroup{filters: [%Filter{field: :title, operator: :equals, value: "exact", type: :string}]}