Skip to content

Commit

Permalink
New write/promise
Browse files Browse the repository at this point in the history
Model compilation moved to model.js
  • Loading branch information
rauchg committed Jun 7, 2010
1 parent 5cd7b26 commit 3350522
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 1,097 deletions.
40 changes: 0 additions & 40 deletions examples/fs.js

This file was deleted.

40 changes: 0 additions & 40 deletions examples/models/User.js

This file was deleted.

70 changes: 0 additions & 70 deletions examples/models/file.js

This file was deleted.

19 changes: 0 additions & 19 deletions examples/simple.js

This file was deleted.

50 changes: 0 additions & 50 deletions examples/user.js

This file was deleted.

69 changes: 31 additions & 38 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Connection = this.Connection = Class({
this.base = base;
this.name = uri.pathname.replace('/', '');
this.uri = uri;
this._compiled = {};
this._queued = [];
EventEmitter.call(this);
this._open();
},

_open: function(){
this.db = new mongo.Db(this.name, new mongo.Server(uri.hostname, uri.port, options));
this.db.open(function(err){
if (err) return self._error("Can't connect to " + url.format(uri));
Expand All @@ -22,9 +29,6 @@ Connection = this.Connection = Class({
self._collection = collection;
});
});
this._compiled = {};
this._queued = [];
EventEmitter.call(this);
},

model: function(model){
Expand All @@ -38,40 +42,9 @@ Connection = this.Connection = Class({
},

_compile: function(model){
var _model = this.base.model(model), _definition;
_definition.extend = _model['static'] || {};
_definition.include = [Model];
_definition.__doc = this._compileProperties(_model.properties);
_model = Class(_definition);
_model.prototype._connection = _model._connection = this;
this._compiled[model] = _model;
return _model;
},

_compileProperties: function(props){
var _props = props || [], _ret = {};
_props.forEach(function(prop){
if (typeof prop == 'string'){
_ret[prop] = '';
} else if (prop instanceof Array){
// inspect first element
if (_props[prop].length == 0)
throw new Error('Bad `properties` definition. Empty array');
if (_props[prop].length > 1)
throw new Error('Bad `properties` definition. An array cannot contain multiple elements.');
switch (typeof _props[prop][0]){
// simply an array
case 'string':
_ret[_props[prop][0]]
break
case 'object':
break;
default:
throw new Error('Bad `properties` definition. Array contains illegal element')
}
}
});
return _props;
var _compiled = Model.compile(this.base.model(model), this);
this._compiled[model] = _compiled;
return _compiled;
},

_queue: function(method, args){
Expand Down Expand Up @@ -100,4 +73,24 @@ for (var i in Collection.prototype){
return this._queue(i, arguments);
};
}
}
}

MockCollection = Class({

find: function(query, options, callback){
callback({}, []); // will yield no results
},

save: function(doc, callback){
callback();
}

});

MockConnection = Connection.extend({

_open: function(){
this._collection = new MockCollection();
}

});
Loading

0 comments on commit 3350522

Please sign in to comment.