Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from intabulas/0_7_0-StrongParams
Add StrongParam
- Loading branch information
Showing
5 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
|
||
}); | ||
|
||
|
||
|
||
}); | ||
|
||
}); |