Rulex
Rulex allow you to define rules just as you define functions, a simple macro wrapper add the needed code in order to not match the same rule twice. It creates a recursive function apply_rules
which returns the accumulator when no rule match. Each rule can return an error, the rules will continue to apply and errors will be listed in the response tuple.
defrule rulename(matchingspec,acc) :: {:ok,newacc} | {:error,errorterm}
Example usage :
iex> defmodule MyRules do
...> use Rulex
...> defrule my_first_rule("y"<>_,string_desc), do:
...> {:ok,[:starts_with_y|string_desc]}
...> defrule my_second_rule("yahoo",_string_desc), do:
...> {:error,:yahoo_is_err}
...> defrule my_third_rule("ya"<>_,string_desc), do:
...> {:ok,[:starts_with_ya|string_desc]}
...> defrule my_fourth_rule("b"<>_,string_desc), do:
...> {:ok,[:starts_with_b|string_desc]}
...> defrule my_fifth_rule(param,string_desc) do
...> false = param == "yahoo"
...> {:ok,string_desc}
...> rescue _->{:error,:yahoo_not_expected}
...> end
...> end
...> MyRules.apply_rules("yahoo",[])
{[:starts_with_ya,:starts_with_y],[:yahoo_not_expected,:yahoo_is_err]}