View Source Conditioner
introduction
Introduction
Conditioner allows you to define and process conditional logic in separated way:
- Create logical representation of conditions:
conditions = %{
"and" => [
["filename", "containsfn", "he"],
["filename", "containsfn", "lo"],
["otherrule", "contains", "lo"],
%{
"or" => [
["filename", "containsfn", "bo"],
["filename", "containsfn", "he"],
%{"and" => true}
]
}
]
}
- Define matcher module with rules:
defmodule SomeMatcher do
use Conditioner.Matcher
def match?(["filename", "containsfn", str], _original_value) do
fn val ->
String.contains?(val, str)
end
end
def match?(["otherrule", "contains", str], value) do
String.contains?(value, str)
end
end
- Verify conditions by calling matcher with rules:
result = Conditioner.match?(conditions, "hello", SomeMatcher)
goals
Goals
Conditions are represented as map, so they can be easily serialized and stored,
Rules can be represented as any type, as long as rule can be matched by pattern matching mechanism in Elixir.
changelog
Changelog
0.2.1 - support defining matcher as fun with arity 2,
0.2.0 - changed
Conditioner.Matcher.match/3
function signature tomatch?/3
, docs improvements,0.1.0 - initial version.
installation
Installation
def deps do
[
{:conditioner, "~> 0.2.1"}
]
end
The docs can be found at https://hexdocs.pm/conditioner.