View Source Statux.ValueRules (Statux v0.1.4)
Provides the rules handling for the status system.
Expects to get the actual value and the value constraints of states. Returns if the state is valid or not.
Link to this section Summary
Functions
Takes a set of options and runs through them.
Takes a set of rules and runs through them. Returns the first status that the value rules match for. May be used to check just a subset of validators. Keys that do not exist in the rule set are ignored.
Pass in a value and a rule set to check wether the value confirms to the rules or not.
Link to this section Functions
Takes a set of options and runs through them.
Returns the first option that the value rules match for.
When the options are nil
, what might happen when a set of options is
selected that does not exist, an empty list is returned.
Example
iex> battery_alarm_options = %{
...> critical: %{
...> constraints: %{count: %{min: 3}, duration: %{min: "PT10M" |> Timex.Duration.parse!()}},
...> value: %{lt: 11.8}
...> },
...> low: %{
...> constraints: %{count: %{min: 3}, duration: %{min: "PT10M" |> Timex.Duration.parse!()}},
...> value: %{max: 12.0, min: 11.8}
...> },
...> ok: %{constraints: %{count: %{min: 3}}, value: %{gt: 12.1}}
...> }
...> find_possible_valid_status(12.6, battery_alarm_options)
[:ok]
iex> find_possible_valid_status(11.8, battery_alarm_options)
[:low]
iex> find_possible_valid_status(11.3, battery_alarm_options)
[:critical]
iex> find_possible_valid_status(11.3, nil)
[]
find_possible_valid_status(value, rules_as_map, order_of_status_checks)
View SourceTakes a set of rules and runs through them. Returns the first status that the value rules match for. May be used to check just a subset of validators. Keys that do not exist in the rule set are ignored.
Similar to :find_possible_valid_status, but executes the checks in the given order. If your rules are done properly, the order should not matter. However, this can be used to find out conflicts, i.e. when you have rules that overlap and the first rule that matches is taken.
Example
Both :min and :max include the set value, so the following rule set has a conflict for the value 11.8:
iex> rules = %{
...> overlapping: %{
...> low: %{
...> value: %{max: 11.8}
...> },
...> high: %{
...> value: %{min: 11.8}
...> },
...> }
...> }
...> find_possible_valid_status(11.8, rules.overlapping, [:low, :high])
[:high, :low]
iex> find_possible_valid_status(11.8, rules.overlapping, [:high, :low])
[:low, :high]
iex> find_possible_valid_status(11.8, rules.overlapping, [:not_valid, :low])
[:low]
You may specify specific :return_as values to customize what is returned for valid states other than their status name:
iex> rules = %{
...> return: %{
...> one: %{
...> value: %{max: 11.8}, return_as: 1,
...> },
...> two: %{
...> value: %{min: 11.8}, return_as: "2",
...> },
...> },
...> }
...> find_possible_valid_status(11.8, rules.overlapping, [:one, :two])
["2", 1]
Pass in a value and a rule set to check wether the value confirms to the rules or not.
Valid rules are:
numeric | :max , :min , :lt , :gt | |
string | :contains , :starts_with , :ends_with , :does_not_contain # TODOL Implement | |
any value | :is , :not |
Example
iex> valid?(12, %{max: 12})
true
iex> valid?(12, %{min: 12})
true
iex> valid?(12, %{lt: 12})
false
iex> valid?(12, %{gt: 12})
false
iex> valid?(11.9, %{lt: 12})
true
iex> valid?(12.1, %{gt: 12})
true
iex> valid?(11.5, %{min: 11.7, max: 12})
false
iex> valid?(11.7, %{is: 11.7})
true
iex> valid?(11.5, %{is: 11.7})
false
iex> valid?(11.5, %{not: 11.7})
true
iex> valid?(11.7, %{not: 11.7})
false
iex> valid?(11.5, %{is: [11.5, 11.7]})
true
iex> valid?(11.6, %{is: [11.5, 11.7]})
false
iex> valid?(11.5, %{not: [11.5, 11.7]})
false
iex> valid?(11.6, %{not: [11.5, 11.7]})
true
iex> valid?(11.6, %{max: 12, min: 10, not: [11.6]})
false
iex> valid?(11, %{max: 12, min: 10, not: [11.6]})
true
iex> valid?(9, %{max: 12, min: 10, not: [11.6]})
false
iex> valid?(:no, %{max: 12, min: 10, not: [:no]})
false
iex> valid?(:yes, %{max: 12, min: 10, not: [:no]})
true
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -10), %{min: "PT8M" |> Timex.Duration.parse!()})
true
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -5), %{min: "PT8M" |> Timex.Duration.parse!()})
false
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -5), %{max: "PT8M" |> Timex.Duration.parse!()})
true
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -10), %{max: "PT8M" |> Timex.Duration.parse!()})
false
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -10), %{is: "PT10M" |> Timex.Duration.parse!()})
true
iex> valid?(DateTime.utc_now() |> Timex.shift(minutes: -10), %{not: "PT10M" |> Timex.Duration.parse!()})
false
iex> valid?("test", %{not: "test"})
false
iex> valid?("test", %{match: ~r(test)})
true
iex> valid?("test", %{contains: "test"})
true
iex> valid?("test", %{is: "test"})
true
iex> valid?("test", %{is: ["test", "world"]})
true
iex> valid?("test", %{not: ["test", "world"]})
false
iex> valid?("ab_test_cd", %{match: [~r(\d{4})]})
false
iex> valid?("ab_1234_cd", %{match: [~r(\d{4})]})
true
iex> valid?("ab_1234_cd", %{match: [~r(\d{8}), ~r(_\d{4}_)]})
true