Refactor types out
This commit is contained in:
parent
8c1a102179
commit
630aad2071
|
@ -7,7 +7,6 @@ var ModelBaseClass = require('./model.js');
|
|||
var List = require('./list.js');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var util = require('util');
|
||||
var GeoPoint = require('./geo').GeoPoint;
|
||||
|
||||
/**
|
||||
* Export public API
|
||||
|
@ -21,36 +20,6 @@ exports.Schema = exports.ModelBuilder = ModelBuilder;
|
|||
*/
|
||||
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
|
||||
*/
|
||||
|
@ -62,6 +31,8 @@ function ModelBuilder() {
|
|||
|
||||
util.inherits(ModelBuilder, EventEmitter);
|
||||
|
||||
// Set up types
|
||||
require('./types')(ModelBuilder);
|
||||
|
||||
/**
|
||||
* Define class
|
||||
|
@ -221,7 +192,7 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
|||
if(!DataType) {
|
||||
throw new Error('Invalid type for property ' + attr);
|
||||
}
|
||||
if (DataType instanceof Array) {
|
||||
if (Array.isArray(DataType)) {
|
||||
DataType = List;
|
||||
} else if (DataType.name === 'Date') {
|
||||
var OrigDate = Date;
|
||||
|
@ -261,6 +232,8 @@ ModelBuilder.prototype.define = function defineClass(className, properties, sett
|
|||
if(DataType === List) {
|
||||
this.__data[attr] = DataType(value, properties[attr].type, this.__data);
|
||||
} else {
|
||||
// Assume the type constructor handles Constructor() call
|
||||
// If not, we should call new DataType(value).valueOf();
|
||||
this.__data[attr] = DataType(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue