Membrane v0.1.0 Membrane.Query View Source

Membrane.Query module evaluates query against a map or struct. It evaluates every condition through Membrane.Parser module.

Query Conditions

The query conditions are divided into 4 types.

Logical Conditions

These are conditions which are handled by Membrane.Parser.LogicalParser module. The condition will be of format keyword: value.

KeywordMeaning
:gtGreater than
:gteGreater than or equal to
:ltLesser than
:lteLesser than or equal to
:eqEqual to
:neqNot Equal to

Examples

iex> data = %{a: 20, b: "hello", c: [1, 2, 3], d: %{e: 30}}
iex> Membrane.Query.process(data, a: [gt: 10])
true
iex> Membrane.Query.process(data, a: [lt: 15])
false
# Underneath, its a simple greater or lesser operator
# Hence it also supports strings or mixed types
iex> Membrane.Query.process(data, b: [lt: "Hello"]) # => "hello" < "Hello"
false
iex> Membrane.Query.process(data, b: [gte: 5]) # => "hello" >= 5
true
# With multiple conditions
iex> Membrane.Query.process(data, a: [lte: 20], c: [eq: [1, 2, 3]], d: [e: [gt: 20]])
true

Atom Conditions

These are conditions which are handled by Membrane.Parser.AtomParser module. The condition will be of format attribute: keyword.

KeywordMeaning
:existsTrue if attribute exists
:notexistsTrue if attribute does not exist
:integerTrue if the attrbute is an integer
:floatTrue if the attrbute is a float
:numberTrue if the attrbute is a number
:stringTrue if the attrbute is a string
:listTrue if the attrbute is a list
:mapTrue if the attrbute is a map

Examples

iex> data = %{a: 20, b: "hello", c: [1, 2, 3], d: %{e: 30}}
iex> Membrane.Query.process(data, a: :exists)
true
iex> Membrane.Query.process(data, d: [e: :notexists])
false
iex> Membrane.Query.process(data, b: :string)
true

Property Conditions

These are conditions which are handled by Membrane.Parser.PropertyParser module. The condition will be of format keyword: value | condition.

This also handles a Regex value. It returns true if the attribute’s value matches with regex

KeywordMeaning
:lenLength of the attribute should be equal to value, if condition is passed then evaluation is aginst its length

Examples

iex> data = %{a: 20, b: "hello", c: [1, 2, 3], d: %{e: 30}}
iex> Membrane.Query.process(data, c: [len: 3])
true
# Length against a number doesn't exist.
iex> Membrane.Query.process(data, a: [len: 10])
false
# With Regex
iex> Membrane.Query.process(data, b: ~r'\w+ll\w+')
true
# Internally a number is converted to a string, Hence regex works even against a number
iex> Membrane.Query.process(data, d: [e: ~r'\d+'])
true

List Conditions

These are conditions which are handled by Membrane.Parser.ListParser module. The condition will be of format keyword: value. In this condition either attribute’s value or keyword’s value has to be a list

KeywordMeaning
:inTrue if value of attribute is in keyword’s value
:ninTrue if value of attribute is not in keyword’s value
:hasTrue if value of attribute has keyword’s value
:nhaTrue if value of attribute does not have keyword’s value

Examples

iex> data = %{a: 20, b: "hello", c: [1, 2, 3], d: %{e: 30}}
iex> Membrane.Query.process(data, a: [in: [10, 20, 30]])
true
iex> Membrane.Query.process(data, c: [has: 2])
true
iex> Membrane.Query.process(data, d: [e: [nin: [10, 20, 30]]])
false

Link to this section Summary

Functions

Evaluates the struct against a query and returns true or false

Link to this section Functions

Link to this function process(document, list) View Source
process(map() | struct(), list()) :: boolean()

Evaluates the struct against a query and returns true or false.

Examples

iex> alias Membrane.Query
Membrane.Query
iex> document = %{a: 100, b: 20, c: -1, meta: %{creator: "max"}}
%{a: 100, b: 20, c: -1, meta: %{creator: "max"}}
iex> Query.process(document, a: :exists, meta: [creator: "max"])
true
iex> Query.process(document, a: :exists, c: [gt: 0])
false