-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone-business-rule-processor.js
73 lines (53 loc) · 1.92 KB
/
backbone-business-rule-processor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(function(Backbone) {
"use strict";
Backbone.BusinessRuleProcessor = {};
Backbone.BusinessRuleProcessor._evaluateRuleString = function(options) {
if (!options.facts || !(options.facts instanceof Backbone.Model)) {
throw 'facts must be a backbone model';
}
var facts = options.facts.attributes;
var ruleString = options.ruleString;
//TODO: could be made more robust
return eval(ruleString);
};
Backbone.BusinessRuleProcessor.processRule = function(options) {
var rule = options.rule;
var facts = options.facts;
if (typeof rule === 'string') { //allow for a rules with one clause to be strings
rule = [rule];
}
if (typeof rule === 'boolean') {
return rule;
}
else if (typeof rule === 'function') {
return rule();
}
else if (Array.isArray(rule)) {
if (rule.length === 0) {
throw 'rule cannot be an empty array';
}
return (function () {
//right now the idea is that all of the array parameters should be strings and that we are AND'ing them
//TODO: add more flexibility to this (ex: other boolean logic (OR, NOT))
var i,rulePartial,partialResult,result;
for (i=0;i<rule.length;i++) {
if (typeof rule[i] !== 'string') {
throw 'all rule elements must be strings';
}
}
rulePartial = rule[0];
partialResult = Backbone.BusinessRuleProcessor._evaluateRuleString({ruleString : rule[0], facts : facts});
result = partialResult;
for(i=1; i<rule.length;i++) {
rulePartial=rule[i];
partialResult = Backbone.BusinessRuleProcessor._evaluateRuleString({ruleString : rulePartial, facts : facts});
result = result && partialResult; //TODO: AND is currently the only way rule partials are combined
}
return result;
})();
}
else {
throw 'invalid type';
}
};
})(Backbone);