Permalink
Browse files

added; virtual {g,s}etter introspection

they now have access to their virtual type (same as path {g,s}etters

closes #1070
  • Loading branch information...
1 parent 696ee51 commit f5e40852892b3de37009cb052e2c24025e9d8265 @aheckmann aheckmann committed Aug 22, 2012
Showing with 20 additions and 6 deletions.
  1. +1 −1 lib/schema.js
  2. +5 −4 lib/virtualtype.js
  3. +14 −1 test/schema.test.js
View
@@ -702,7 +702,7 @@ Schema.prototype.virtual = function (name, options) {
var parts = name.split('.');
return virtuals[name] = parts.reduce(function (mem, part, i) {
mem[part] || (mem[part] = (i === parts.length-1)
- ? new VirtualType(options)
+ ? new VirtualType(options, name)
: {});
return mem[part];
}, this.tree);
View
@@ -13,7 +13,8 @@
* @api public
*/
-function VirtualType (options) {
+function VirtualType (options, name) {
+ this.path = name;
this.getters = [];
this.setters = [];
this.options = options || {};
@@ -72,8 +73,8 @@ VirtualType.prototype.set = function (fn) {
VirtualType.prototype.applyGetters = function (value, scope) {
var v = value;
- for (var l = this.getters.length - 1; l >= 0; l--){
- v = this.getters[l].call(scope, v);
+ for (var l = this.getters.length - 1; l >= 0; l--) {
+ v = this.getters[l].call(scope, v, this);
}
return v;
};
@@ -90,7 +91,7 @@ VirtualType.prototype.applyGetters = function (value, scope) {
VirtualType.prototype.applySetters = function (value, scope) {
var v = value;
for (var l = this.setters.length - 1; l >= 0; l--) {
- v = this.setters[l].call(scope, v);
+ v = this.setters[l].call(scope, v, this);
}
return v;
};
View
@@ -1121,14 +1121,27 @@ describe('schema', function(){
});
});
+ describe('getter', function(){
+ it('scope', function(){
+ var Tobi = new Schema;
+
+ Tobi.virtual('name').get(function (v, self) {
+ assert.equal('b', this.a);
+ assert.equal('name', self.path);
+ return v.toLowerCase();
+ })
+
+ assert.equal('yep', Tobi.virtualpath('name').applyGetters('YEP', { a: 'b' }));
+ })
+ })
describe('setter', function(){
it('scope', function(){
var Tobi = new Schema;
Tobi.virtual('name').set(function (v, self) {
assert.equal('b', this.a);
- //assert.equal('name', self.path);
+ assert.equal('name', self.path);
return v.toLowerCase();
})

0 comments on commit f5e4085

Please sign in to comment.