View Source Resourceful.Collection.Filter (Resourceful v0.1.5)
Provides a common interface for filtering collections. See call/2
for use
and examples.
This module is intended to dispatch arguments to the appropriate Filter
module for the underlying data source.
Filtering is not meant to replace and be anything even resembling feature complete with more robust querying options provided by various databases. As The focus is on edge-facing APIs, generally web-based APIs, filthering is meant to be much simpler and more predictable. For instance, wildcard and regular expression filtering are omitted specifically by default. This is intentional.
Link to this section Summary
Functions
Returns a data source that is filtered in accordance with filters
.
Converts an argument into an appropriate filter parameter. A castd filter is a tuple of containing an atom for the field name, an atom of the function name that will be called by the deligated module, and the value that will be used for comparison.
Checks whether or not an operator is valid.
Checks whether or not an operator is valid in conjunction with an intended value. This can be used to validate the data from a client query.
Link to this section Types
@type t() :: {Resourceful.Collection.queryable(), String.t(), any()}
Link to this section Functions
Returns a data source that is filtered in accordance with filters
.
If data_source
is not an actual list of resources (e.g. an Ecto Queryable)
underlying modules should not return a list of resources, but rather a
filtered version of data_source
.
args
Args
data_source
: SeeResourceful.Collection
module overview.filters
: A list of filters. Seecast/1
for a list of valid filters.
Converts an argument into an appropriate filter parameter. A castd filter is a tuple of containing an atom for the field name, an atom of the function name that will be called by the deligated module, and the value that will be used for comparison.
Filter parameters can be provded as a tuple, list, or string and will be
castd to the appropriate format. Invalid operators and their respective
values will result in an exception. Please see valid_operator?/2
if you want
to ensure client provided data is valid first.
(Note: Should this throw an exception or should it return an :error tuple by default?)
examples
Examples
cast({:age, "gte", 18})
cast(["age", "gte", 18])
cast(["age gte", 18])
Checks whether or not an operator is valid.
args
Args
op
: Intended operator. Valid operators are keys in@shorthand
.
Checks whether or not an operator is valid in conjunction with an intended value. This can be used to validate the data from a client query.
args
Args
op
: Seevalid_operator?/1
.val
: The value to be used with the operator. Certain operators only work on a subset of value types. For instancesw
is only valid with strings.