1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
8 |
|
9 | var _objectHash = require('object-hash');
|
10 |
|
11 | var _objectHash2 = _interopRequireDefault(_objectHash);
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
16 |
|
17 | var verbose = require('debug')('json-rules-engine-verbose');
|
18 |
|
19 | var Fact = function () {
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function Fact(id, valueOrMethod, options) {
|
29 | _classCallCheck(this, Fact);
|
30 |
|
31 | this.id = id;
|
32 | var defaultOptions = { cache: true };
|
33 | if (typeof options === 'undefined') {
|
34 | options = defaultOptions;
|
35 | }
|
36 | if (typeof valueOrMethod !== 'function') {
|
37 | this.value = valueOrMethod;
|
38 | this.type = this.constructor.CONSTANT;
|
39 | } else {
|
40 | this.calculationMethod = valueOrMethod;
|
41 | this.type = this.constructor.DYNAMIC;
|
42 | }
|
43 |
|
44 | if (!this.id) throw new Error('factId required');
|
45 | if (typeof this.value === 'undefined' && typeof this.calculationMethod === 'undefined') {
|
46 | throw new Error('facts must have a value or method');
|
47 | }
|
48 |
|
49 | this.priority = parseInt(options.priority || 1, 10);
|
50 | this.options = Object.assign({}, defaultOptions, options);
|
51 | this.cacheKeyMethod = this.defaultCacheKeys;
|
52 | return this;
|
53 | }
|
54 |
|
55 | _createClass(Fact, [{
|
56 | key: 'isConstant',
|
57 | value: function isConstant() {
|
58 | return this.type === this.constructor.CONSTANT;
|
59 | }
|
60 | }, {
|
61 | key: 'isDynamic',
|
62 | value: function isDynamic() {
|
63 | return this.type === this.constructor.DYNAMIC;
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | }, {
|
74 | key: 'calculate',
|
75 | value: function calculate(params, almanac) {
|
76 |
|
77 | if (this.hasOwnProperty('value')) {
|
78 | return this.value;
|
79 | }
|
80 | return this.calculationMethod(params, almanac);
|
81 | }
|
82 |
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | }, {
|
90 | key: 'defaultCacheKeys',
|
91 |
|
92 |
|
93 | |
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | value: function defaultCacheKeys(id, params) {
|
102 | return { params: params, id: id };
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | }, {
|
113 | key: 'getCacheKey',
|
114 | value: function getCacheKey(params) {
|
115 | if (this.options.cache === true) {
|
116 | var cacheProperties = this.cacheKeyMethod(this.id, params);
|
117 | var _hash = Fact.hashFromObject(cacheProperties);
|
118 | return _hash;
|
119 | }
|
120 | }
|
121 | }], [{
|
122 | key: 'hashFromObject',
|
123 | value: function hashFromObject(obj) {
|
124 | verbose('fact::hashFromObject generating cache key from:', obj);
|
125 | return (0, _objectHash2.default)(obj);
|
126 | }
|
127 | }]);
|
128 |
|
129 | return Fact;
|
130 | }();
|
131 |
|
132 | Fact.CONSTANT = 'CONSTANT';
|
133 | Fact.DYNAMIC = 'DYNAMIC';
|
134 |
|
135 | exports.default = Fact; |
\ | No newline at end of file |