Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add jQuery style end() to stop the chain

  • Loading branch information...
commit c4b588e1fc4489b945ffe9a2949fa2ebce81ae0c 1 parent 1b280b2
Garrett Johnson authored
Showing with 35 additions and 26 deletions.
  1. +16 −18 README.md
  2. +6 −7 examples/and.js
  3. +2 −1  examples/api.js
  4. +11 −0 lib/rule.js
View
34 README.md
@@ -22,11 +22,7 @@ var ruler = require('ruler')
// initialize
-var engine = ruler();
-
-// describe rules
-
-engine
+var engine = ruler()
.rule('name.first')
.eq('john')
.rule('name.last')
@@ -35,7 +31,8 @@ engine
.contains('red')
.rule('number')
.gte(0)
- .lte(100);
+ .lte(100)
+ .end();
// rests an object against the "rule set"
@@ -62,7 +59,7 @@ var obj = {
first: 'john',
last: 'doe'
},
- age: 21,
+ number: 100,
company: 'redventures'
};
@@ -70,7 +67,7 @@ var filters = [
{ cmp: 'is', path: 'name.first', value: 'john' },
{ cmp: 'not', path: 'name.last', value: 'buzz' },
{ cmp: 'contains', path: 'company', value: 'red' },
- { cmp: 'gte', path: 'age', value: 21 }
+ { cmp: 'gte', path: 'number', value: 75 }
];
var engine = ruler(filters);
@@ -89,18 +86,19 @@ var ruler = require('ruler')
// name rules
var names = ruler();
-
-names
- .is('name.first', 'john')
- .not('name.last', 'buzz')
+ .rule('name.first')
+ .eq('john')
+ .rule('name.last')
+ .neq'buzz');
+ .end();
// info rules
-var info = ruler();
-
-info
- .contains('company', 'red')
- .gte('age', 21);
+var info = ruler()
+ .rule('company')
+ .contains(red')
+ .rule('number')
+ .gte(50);
// compose "name" and "info" together
@@ -113,7 +111,7 @@ var result = all.test({
first: 'john',
last: 'doe'
},
- age: 21,
+ number: 75,
company: 'redventures'
});
View
13 examples/and.js
@@ -2,24 +2,23 @@
var ruler = require('..')
, assert = require('assert');
-var names = ruler();
-var info = ruler();
-
// names
-names
+var names = ruler()
.rule('name.first')
.eq('john')
.rule('name.last')
- .eq('buzz');
+ .neq('buzz')
+ .end();
// info
-info
+var info = ruler()
.rule('company')
.contains('red')
.rule('age')
- .eq(21);
+ .eq(21)
+ .end();
// compose an "all" ruler
View
3  examples/api.js
@@ -13,7 +13,8 @@ var engine = ruler()
.contains('red')
.rule('age')
.gte(21)
- .lte(31);
+ .lte(31)
+ .end();
// test it
View
11 lib/rule.js
@@ -55,6 +55,17 @@ Rule.prototype.rule = function(){
};
/**
+ * Breaks the chain and returns the origin `ruler`.
+ *
+ * @api public
+ * @return {Ruler}
+ */
+
+Rule.prototype.end = function(){
+ return this.ruler;
+};
+
+/**
* Mixin our pre-baked comparators.
*/
Please sign in to comment.
Something went wrong with that request. Please try again.