Skip to content

Commit

Permalink
Merge pull request #115 from flashingpumpkin/master
Browse files Browse the repository at this point in the history
contextMatchesOptions + path array
  • Loading branch information
quirkey committed Jan 21, 2012
2 parents 4cbea2f + 45d2adf commit 0ee42ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/sammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@
log: function() {
Sammy.log.apply(Sammy, Array.prototype.concat.apply([this.element_selector],arguments));
},


// `route()` is the main method for defining routes within an application.
// For great detail on routes, check out:
// [http://sammyjs.org/docs/routes](http://sammyjs.org/docs/routes)
Expand Down Expand Up @@ -1097,6 +1097,15 @@
// // match all except a path
// app.contextMatchesOptions(context, {except: {path:'#/otherpath'}}); //=> true
// app.contextMatchesOptions(context, {except: {path:'#/mypath'}}); //=> false
// // match multiple paths
// app.contextMatchesOptions(context, {path: ['#/mypath', '#/otherpath']}); //=> true
// app.contextMatchesOptions(context, {path: ['#/otherpath', '#/thirdpath']}); //=> false
// // equivalent to
// app.contextMatchesOptions(context, {only: {path: ['#/mypath', '#/otherpath']}}); //=> true
// app.contextMatchesOptions(context, {only: {path: ['#/otherpath', '#/thirdpath']}}); //=> false
// // match all except multiple paths
// app.contextMatchesOptions(context, {except: {path: ['#/mypath', '#/otherpath']}}); //=> false
// app.contextMatchesOptions(context, {except: {path: ['#/otherpath', '#/thirdpath']}}); //=> true
//
contextMatchesOptions: function(context, match_options, positive) {
// empty options always match
Expand All @@ -1111,6 +1120,17 @@
if (typeof options === 'string' || _isFunction(options.test)) {
options = {path: options};
}
// Do we have to match against multiple paths?
if (_isArray(options.path)){
var results, numopt, opts;
results = [];
for (numopt in options.path){
opts = $.extend({}, options, {path: options.path[numopt]});
results.push(this.contextMatchesOptions(context, opts));
}
var matched = $.inArray(true, results) > -1 ? true : false;
return positive ? matched : !matched;
}
if (options.only) {
return this.contextMatchesOptions(context, options.only, true);
} else if (options.except) {
Expand Down Expand Up @@ -1539,7 +1559,7 @@
context.load(partials[name])
.then(function(template) {
this.partials[name] = template;
});
});
})(this, name);
}
}
Expand Down
24 changes: 24 additions & 0 deletions test/test_sammy_application.js
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,30 @@
ok(!this.app.contextMatchesOptions(this.route, {except: {verb: 'get'}}));
ok(this.app.contextMatchesOptions(this.route, {except: {verb: 'put'}}));
})
.should('match against path array', function(){
ok(!this.app.contextMatchesOptions(this.route, {path: ['#/', '#/foo']}));
ok(this.app.contextMatchesOptions(this.route, {path: ['#/', '#/boosh']}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/foo']}}));
ok(this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/boosh']}}));
ok(this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/foo']}}));
ok(!this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/boosh']}}));
})
.should('match against path array with verb', function(){
ok(this.app.contextMatchesOptions(this.route, {path: ['#/', '#/boosh'], verb: 'get'}));
ok(!this.app.contextMatchesOptions(this.route, {path: ['#/', '#/boosh'], verb: 'put'}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/boosh'], verb: 'put'}}));
ok(this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/boosh'], verb: 'get'}}));
ok(this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/boosh'], verb: 'put'}}));
ok(!this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/boosh'], verb: 'get'}}));
})
.should('match against path array with verb array', function(){
ok(this.app.contextMatchesOptions(this.route, {path: ['#/', '#/boosh'], verb: ['get', 'put']}));
ok(!this.app.contextMatchesOptions(this.route, {path: ['#/', '#/boosh'], verb: ['put', 'post']}));
ok(!this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/boosh'], verb: ['put', 'post']}}));
ok(this.app.contextMatchesOptions(this.route, {only: {path: ['#/', '#/boosh'], verb: ['put', 'get']}}));
ok(this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/boosh'], verb: ['put', 'post']}}));
ok(!this.app.contextMatchesOptions(this.route, {except: {path: ['#/', '#/boosh'], verb: ['put', 'get']}}));
})
.should('match against just path', function() {
ok(this.app.contextMatchesOptions(this.route, '#/boosh'), 'should match exact string path');
ok(!this.app.contextMatchesOptions(this.route, '#/boo'), 'should not match partial string path');
Expand Down

0 comments on commit 0ee42ac

Please sign in to comment.