https://github.com/up2jj/conditioner
Conditional logic utility
https://github.com/up2jj/conditioner
boolean-logic elixir elixir-lang logic
Last synced: over 1 year ago
JSON representation
Conditional logic utility
- Host: GitHub
- URL: https://github.com/up2jj/conditioner
- Owner: up2jj
- License: apache-2.0
- Created: 2023-04-03T18:55:33.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-19T18:24:14.000Z (about 3 years ago)
- Last Synced: 2025-01-29T10:16:02.141Z (over 1 year ago)
- Topics: boolean-logic, elixir, elixir-lang, logic
- Language: Elixir
- Homepage:
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Conditioner
[](https://hex.pm/packages/conditioner)
---
## Introduction
Conditioner allows you to define and process conditional logic in separated way:
1. Create logical representation of conditions:
```elixir
conditions = %{
"and" => [
["filename", "containsfn", "he"],
["filename", "containsfn", "lo"],
["otherrule", "contains", "lo"],
%{
"or" => [
["filename", "containsfn", "bo"],
["filename", "containsfn", "he"],
%{"and" => true}
]
}
]
}
```
2. Define matcher module with rules:
```elixir
defmodule SomeMatcher do
# using Conditioner.Matcher is optional, but module provides some convenient functions
use Conditioner.Matcher
def match?(["filename", "containsfn", str], _original_value) do
# match?/2 can return anonymous function or boolean value
fn val ->
String.contains?(val, str)
end
end
def match?(["otherrule", "contains", str], _original_value) do
String.contains?(value, str)
end
def match?("hello", "hello") do
# rule pattern can by anything, i.e. plain string
true
end
end
```
3. Verify conditions by calling matcher with rules:
```elixir
result = Conditioner.match?(conditions, "hello", SomeMatcher)
```
## Goals
1. Conditions are represented as map, so they can be easily serialized and stored,
2. Rules can be represented as any type, as long as rule can be matched by pattern matching mechanism in Elixir,
3. Matcher can be defined as module or anonymous function.
## Changelog
* 0.2.2 - docs improvements, add custom caller exception,
* 0.2.1 - support defining matcher as fun with arity 2,
* 0.2.0 - changed `Conditioner.Matcher.match/3` function signature to `match?/3`, docs improvements,
* 0.1.0 - initial version.
## Installation
```elixir
def deps do
[
{:conditioner, "~> 0.2.2"}
]
end
```
The docs can be found at .