Skip to content

Commit

Permalink
Merge pull request #155 from intabulas/0_7_0-StrongParams
Browse files Browse the repository at this point in the history
Add StrongParam
  • Loading branch information
keithwhor committed Jan 31, 2016
2 parents bf34bf0 + 536ca14 commit 0a1f8da
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 3 deletions.
7 changes: 7 additions & 0 deletions core/module.js
Expand Up @@ -32,6 +32,7 @@ module.exports = (function() {
Scheduler: null,
SchedulerTask: null,
SchemaGenerator: null,
StrongParam: null,
Task: null,
Template: null,
my: {
Expand Down Expand Up @@ -187,6 +188,12 @@ module.exports = (function() {
},
enumerable: true
},
StrongParam: {
get: function() {
return Nodal.StrongParam || (Nodal.StrongParam = require('./required/strong_param.js'));
},
enumerable: true
},
Task: {
get: function() {
return Nodal.Task || (Nodal.Task = require('./required/task.js'));
Expand Down
6 changes: 3 additions & 3 deletions core/required/router.js
Expand Up @@ -7,7 +7,7 @@ module.exports = (() => {
const domain = require('domain'); // TODO: Will be deprecated

const utilities = require('./utilities.js');

const StrongParam = require('./strong_param.js');
const ExecutionQueue = require('./execution_queue.js');

class Route {
Expand Down Expand Up @@ -151,8 +151,8 @@ module.exports = (() => {

let params = {
buffer: new Buffer(routeData.body, 'binary'),
query: this.parseQueryParameters(url.parse(routeData.url, true).query),
body: this.parseBody(routeData.body, routeData.headers),
query: new StrongParam(this.parseQueryParameters(url.parse(routeData.url, true).query)),
body: new StrongParam(this.parseBody(routeData.body, routeData.headers)),
path: routeData.path,
matches: routeData.matches,
route: routeData.route,
Expand Down
89 changes: 89 additions & 0 deletions core/required/strong_param.js
@@ -0,0 +1,89 @@
module.exports = (function() {

'use strict';

const utilities = require('./utilities.js');

/**
* StrongParam are what the router uses to actually instantiate Controllers
* @class
*/
class StrongParam {

/**
* @param {props} object Properties
*/
constructor(props) {

if (props instanceof StrongParam) {
return props;
}

Object.keys(props).forEach((key) => {
Object.defineProperty(this, key, {
enumerable: true,
value: utilities.isObject(props[key]) ? new StrongParam(props[key]) : props[key],
writable: true
});
});

}

except() {
let list = Array.prototype.slice.call(arguments);

let filteredObject = {};
Object.keys(this).forEach( key => {
if ( list.indexOf(key) === -1 ) {
if (utilities.isObject(this[key])) {
filteredObject[key] = this[key].except.apply(this[key], arguments)
} else {
filteredObject[key] = this[key];
}
}

});

return new StrongParam(filteredObject);

}

permit() {
let list = Array.prototype.slice.call(arguments);

let filteredObject = {};
Object.keys(this).forEach( key => {
if ( list.indexOf(key) !== -1 ) {
if (utilities.isObject(this[key])) {
filteredObject[key] = this[key].permit.apply(this[key], arguments)
} else {
filteredObject[key] = this[key];
}
}

});

return new StrongParam(filteredObject);

}

toObject() {
let flattenedObject = {};
Object.keys(this).forEach( key => {
if (this[key] instanceof StrongParam) {
flattenedObject[key] = this[key].toObject()
} else {
flattenedObject[key] = this[key];
}
});
return flattenedObject;
}



};


return StrongParam;

})();
2 changes: 2 additions & 0 deletions test/runner.js
Expand Up @@ -70,6 +70,8 @@ describe('Test Suite', function() {

require('./tests/relationship_graph.js')(Nodal);

require('./tests/strong_param.js')(Nodal);

require('./tests/utilities.js')(Nodal);

});
100 changes: 100 additions & 0 deletions test/tests/strong_param.js
@@ -0,0 +1,100 @@
module.exports = (function(Nodal) {

'use strict';

const async = require('async');

let expect = require('chai').expect;

describe('Strong Parameters', function() {

it('should act like plain object', () => {

let param = new Nodal.StrongParam({
title: 'Some Title',
name: 'Some Name'
});

expect(param).to.have.ownProperty('title');
expect(param).to.have.ownProperty('name');
expect(param.name).to.equal('Some Name');
expect(param.title).to.equal('Some Title');

});

it('should filter query keys with except()', () => {

let param = new Nodal.StrongParam({
title: 'Some Title',
name: 'Some Name',
query: new Nodal.StrongParam({
name: 'Tom Saywer',
task: 'Paint Fence'
})
}).except('task')

expect(param.query).to.have.ownProperty('name');
expect(param.query).to.not.have.ownProperty('task');

});

it('should filter body keys with except()', () => {

let param = new Nodal.StrongParam({
title: 'Some Title',
name: 'Some Name',
task: 'Some Task',
body: new Nodal.StrongParam({
name: 'Tom Saywer',
task: 'Paint Fence'
})
}).except('task')

expect(param).to.not.have.ownProperty('task');
expect(param.body).to.have.ownProperty('name');
expect(param.body).to.not.have.ownProperty('task');

});

it('should allow body keys with permit()', () => {

let param = new Nodal.StrongParam({
title: 'Some Title',
name: 'Some Name',
task: 'Some Task',
body: new Nodal.StrongParam({
name: 'Tom Saywer',
task: 'Paint Fence'
})
}).permit('name')

console.log(param)

expect(param).to.have.ownProperty('name');

});

it('should filter body data multiple keys with permit()', () => {

let param = new Nodal.StrongParam({
title: 'Some Title',
name: 'Some Name',
task: 'Some Task',
body: new Nodal.StrongParam({
name: 'Tom Saywer',
task: 'Paint Fence'
})
}).except('name', 'task')


expect(param).to.not.have.ownProperty('task');
expect(param.body).to.not.have.ownProperty('name');
expect(param.body).to.not.have.ownProperty('task');

});



});

});

0 comments on commit 0a1f8da

Please sign in to comment.