Revision 79108046ad0c63ddd49a72f7e7fb76d1a4b3f6dd authored by Valeri Karpov on 03 December 2015, 21:14:22 UTC, committed by Valeri Karpov on 03 December 2015, 21:14:22 UTC
1 parent c7e5fc0
virtualtype.js
/**
* VirtualType constructor
*
* This is what mongoose uses to define virtual attributes via `Schema.prototype.virtual`.
*
* ####Example:
*
* var fullname = schema.virtual('fullname');
* fullname instanceof mongoose.VirtualType // true
*
* @parma {Object} options
* @api public
*/
function VirtualType(options, name) {
this.path = name;
this.getters = [];
this.setters = [];
this.options = options || {};
}
/**
* Defines a getter.
*
* ####Example:
*
* var virtual = schema.virtual('fullname');
* virtual.get(function () {
* return this.name.first + ' ' + this.name.last;
* });
*
* @param {Function} fn
* @return {VirtualType} this
* @api public
*/
VirtualType.prototype.get = function(fn) {
this.getters.push(fn);
return this;
};
/**
* Defines a setter.
*
* ####Example:
*
* var virtual = schema.virtual('fullname');
* virtual.set(function (v) {
* var parts = v.split(' ');
* this.name.first = parts[0];
* this.name.last = parts[1];
* });
*
* @param {Function} fn
* @return {VirtualType} this
* @api public
*/
VirtualType.prototype.set = function(fn) {
this.setters.push(fn);
return this;
};
/**
* Applies getters to `value` using optional `scope`.
*
* @param {Object} value
* @param {Object} scope
* @return {any} the value after applying all getters
* @api public
*/
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, this);
}
return v;
};
/**
* Applies setters to `value` using optional `scope`.
*
* @param {Object} value
* @param {Object} scope
* @return {any} the value after applying all setters
* @api public
*/
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, this);
}
return v;
};
/*!
* exports
*/
module.exports = VirtualType;
Computing file changes ...