List API improvements

This commit is contained in:
Anatoliy Chakkaev 2012-09-11 23:22:55 +04:00
parent e0f5f45b05
commit c8b8012e28
4 changed files with 88 additions and 10 deletions

View File

@ -66,7 +66,7 @@ AbstractClass.prototype._initProperties = function (data, applySetters) {
var type = properties[attr].type;
if (BASE_TYPES.indexOf(type.name) === -1) {
if (typeof this[_attr] !== 'object') {
if (typeof this[_attr] !== 'object' && this[_attr]) {
try {
this[_attr] = JSON.parse(this[_attr] + '');
} catch (e) {
@ -149,7 +149,7 @@ AbstractClass.whatTypeName = function (propName) {
AbstractClass._forDB = function (data) {
var res = {};
Object.keys(data).forEach(function (propName) {
if (this.whatTypeName(propName) === 'JSON') {
if (this.whatTypeName(propName) === 'JSON' || data[propName] instanceof Array) {
res[propName] = JSON.stringify(data[propName]);
} else {
res[propName] = data[propName];

View File

@ -25,8 +25,12 @@ function List(data, type, parent) {
}
data.forEach(function (item, i) {
data[i] = new Item(item, parent);
list[data[i].id] = data[i];
data[i] = new Item(item, list);
Object.defineProperty(list, data[i].id, {
writable: true,
enumerable: false,
value: data[i]
});
if (list.nextid <= data[i].id) {
list.nextid = data[i].id + 1;
}
@ -44,20 +48,81 @@ function List(data, type, parent) {
}
var _;
try {
_ = require('underscore');
} catch (e) {
_ = false;
}
if (_) {
var _import = [
// collection methods
'each',
'map',
'reduce',
'reduceRight',
'find',
'filter',
'reject',
'all',
'any',
'include',
'invoke',
'pluck',
'max',
'min',
'sortBy',
'groupBy',
'sortedIndex',
'shuffle',
'toArray',
'size',
// array methods
'first',
'initial',
'last',
'rest',
'compact',
'flatten',
'without',
'union',
'intersection',
'difference',
'uniq',
'zip',
'indexOf',
'lastIndexOf',
'range'
];
_import.forEach(function (name) {
List.prototype[name] = function () {
var args = [].slice.call(arguments);
args.unshift(this.items);
return _[name].apply(_, args);
};
});
}
List.prototype.toObject = function () {
return this.items;
};
List.prototype.toJSON = function () {
return this.items;
};
List.prototype.toString = function () {
return JSON.stringify(this.items);
};
List.prototype.autoincrement = function () {
return this.nextid++;
};
List.prototype.push = function (obj) {
var item = new ListItem(obj, this);
if (this.ItemType) {
item.__proto__ = this.ItemType.prototype;
}
item.id = this.autoincrement();
this.items.push(item);
return item;
};
@ -99,5 +164,18 @@ function ListItem(data, parent) {
configurable: true,
value: parent
});
if (!this.id) {
this.id = parent.autoincrement();
}
if (parent.ItemType) {
this.__proto__ = parent.ItemType.prototype;
if (parent.ItemType !== ListItem) {
parent.ItemType.apply(this);
}
}
this.save = function (c) {
parent.parent.save(c);
};
}

View File

@ -1,7 +1,7 @@
{
"name": "jugglingdb",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.1.17",
"version": "0.1.18",
"author": "Anatoliy Chakkaev <rpm1602@gmail.com>",
"contributors": [
{ "name": "Anatoliy Chakkaev", "email": "rpm1602@gmail.com" },

View File

@ -17,7 +17,7 @@ var schemas = {
database: ':memory:'
},
neo4j: { url: 'http://localhost:7474/' },
mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
// mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
mongodb: { url: 'mongodb://travis:test@localhost:27017/myapp' },
redis2: {},
memory: {}