Skip to content

Commit

Permalink
Merge pull request #10 from d00rman/rules/match_not
Browse files Browse the repository at this point in the history
Rules: Add the `match_not` predicate (release v0.2.0)
  • Loading branch information
Pchelolo committed Apr 27, 2016
2 parents a8486a9 + 0d83465 commit fcbcc4f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/rule.js
Expand Up @@ -106,6 +106,7 @@ class Rule {
this.shouldRetry = _compileRetryCondition(this.spec.retry_on);
this.exec = this._processExec(this.spec.exec);
this._match = this._processMatch(this.spec.match);
this._match_not = (this._processMatch(this.spec.match_not) || {}).test;
}

/**
Expand All @@ -115,7 +116,14 @@ class Rule {
* @return true if no match is set for this rule or if the message matches
*/
test(message) {
return !this._match || !this._match.test || this._match.test(message);
var match = true;
if (this._match && this._match.test) {
match = this._match.test(message);
}
if (this._match_not) {
match = match && !this._match_not(message);
}
return match;
}

/**
Expand Down Expand Up @@ -175,4 +183,4 @@ class Rule {
}
}

module.exports = Rule;
module.exports = Rule;
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "change-propagation",
"version": "0.1.3",
"version": "0.2.0",
"description": "Listens to events from Kafka and delivers them",
"main": "server.js",
"repository": {
Expand Down
39 changes: 39 additions & 0 deletions test/feature/rule.js
Expand Up @@ -113,6 +113,45 @@ describe('Rule', function() {
}, Error);
});

it('match_not', function() {
var r = new Rule('rule', {
topic: 'nono',
exec: {uri: 'a/b/c'},
match_not: {meta: {uri: '/my-url/'}}
});
assert.ok(r.test(msg), 'Expected the rule to match the given message!');
});

it('matches match and match_not', function() {
var r = new Rule('rule', {
topic: 'nono',
exec: {uri: 'a/b/c'},
match: {number: 1},
match_not: {meta: {uri: '/my-url/'}}
});
assert.ok(r.test(msg), 'Expected the rule to match the given message!');
});

it('matches match but not match_not', function() {
var r = new Rule('rule', {
topic: 'nono',
exec: {uri: 'a/b/c'},
match: {number: 1},
match_not: {meta: {uri: '/fake/'}}
});
assert.ok(!r.test(msg), 'Expected the rule not to match the given message!');
});

it('matches match_not but not match', function() {
var r = new Rule('rule', {
topic: 'nono',
exec: {uri: 'a/b/c'},
match: {number: 10},
match_not: {meta: {uri: '/my-url/'}}
});
assert.ok(!r.test(msg), 'Expected the rule not to match the given message!');
});

it('expansion', function() {
var r = new Rule('rule', {
topic: 'nono',
Expand Down

0 comments on commit fcbcc4f

Please sign in to comment.