-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
816bf2a
commit 9825fd8
Showing
8 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.