Skip to content

Commit

Permalink
more exploring
Browse files Browse the repository at this point in the history
  • Loading branch information
samselikoff committed May 16, 2015
1 parent 816bf2a commit 9825fd8
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 11 deletions.
2 changes: 1 addition & 1 deletion addon/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default function() {

this._insert = function(collection, data) {
var _this = this;
var copy = JSON.parse(JSON.stringify(data));
var copy = data ? JSON.parse(JSON.stringify(data)) : {};
var returnData;

if (!Ember.isArray(copy)) {
Expand Down
26 changes: 23 additions & 3 deletions addon/model-registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { pluralize } from './utils/inflector';

var ModelRegistry = function() {
export default function(db) {

};
this.db = db;
this._registry = {};

this.register = function(type, typeClass) {
this._registry[type] = typeClass;
};

this.create = function(type, attrs) {
var collection = pluralize(type);

if (!this.db[collection]) {
db.createCollection(collection);
}

export default ModelRegistry;
var augmentedAttrs = this.db[collection].insert(attrs);

var instance = new this._registry[type](augmentedAttrs);

return instance;
}

};
11 changes: 10 additions & 1 deletion addon/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import extend from 'ember-cli-mirage/utils/extend';

var Model = function() {
var Model = function(attrs) {
var _this = this;

this.attrs = attrs;

Object.keys(attrs).forEach(function(attr) {
_this[attr] = attrs[attr];
});
};

Model.extend = extend;

export default Model;
36 changes: 36 additions & 0 deletions addon/utils/extend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//import _ from 'underscore';
/* jshint global _ */

export default function(protoProps, staticProps) {
var parent = this;
var child;

// The constructor function for the new subclass is either defined by you
// (the "constructor" property in your `extend` definition), or defaulted
// by us to simply call the parent's constructor.
if (protoProps && _.has(protoProps, 'constructor')) {
child = protoProps.constructor;
} else {
child = function(){ return parent.apply(this, arguments); };
}

// Add static properties to the constructor function, if supplied.

_.extend(child, parent, staticProps);

// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate;

// Add prototype properties (instance properties) to the subclass,
// if supplied.
if (protoProps) _.extend(child.prototype, protoProps);

// Set a convenience property in case the parent's prototype is needed
// later.
child.__super__ = parent.prototype;

return child;
};
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"qunit": "~1.17.1",
"pretender": "~0.6.0",
"ember-inflector": "~1.3.1",
"lodash": "~3.7.0"
"lodash": "~3.7.0",
"underscore": "~1.8.3"
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ module.exports = {
app.import(app.bowerDirectory + '/FakeXMLHttpRequest/fake_xml_http_request.js');
app.import(app.bowerDirectory + '/route-recognizer/dist/route-recognizer.js');
app.import(app.bowerDirectory + '/pretender/pretender.js');
app.import('vendor/ember-cli-mirage/shim.js', {
app.import('vendor/ember-cli-mirage/pretender-shim.js', {
type: 'vendor',
exports: { 'pretender': ['default'] }
});
app.import(app.bowerDirectory + '/ember-inflector/ember-inflector.js');
app.import(app.bowerDirectory + '/lodash/lodash.js');
app.import(app.bowerDirectory + '/underscore/underscore.js');
}
},

Expand Down
39 changes: 35 additions & 4 deletions tests/unit/model-registry-test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import ModelRegistry from 'ember-cli-mirage/model-registry';
import Model from 'ember-cli-mirage/model';
import Db from 'ember-cli-mirage/db';

module('mirage:model-registry');

test('it can be instantiated', function(assert) {
var modelRegistry = new ModelRegistry();
var modelRegistry = new ModelRegistry(new Db());
assert.ok(modelRegistry);
});

test('it can be instantiated', function(assert) {
var modelRegistry = new ModelRegistry();
test('it can create registered models', function(assert) {
var User = Model.extend();
var modelRegistry = new ModelRegistry(new Db());

modelRegistry.register('user', User);
var user = modelRegistry.create('user', {name: 'Link'});

modelRegistry.create('user');
assert.ok(user instanceof Model);
assert.ok(user instanceof User);
assert.deepEqual(user.attrs, {id: 1, name: 'Link'});
});

//test('it respects passed in attrs', function(assert) {
//var modelRegistry = new ModelRegistry(new Db());

//var User = Model.extend();
//modelRegistry.register('user', User);
//var user = modelRegistry.create('user', {name: 'Link'});

//assert.deepEqual(user, {id: 1, name: 'Link'});
//assert.equal(user.constructor, User);
//});

//test('it respects passed in attrs', function(assert) {
//var modelRegistry = new ModelRegistry(new Db());

//modelRegistry.register('user', Model.extend());
//reg.find('user', 1);
//reg.users
//var user = modelRegistry.create('user', {name: 'Link'});

//assert.deepEqual(user, {id: 1, name: 'Link'});
//});

File renamed without changes.

0 comments on commit 9825fd8

Please sign in to comment.