Skip to content

Commit

Permalink
Merge branch 'feature/fluent-interface' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Walter Barbagallo committed Apr 8, 2016
2 parents 148a471 + 01f56a2 commit ca21351
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,23 @@ router.scope('admin', function() {
The difference with the namespace is that the routes paths don't have a prefix, but the controller lives inside a directory with the same name of the scope.


#### Fluent interface

Guidance router have a fluent interface, so it can be used in this way:

```javascript
router
.get('/geocoder', { to: 'geocoder#show' })
.namespace('admin', function() {})
.resources('books')
.resource('geocoder')
.scope('admin', function() {})
.root('welcome#index')
.post('/photos', { to: 'photos#create' })
;
```


## Notes

A modern version of node is required (due to harmony syntax).
Expand Down
10 changes: 10 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ Router.prototype.resources = function(resource, fn) {
];

routes.forEach(route => this.layers.push(this._parseRoute(route)));

return this;
};


Expand Down Expand Up @@ -249,20 +251,26 @@ Router.prototype.resource = function(resource) {
];

routes.forEach(route => this.layers.push(this._parseRoute(route)));

return this;
};


Router.prototype.namespace = function (namespace, fn) {
this.namespaceStack.push(namespace);
fn.call(this);
this.namespaceStack.pop();

return this;
};


Router.prototype.scope = function (scope, fn) {
this.scopeStack.push(scope);
fn.call(this);
this.scopeStack.pop();

return this;
};


Expand All @@ -279,5 +287,7 @@ methods.forEach((method) => {
let layer = this._parseRoute({ method, path, opts });

this.layers.push(layer);

return this;
};
});
18 changes: 18 additions & 0 deletions test/guidanceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ describe('guidance', function() {
.end(done)
;
});


it('use a fluent interface', function() {
let routes = function(router) {
router
.get('/geocoder', { to: 'geocoder#show' })
.namespace('admin', function() {})
.resources('books')
.resource('geocoder')
.scope('admin', function() {})
.root('welcome#index')
.post('/photos', { to: 'photos#create' })
;
};

app.use(guidance.initialize(routes, { controllersDir }));

});
});

context('resource', function() {
Expand Down

0 comments on commit ca21351

Please sign in to comment.