Skip to content

Commit

Permalink
add a clear method (adobe#15)
Browse files Browse the repository at this point in the history
* made some change to make it works with core

* add a clear method

Co-authored-by: jgeng <[email protected]>
  • Loading branch information
shalehaha and jiabingeng authored Aug 10, 2020
1 parent 433ec80 commit df4dd8d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Sources/SwiftRulesEngine/RulesEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import Foundation

public typealias RulesTracer = (Bool, Evaluable, Context, RulesFailure?) -> Void
public typealias RulesTracer = (Bool, Rule, Context, RulesFailure?) -> Void

public class RulesEngine<T: Rule> {
let evaluator: Evaluating
Expand All @@ -33,7 +33,7 @@ public class RulesEngine<T: Rule> {
return rules.filter { rule -> Bool in
let result = rule.condition.evaluate(in: context)
if let tracer = self.tracer {
tracer(result.value, rule.condition, context, result.error)
tracer(result.value, rule, context, result.error)
}
return result.value
}
Expand All @@ -46,11 +46,16 @@ public class RulesEngine<T: Rule> {
self.rules += rules
}

/// clear the current rules set
public func clearRules() {
rules = [T]()
}

/// trace the result of each rule eveluation
/// - Parameter tracer: the rules tracer will be called after each rule eveluation
public func trace(with tracer: @escaping RulesTracer) {
self.tracer = tracer
}

// TODO: Query of rules?
}

public struct Context {
Expand Down

0 comments on commit df4dd8d

Please sign in to comment.