From df4dd8d0112eed11972649ff28b8e07f17125507 Mon Sep 17 00:00:00 2001 From: Jiabin Geng Date: Mon, 10 Aug 2020 11:44:10 -0600 Subject: [PATCH] add a clear method (#15) * made some change to make it works with core * add a clear method Co-authored-by: jgeng --- Sources/SwiftRulesEngine/RulesEngine.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/SwiftRulesEngine/RulesEngine.swift b/Sources/SwiftRulesEngine/RulesEngine.swift index e699259..e074337 100644 --- a/Sources/SwiftRulesEngine/RulesEngine.swift +++ b/Sources/SwiftRulesEngine/RulesEngine.swift @@ -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 { let evaluator: Evaluating @@ -33,7 +33,7 @@ public class RulesEngine { 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 } @@ -46,11 +46,16 @@ public class RulesEngine { 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 {