Skip to content

Commit

Permalink
add support to register classes
Browse files Browse the repository at this point in the history
It also disables the binding parameters validation since it
was broken
  • Loading branch information
aitrusgit committed Oct 2, 2017
1 parent 4931721 commit 03353a9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/closure/Closure.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class Closure {
* @param {Engine} engine - the rules engine instance
*/
bind(name, parameters, engine) {
const missing = (this.options.required || []).find(required => parameters[required] === undefined)
if (missing) {
throw new Error(`Cannot instantiate provided closure '${this.name}'. Parameter ${missing} is unbounded`);
}
// const missing = (this.options.required || []).find(required => parameters[required] === undefined)
// if (missing) {
// throw new Error(`Cannot instantiate provided closure '${this.name}'. Parameter ${missing} is unbounded`);
// }

// No need to perform any binding, there is nothing to bind
if (! Object.keys(parameters).length) {
Expand All @@ -81,6 +81,7 @@ class Closure {

}

Closure.closureType = true;

/**
* A closure bound to a certain set of parameters
Expand Down
6 changes: 6 additions & 0 deletions lib/closure/ClosureRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ module.exports = class ClosureRegistry {
add(name, closure, options = {}) {
name || util.raise("Cannot add anonymous closure");

if (closure.closureType) {
options.required = options.required || closure.required;
options.closureParameters = options.closureParameters || closure.closureParameters;
closure = new closure(name, options);
}
if (typeof(closure) === "function") {
closure = new FunctionalClosure(name, closure, options);
}

if (this.namedClosures[name] && !options.override) {
throw new Error(`Already defined a closure with name '${name}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/closure/RuleFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RuleFlow extends ClosureReducer {

* do(fact, context) {
context.initiateFlow();
fact = yield super.process(fact, context);
fact = yield super.do(fact, context);
context.endFlow();

return fact
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.4",
"version": "0.0.5",
"description": "A simple rule-engine for javascript",
"main": "index.js",
"author": "Claudio Fernandez <[email protected]>",
Expand Down
26 changes: 26 additions & 0 deletions test/closure/ClosureRegistry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,32 @@ describe("ClosureRegistry", () => {
result.fact.should.be.true;
});

});

describe("add (with Closure class)", () => {
const TrueClosure = class {
do(fact, context) {
return true;
}
}
const FalseClosure = class {
do(fact, context) {
return false;
}
}

it("should fail if an closure with the given name is already defined", () => {
registry.add("true", FalseClosure);
(() => registry.add("true", TrueClosure)).should.throw();
});

it("should replace a closure if override option is provided", function* () {
registry.add("true", FalseClosure);
registry.add("true", TrueClosure, { override: true });

const result = yield registry.get("true").process("foo", context());
result.fact.should.be.true;
});

});

Expand Down
2 changes: 1 addition & 1 deletion test/closure/FunctionalClosure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe("FunctionalClosure", () => {
(() => closure.process("foo", context())).should.eventually.throw();
});

it("should fail when binding but parameter not provided", () => {
it.skip("should fail when binding but parameter not provided", () => {
(() => closure.bind(null, {})).should.throw();
});

Expand Down

0 comments on commit 03353a9

Please sign in to comment.