Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz authored Aug 9, 2022
1 parent 986e64d commit fe8da56
Showing 1 changed file with 6 additions and 37 deletions.
43 changes: 6 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ eRules supports Scala 2.13 and 3

**Sbt**
```sbt
libraryDependencies += "com.github.geirolz" %% "erules-core" % "0.0.4"
libraryDependencies += "com.github.geirolz" %% "erules-core" % "@VERSION@"
```


Expand All @@ -33,7 +33,7 @@ and some other information like the execution time.
## How to use

Given these data classes
```scala
```scala mdoc:to-string
case class Country(value: String)
case class Age(value: Int)

Expand All @@ -51,7 +51,7 @@ Assuming we want to check:
- The person has a UK citizenship

Let's write the rules!
```scala
```scala mdoc:to-string
import erules.core.Rule
import erules.core.RuleVerdict.*
import cats.data.NonEmptyList
Expand All @@ -62,14 +62,12 @@ val checkCitizenship: Rule[Id, Citizenship] =
case Citizenship(Country("UK")) => Allow.withoutReasons
case _ => Deny.because("Only UK citizenship is allowed!")
}
// checkCitizenship: Rule[Id, Citizenship] = RuleImpl(<function1>,Check UK citizenship,None,None)

val checkAdultAge: Rule[Id, Age] =
Rule("Check Age >= 18").apply[Id, Age] {
case a: Age if a.value >= 18 => Allow.withoutReasons
case _ => Deny.because("Only >= 18 age are allowed!")
}
// checkAdultAge: Rule[Id, Age] = RuleImpl(<function1>,Check Age >= 18,None,None)

val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
checkCitizenship
Expand All @@ -79,7 +77,6 @@ val allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList.of(
.targetInfo("age")
.contramap(_.age)
)
// allPersonRules: NonEmptyList[Rule[Id, Person]] = NonEmptyList(RuleImpl(scala.Function1$$Lambda$13639/0x0000000803515040@67d92594,Check UK citizenship,None,Some(citizenship)), RuleImpl(scala.Function1$$Lambda$13639/0x0000000803515040@38c7f34f,Check Age >= 18,None,Some(age)))
```

N.B. Importing even the `erules-generic` you can use macro to auto-generate the target info using `contramapTarget` method.
Expand All @@ -92,53 +89,25 @@ We can evaluate rules in two different ways:
- denyAllNotAllowed
- allowAllNotDenied

```scala
```scala mdoc:to-string
import erules.core.*
import erules.implicits.*
import cats.effect.IO
import cats.effect.unsafe.implicits.*

val person: Person = Person("Mimmo", "Rossi", Age(16), Citizenship(Country("IT")))
// person: Person = Person(Mimmo,Rossi,Age(16),Citizenship(Country(IT)))

val result: IO[EngineResult[Person]] = for {
engine <- RulesEngine[IO].withRules[Id, Person](allPersonRules).denyAllNotAllowed
result <- engine.parEval(person)
} yield result
// result: IO[EngineResult[Person]] = IO(...)

//yolo
result.unsafeRunSync().asReport[String]
// res0: String = ###################### ENGINE VERDICT ######################
//
// Data: Person(Mimmo,Rossi,Age(16),Citizenship(Country(IT)))
// Rules: 2
// Interpreter verdict: Denied
//
// ------------ Check UK citizenship for citizenship -----------
// - Rule: Check UK citizenship
// - Description:
// - Target: citizenship
// - Execution time: 144042 nanoseconds
//
// - Verdict: Success(Deny)
// - Because: Only UK citizenship is allowed!
// ------------------------------------------------------------
// ------------------ Check Age >= 18 for age -----------------
// - Rule: Check Age >= 18
// - Description:
// - Target: age
// - Execution time: 7917 nanoseconds
//
// - Verdict: Success(Deny)
// - Because: Only >= 18 age are allowed!
// ------------------------------------------------------------
//
//
// ############################################################
```


### Modules
- [erules-generic](https://github.com/geirolz/erules/tree/main/modules/generic/src)
- [erules-generic](https://github.com/geirolz/erules/tree/main/modules/generic)
- [erules-circe](https://github.com/geirolz/erules/tree/main/modules/circe)
- [erules-scalatest](https://github.com/geirolz/erules/tree/main/modules/scalatest)

0 comments on commit fe8da56

Please sign in to comment.