From dabfbd151c92a6a42448b7ae6ce3ad614226664c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 21 Jan 2014 16:34:25 +0100 Subject: [PATCH] Add ctor.http to SharedClass The SharedClass has the `http` property always set now. If the ctor does not provide `http.path`, a default value of `'/' + name` is used. This way we can decouple the REST (HTTP) routing from the model name and make the REST path accessible from any external code. The second part of the commit modifies ext/swagger to use this new property. --- ext/swagger.js | 3 ++- lib/shared-class.js | 4 +++- test/shared-class.test.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/shared-class.test.js diff --git a/ext/swagger.js b/ext/swagger.js index 1f28cead..1586c715 100644 --- a/ext/swagger.js +++ b/ext/swagger.js @@ -36,7 +36,7 @@ function Swagger(remotes, options, models) { classes.forEach(function (item) { resourceDoc.apis.push({ - path: '/' + name + '/' + item.name, + path: '/' + name + item.http.path, description: item.ctor.sharedCtor && item.ctor.sharedCtor.description }); @@ -50,6 +50,7 @@ function Swagger(remotes, options, models) { helper.method(api, { path: item.name, + http: { path: item.http.path }, returns: { type: 'object', root: true } }); function api(callback) { diff --git a/lib/shared-class.js b/lib/shared-class.js index 515a6b7c..ad20154e 100644 --- a/lib/shared-class.js +++ b/lib/shared-class.js @@ -32,6 +32,8 @@ function SharedClass(name, ctor) { assert(ctor.sharedCtor, 'must define a sharedCtor'); this.sharedCtor = new SharedMethod(ctor.sharedCtor, 'sharedCtor'); } + + this.http = util._extend({ path: '/' + this.name }, ctor.http); assert(this.name, 'must include a remoteNamespace when creating a SharedClass'); } @@ -77,4 +79,4 @@ function eachRemoteFunctionInObject(obj, f) { f(fn, key); } } -} \ No newline at end of file +} diff --git a/test/shared-class.test.js b/test/shared-class.test.js new file mode 100644 index 00000000..d44d32d0 --- /dev/null +++ b/test/shared-class.test.js @@ -0,0 +1,22 @@ +var extend = require('util')._extend; +var expect = require('chai').expect; +var SharedClass = require('../lib/shared-class'); +var factory = require('./helpers/shared-objects-factory.js'); + +describe('SharedClass', function() { + var SomeClass; + beforeEach(function() { SomeClass = factory.createSharedClass(); }); + + describe('constructor', function() { + it('fills http.path from ctor.http', function() { + SomeClass.http = { path: '/foo' }; + var sc = new SharedClass('some', SomeClass); + expect(sc.http.path).to.equal('/foo'); + }); + + it('fills http.path using the name', function() { + var sc = new SharedClass('some', SomeClass); + expect(sc.http.path).to.equal('/some'); + }); + }); +});