View Source Tablex.Rules (tablex v0.2.0-alpha.1)

High level rule APIs for Tablex.

With rule APIs, one can:

  • find rules by a given set of inputs,
  • update an existing rule,
  • or create a new rule.

Link to this section Summary

Functions

Find rules by a given set of inputs.

Check if the given value matches the asserting expectation.

Update an existing rule.

Update an existing rule by input.

Link to this section Types

@type rule() :: Tablex.Table.rule()
@type table() :: Tablex.Table.t()
@type update() :: {:input, [any()] | map()} | {:output, [any()] | map()}
@type updates() :: [update()]

Link to this section Functions

@spec get_rules(
  table(),
  keyword()
) :: [rule()]

Find rules by a given set of inputs.

parameters

Parameters

  • table: the table to find rules for
  • args: the set of inputs to find rules for

returns

Returns

A list of rules matching the given set of inputs. The ordering of the list follows the priority of the rules, with the lower-priority rule taking more precedence.

example

Example

iex> table = Tablex.new(""" ...> F value || color ...> 1 >90 || red ...> 2 80..90 || orange ...> 3 20..79 || green ...> 4 <20 || blue ...> """) ...> Tablex.Rules.get_rules(table, %{}) [] ...> Tablex.Rules.get_rules(table, %{value: 80}) [%Tablex.Rules.Rule{id: 2, inputs: [{[:value], 80..90}], outputs: [{[:color], "orange"}]}]

This example shows how the returned rules are ordered by priority:

iex> table = Tablex.new(""" ...> M country state || feature_enabled ...> 1 US CA || true ...> 2 US - || false ...> 3 CA - || true ...> """) ...> Tablex.Rules.get_rules(table, %{country: "US", state: "CA"}) [

%Tablex.Rules.Rule{id: 2, inputs: [{[:country], "US"}, {[:state], :any}], outputs: [{[:feature_enabled], false}]},
%Tablex.Rules.Rule{id: 1, inputs: [{[:country], "US"}, {[:state], "CA"}], outputs: [{[:feature_enabled], true}]}

]

Nested inputs are supported.

iex> table = Tablex.new(""" ...> F foo.value || color ...> 1 >90 || red ...> 2 80..90 || orange ...> 3 20..79 || green ...> 4 <20 || blue ...> """) ...> Tablex.Rules.get_rules(table, %{foo: %{value: 80}}) [%Tablex.Rules.Rule{id: 2, inputs: [{[:foo, :value], 80..90}], outputs: [{[:color], "orange"}]}]

Link to this function

match_expect?(expect, value)

View Source

Check if the given value matches the asserting expectation.

Link to this function

update_rule(table, id, updates)

View Source
@spec update_rule(table(), integer(), updates()) :: table()

Update an existing rule.

example

Example

A basic example of updating a rule:

iex> table = Tablex.new(""" ...> F value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, input: [80..90], output: ["orange"]) ...> table.rules [[1, input: [80..90], output: ["orange"]]]

You can also updte a rule with changes in a map format:

iex> table = Tablex.new(""" ...> F value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, input: %{value: 80..90}, output: %{color: "orange"}) ...> table.rules [[1, input: [80..90], output: ["orange"]]]

You can only update input values:

iex> table = Tablex.new(""" ...> F value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, input: %{value: 80..90}) ...> table.rules [[1, input: [80..90], output: ["red"]]]

You can only update output values:

iex> table = Tablex.new(""" ...> F value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, output: %{color: "orange"}) ...> table.rules [[1, input: [:any], output: ["orange"]]]

For updating nested input or output values, both nested map or direct values are supported:

iex> table = Tablex.new(""" ...> F target.value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, input: %{target: %{value: 80..90}}, output: %{color: "orange"}) ...> table.rules [[1, input: [80..90], output: ["orange"]]]

iex> table = Tablex.new(""" ...> F target.value || color ...> 1 - || red ...> """) ...> table = Tablex.Rules.update_rule(table, 1, input: [80..90], output: ["orange"]) ...> table.rules [[1, input: [80..90], output: ["orange"]]]

Link to this function

update_rule_by_input(table, input, output_updates)

View Source
@spec update_rule_by_input(table(), map(), map()) :: table()

Update an existing rule by input.