Skip to content

Commit

Permalink
introduce template method for closure return value handling. Now obje…
Browse files Browse the repository at this point in the history
…ct closures must implement the method do instead of process
  • Loading branch information
aitrusgit committed Oct 2, 2017
1 parent ba212a8 commit 4931721
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ or

```javascript
class CalculateTotalPrize extends Closure {
process(fact, context) {
do(fact, context) {
fact.totalPrice = fact.books.reduce((total, book) => total + book.price, 0);
return fact;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ function (fact, context) {

//the same thing implemented through a class
class TaxCalculator extends Closure {
process(fact, context) {
do(fact, context) {
return fact.totalPrice * context.parameters.salesTax;
}
}
Expand Down
15 changes: 15 additions & 0 deletions lib/closure/Closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ class Closure {
* @return {Promise} a promise that will be resolved to some result
*/
process(fact, context) {
const result = this.do(fact, context);
return Promise.resolve(result);
}

/**
* Evaluates the closure against a certain fact
*
* @param {Object} fact a fact
* @param {Context} context an execution context.
* @param {Object} context.parameters the execution parameters, if any
* @param {Engine} context.engine the rules engine
*
* @return {Object} the result of the evaluation
*/
do(fact, context) {
throw new Error("This is an abstract closure, how did you get to instantiate this?");
}

Expand Down
2 changes: 1 addition & 1 deletion lib/closure/ClosureReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ClosureReducer extends Closure {
this.closures = closures || util.raise(`Cannot build closure reducer [${name}] without closure chain`);
}

* process(fact, context) {
* do(fact, context) {
for (let closure of this.closures) {
fact = yield closure.process(fact, context);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/closure/FunctionalClosure.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ class FunctionalClosure extends Closure {
*
* @return {Promise} a promise that will be resolved to some result
*/
process(fact, context) {
const result = this.fn.call(this, fact, context);
return Promise.resolve(result);
do(fact, context) {
return this.fn.call(this, fact, context);
}

}
Expand Down
2 changes: 1 addition & 1 deletion lib/closure/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Rule extends Closure{
* @return {Promise} a promise that will be resolved to some result (typically
* such result will be used as next's rule fact)
*/
* process(fact, context) {
* do(fact, context) {
const matches = yield this.evaluateCondition(fact, context);
if (matches) {
context.ruleFired(this);
Expand Down
2 changes: 1 addition & 1 deletion lib/closure/RuleFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const wrap = require("async-class-co").wrap,

class RuleFlow extends ClosureReducer {

* process(fact, context) {
* do(fact, context) {
context.initiateFlow();
fact = yield super.process(fact, context);
context.endFlow();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rules-js",
"version": "0.0.3",
"version": "0.0.4",
"description": "A simple rule-engine for javascript",
"main": "index.js",
"author": "Claudio Fernandez <[email protected]>",
Expand Down

0 comments on commit 4931721

Please sign in to comment.