Refactor types out

This commit is contained in:
Raymond Feng 2013-07-25 07:48:31 -07:00
parent 8c1a102179
commit 630aad2071
2 changed files with 66 additions and 32 deletions

View File

@ -7,7 +7,6 @@ var ModelBaseClass = require('./model.js');
var List = require('./list.js'); var List = require('./list.js');
var EventEmitter = require('events').EventEmitter; var EventEmitter = require('events').EventEmitter;
var util = require('util'); var util = require('util');
var GeoPoint = require('./geo').GeoPoint;
/** /**
* Export public API * Export public API
@ -21,36 +20,6 @@ exports.Schema = exports.ModelBuilder = ModelBuilder;
*/ */
var slice = Array.prototype.slice; var slice = Array.prototype.slice;
/**
* Schema types
*/
ModelBuilder.Text = function Text() {}; // Text type
ModelBuilder.JSON = function JSON() {}; // JSON Object
ModelBuilder.Any = function Any() {}; // Any Type
ModelBuilder.schemaTypes = {};
ModelBuilder.registerType = function (type, names) {
names = names || [];
names = names.concat([type.name]);
for (var n = 0; n < names.length; n++) {
this.schemaTypes[names[n].toLowerCase()] = type;
}
};
ModelBuilder.registerType(ModelBuilder.Text);
ModelBuilder.registerType(ModelBuilder.JSON);
ModelBuilder.registerType(ModelBuilder.Any);
ModelBuilder.registerType(String);
ModelBuilder.registerType(Number);
ModelBuilder.registerType(Boolean);
ModelBuilder.registerType(Date);
ModelBuilder.registerType(Buffer, ['Binary']);
ModelBuilder.registerType(Array);
ModelBuilder.registerType(GeoPoint);
ModelBuilder.registerType(Object);
/** /**
* ModelBuilder - Data Model Definition * ModelBuilder - Data Model Definition
*/ */
@ -62,6 +31,8 @@ function ModelBuilder() {
util.inherits(ModelBuilder, EventEmitter); util.inherits(ModelBuilder, EventEmitter);
// Set up types
require('./types')(ModelBuilder);
/** /**
* Define class * Define class
@ -221,7 +192,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
if(!DataType) { if(!DataType) {
throw new Error('Invalid type for property ' + attr); throw new Error('Invalid type for property ' + attr);
} }
if (DataType instanceof Array) { if (Array.isArray(DataType)) {
DataType = List; DataType = List;
} else if (DataType.name === 'Date') { } else if (DataType.name === 'Date') {
var OrigDate = Date; var OrigDate = Date;
@ -261,6 +232,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
if(DataType === List) { if(DataType === List) {
this.__data[attr] = DataType(value, properties[attr].type, this.__data); this.__data[attr] = DataType(value, properties[attr].type, this.__data);
} else { } else {
// Assume the type constructor handles Constructor() call
// If not, we should call new DataType(value).valueOf();
this.__data[attr] = DataType(value); this.__data[attr] = DataType(value);
} }
} }

61
lib/types.js Normal file
View File

@ -0,0 +1,61 @@
module.exports = function (Types) {
var List = require('./list.js');
var GeoPoint = require('./geo').GeoPoint;
/**
* Schema types
*/
Types.Text = function Text(value) {
if (!(this instanceof Text)) {
return new Text(value);
}
this.value = value;
}; // Text type
Types.Text.prototype.toObject = Types.Text.prototype.toJSON = function () {
return this.value;
};
Types.JSON = function JSON() {
if (!(this instanceof JSON)) {
return new JSON(value);
}
this.value = value;
}; // JSON Object
Types.JSON.prototype.toObject = Types.JSON.prototype.toJSON = function () {
return this.value;
};
Types.Any = function Any() {
if (!(this instanceof Any)) {
return new Any(value);
}
this.value = value;
}; // Any Type
Types.Any.prototype.toObject = Types.Any.prototype.toJSON = function () {
return this.value;
};
Types.schemaTypes = {};
Types.registerType = function (type, names) {
names = names || [];
names = names.concat([type.name]);
for (var n = 0; n < names.length; n++) {
this.schemaTypes[names[n].toLowerCase()] = type;
}
};
Types.registerType(Types.Text);
Types.registerType(Types.JSON);
Types.registerType(Types.Any);
Types.registerType(String);
Types.registerType(Number);
Types.registerType(Boolean);
Types.registerType(Date);
Types.registerType(Buffer, ['Binary']);
Types.registerType(Array);
Types.registerType(GeoPoint);
Types.registerType(Object);
}