ember-data-factory-guy 0.3.7 → 0.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bower.json +1 -1
- data/dist/ember-data-factory-guy.js +24 -8
- data/dist/ember-data-factory-guy.min.js +1 -1
- data/ember-data-factory-guy.gemspec +1 -1
- data/package.json +1 -1
- data/src/factory_guy.js +11 -4
- data/src/model_definition.js +13 -4
- data/tests/factory_guy_test.js +8 -0
- data/vendor/assets/javascripts/ember_data_factory_guy.js +24 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d5da74e5338051bba04274e2d5f255df82cb352
|
4
|
+
data.tar.gz: cd94755528f0bc9563585d17c95ce8192826a0cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3db9a3da3647708a8053b49ff08a022099202d0241e0e642ad32edfc049bc1679519f6c15b54f0eec85e5e088266cee4ea23833f14edf475a90a28276c6995d
|
7
|
+
data.tar.gz: 8d3e9393ba1f2f4efdbe552319e517af9ab2b0516ae11b9e62cc789b2bad123b4f67c68c09eb5a376815268fd0fbfafaced86607b8c6888a0737cea7b0d2d00d
|
data/bower.json
CHANGED
@@ -42,13 +42,22 @@ ModelDefinition = function (model, config) {
|
|
42
42
|
}
|
43
43
|
|
44
44
|
/**
|
45
|
-
Call the next method on the named sequence function
|
45
|
+
Call the next method on the named sequence function. If the name
|
46
|
+
is a function, create the sequence with that function
|
46
47
|
|
47
|
-
@param
|
48
|
+
@param {String} name previously declared sequence name or
|
49
|
+
an the random name generate for inline functions
|
50
|
+
@param {Function} sequenceFn optional function to use as sequence
|
48
51
|
@returns {String} output of sequence function
|
49
52
|
*/
|
50
|
-
this.generate = function (
|
51
|
-
|
53
|
+
this.generate = function (name, sequenceFn) {
|
54
|
+
if (sequenceFn) {
|
55
|
+
if (!sequences[name]) {
|
56
|
+
// create and add that sequence function on the fly
|
57
|
+
sequences[name] = new Sequence(sequenceFn);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
var sequence = sequences[name];
|
52
61
|
if (!sequence) {
|
53
62
|
throw new MissingSequenceError("Can not find that sequence named [" + sequenceName + "] in '" + model + "' definition")
|
54
63
|
}
|
@@ -209,13 +218,19 @@ FactoryGuy = {
|
|
209
218
|
|
210
219
|
```
|
211
220
|
|
212
|
-
@param {String}
|
221
|
+
@param {String|Function} value previously declared sequence name or
|
222
|
+
an inline function to use as the sequence
|
213
223
|
@returns {Function} wrapper function that is called by the model
|
214
224
|
definition containing the sequence
|
215
225
|
*/
|
216
|
-
generate: function (
|
226
|
+
generate: function (nameOrFunction) {
|
227
|
+
var sortaRandomName = Math.floor((1 + Math.random()) * 0x10000).toString(16) + Date.now()
|
217
228
|
return function () {
|
218
|
-
|
229
|
+
if (Em.typeOf(nameOrFunction) == "function") {
|
230
|
+
return this.generate(sortaRandomName, nameOrFunction);
|
231
|
+
} else {
|
232
|
+
return this.generate(nameOrFunction);
|
233
|
+
}
|
219
234
|
}
|
220
235
|
},
|
221
236
|
|
@@ -235,7 +250,8 @@ FactoryGuy = {
|
|
235
250
|
|
236
251
|
```
|
237
252
|
|
238
|
-
@param {String} fixture name
|
253
|
+
@param {String} fixtureName fixture name
|
254
|
+
@param {Object} opts options
|
239
255
|
@returns {Function} wrapper function that will build the association json
|
240
256
|
*/
|
241
257
|
association: function (fixtureName, opts) {
|
@@ -1 +1 @@
|
|
1
|
-
Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};function MissingSequenceError(message){this.toString=function(){return message}}ModelDefinition=function(model,config){var sequences={};var defaultAttributes={};var namedModels={};var modelId=1;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.merge=function(config){};this.generate=function(
|
1
|
+
Sequence=function(fn){var index=1;this.next=function(){return fn.call(this,index++)};this.reset=function(){index=1}};function MissingSequenceError(message){this.toString=function(){return message}}ModelDefinition=function(model,config){var sequences={};var defaultAttributes={};var namedModels={};var modelId=1;this.model=model;this.matchesName=function(name){return model==name||namedModels[name]};this.merge=function(config){};this.generate=function(name,sequenceFn){if(sequenceFn){if(!sequences[name]){sequences[name]=new Sequence(sequenceFn)}}var sequence=sequences[name];if(!sequence){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequence.next()};this.build=function(name,opts){var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,opts);for(attribute in fixture){if(Ember.typeOf(fixture[attribute])=="function"){fixture[attribute]=fixture[attribute].call(this,fixture)}else if(Ember.typeOf(fixture[attribute])=="object"){fixture[attribute]=FactoryGuy.build(attribute,fixture[attribute])}}if(!fixture.id){fixture.id=modelId++}return fixture};this.buildList=function(name,number,opts){var arr=[];for(var i=0;i<number;i++){arr.push(this.build(name,opts))}return arr};this.reset=function(){modelId=1;for(name in sequences){sequences[name].reset()}};var parseDefault=function(object){if(!object){return}defaultAttributes=object};var parseSequences=function(object){if(!object){return}for(sequenceName in object){var sequenceFn=object[sequenceName];if(typeof sequenceFn!="function"){throw new Error("Problem with ["+sequenceName+"] sequence definition. Sequences must be functions")}object[sequenceName]=new Sequence(sequenceFn)}sequences=object};var parseConfig=function(config){parseSequences(config.sequences);delete config.sequences;parseDefault(config.default);delete config.default;namedModels=config};parseConfig(config)};FactoryGuy={modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},generate:function(nameOrFunction){var sortaRandomName=Math.floor((1+Math.random())*65536).toString(16)+Date.now();return function(){if(Em.typeOf(nameOrFunction)=="function"){return this.generate(sortaRandomName,nameOrFunction)}else{return this.generate(nameOrFunction)}}},association:function(fixtureName,opts){return function(){return FactoryGuy.build(fixtureName,opts)}},lookupModelForFixtureName:function(name){var definition=this.lookupDefinitionForFixtureName(name);if(definition){return definition.model}},lookupDefinitionForFixtureName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition}}},build:function(name,opts){var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.build(name,opts)},buildList:function(name,number,opts){var definition=this.lookupDefinitionForFixtureName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,opts)},resetModels:function(store){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];definition.reset();try{var modelType=store.modelFor(definition.model);if(store.usingFixtureAdapter()){modelType.FIXTURES=[]}store.unloadAll(modelType)}catch(e){}}},pushFixture:function(modelClass,fixture){if(!modelClass["FIXTURES"]){modelClass["FIXTURES"]=[]}modelClass["FIXTURES"].push(fixture);return fixture},clear:function(opts){if(!opts){this.modelDefinitions={}}}};DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var modelName=FactoryGuy.lookupModelForFixtureName(name);var fixture=FactoryGuy.build(name,options);var modelType=this.modelFor(modelName);if(this.usingFixtureAdapter()){this.setAssociationsForFixtureAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var store=this;var model;Em.run(function(){store.findEmbeddedBelongsToAssociationsForRESTAdapter(modelType,fixture);if(fixture.type){modelName=fixture.type.underscore()}model=store.push(modelName,fixture);store.setAssociationsForRESTAdapter(modelType,modelName,model)});return model}},makeList:function(name,number,options){var arr=[];for(var i=0;i<number;i++){arr.push(this.makeFixture(name,options))}return arr},setAssociationsForFixtureAdapter:function(modelType,modelName,fixture){var self=this;var adapter=this.adapterFor("application");Ember.get(modelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind=="hasMany"){if(fixture[relationship.key]){fixture[relationship.key].forEach(function(id){var hasManyfixtures=adapter.fixturesForType(relationship.type);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=fixture.id})}}if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(belongsToRecord){if(typeof belongsToRecord=="object"){FactoryGuy.pushFixture(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord.id}var hasManyName=self.findHasManyRelationshipNameForFixtureAdapter(relationship.type,relationship.parentType);var belongsToFixtures=adapter.fixturesForType(relationship.type);var belongsTofixture=adapter.findFixtureById(belongsToFixtures,fixture[relationship.key]);if(!belongsTofixture[hasManyName]){belongsTofixture[hasManyName]=[]}belongsTofixture[hasManyName].push(fixture.id)}}})},findEmbeddedBelongsToAssociationsForRESTAdapter:function(modelType,fixture){var store=this;Ember.get(modelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind=="belongsTo"){var belongsToRecord=fixture[relationship.key];if(Ember.typeOf(belongsToRecord)=="object"){belongsToRecord=store.push(relationship.type,belongsToRecord);fixture[relationship.key]=belongsToRecord}}})},setAssociationsForRESTAdapter:function(modelType,modelName,model){var self=this;Ember.get(modelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind=="hasMany"){var belongsToName=modelName;if(relationship.options&&relationship.options.inverse){belongsToName=relationship.options.inverse}var children=model.get(name)||[];children.forEach(function(child){child.set(belongsToName,model)})}if(relationship.kind=="belongsTo"){var belongsToRecord=model.get(name);if(belongsToRecord){var setAssociations=function(){var hasManyName=self.findRelationshipName("hasMany",belongsToRecord.constructor,model);if(hasManyName){belongsToRecord.get(hasManyName).addObject(model)}var oneToOneName=self.findRelationshipName("belongsTo",belongsToRecord.constructor,model);if(oneToOneName){belongsToRecord.set(oneToOneName,model)}};if(belongsToRecord.then){belongsToRecord.then(function(record){belongsToRecord=record;setAssociations()})}else{setAssociations()}}}})},findRelationshipName:function(kind,belongToModelType,childModel){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind==kind&&childModel instanceof relationship.type){relationshipName=relationship.key}});return relationshipName},findHasManyRelationshipNameForFixtureAdapter:function(belongToModelType,childModelType){var relationshipName;Ember.get(belongToModelType,"relationshipsByName").forEach(function(name,relationship){if(relationship.kind=="hasMany"&&childModelType==relationship.type){relationshipName=relationship.key}});return relationshipName},pushPayload:function(type,payload){if(this.usingFixtureAdapter()){var model=this.modelFor(modelName);FactoryGuy.pushFixture(model,payload)}else{this._super(type,payload)}}});DS.FixtureAdapter.reopen({createRecord:function(store,type,record){var promise=this._super(store,type,record);promise.then(function(){var relationShips=Ember.get(type,"relationshipNames");if(relationShips.belongsTo){relationShips.belongsTo.forEach(function(relationship){var belongsToRecord=record.get(relationship);if(belongsToRecord){var hasManyName=store.findRelationshipName("hasMany",belongsToRecord.constructor,record);belongsToRecord.get(hasManyName).addObject(record)}})}});return promise}});FactoryGuyTestMixin=Em.Mixin.create({setup:function(app){this.set("container",app.__container__);return this},useFixtureAdapter:function(app){app.ApplicationAdapter=DS.FixtureAdapter;this.getStore().adapterFor("application").simulateRemoteResponse=false},find:function(type,id){return this.getStore().find(type,id)},make:function(name,opts){return this.getStore().makeFixture(name,opts)},getStore:function(){return this.get("container").lookup("store:main")},pushPayload:function(type,hash){return this.getStore().pushPayload(type,hash)},pushRecord:function(type,hash){return this.getStore().push(type,hash)},stubEndpointForHttpRequest:function(url,json,options){options=options||{};var request={url:url,dataType:"json",responseText:json,type:options.type||"GET",status:options.status||200};if(options.data){request.data=options.data}$.mockjax(request)},handleCreate:function(name,opts){var model=FactoryGuy.lookupModelForFixtureName(name);this.stubEndpointForHttpRequest("/"+Em.String.pluralize(model),this.buildAjaxCreateResponse(name,opts),{type:"POST"})},buildAjaxCreateResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var model=FactoryGuy.lookupModelForFixtureName(name);var hash={};hash[model]=fixture;return hash},handleUpdate:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,{},{type:"PUT"})},handleDelete:function(root,id){this.stubEndpointForHttpRequest("/"+Em.String.pluralize(root)+"/"+id,{},{type:"DELETE"})},teardown:function(){FactoryGuy.resetModels(this.getStore())}});
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "ember-data-factory-guy"
|
4
|
-
s.version = "0.3.
|
4
|
+
s.version = "0.3.8"
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ["Daniel Sudol", "Alex Opak"]
|
7
7
|
s.email = ["dansudol@yahoo.com", "opak.alexandr@gmail.com"]
|
data/package.json
CHANGED
data/src/factory_guy.js
CHANGED
@@ -65,13 +65,19 @@ FactoryGuy = {
|
|
65
65
|
|
66
66
|
```
|
67
67
|
|
68
|
-
@param {String}
|
68
|
+
@param {String|Function} value previously declared sequence name or
|
69
|
+
an inline function to use as the sequence
|
69
70
|
@returns {Function} wrapper function that is called by the model
|
70
71
|
definition containing the sequence
|
71
72
|
*/
|
72
|
-
generate: function (
|
73
|
+
generate: function (nameOrFunction) {
|
74
|
+
var sortaRandomName = Math.floor((1 + Math.random()) * 0x10000).toString(16) + Date.now()
|
73
75
|
return function () {
|
74
|
-
|
76
|
+
if (Em.typeOf(nameOrFunction) == "function") {
|
77
|
+
return this.generate(sortaRandomName, nameOrFunction);
|
78
|
+
} else {
|
79
|
+
return this.generate(nameOrFunction);
|
80
|
+
}
|
75
81
|
}
|
76
82
|
},
|
77
83
|
|
@@ -91,7 +97,8 @@ FactoryGuy = {
|
|
91
97
|
|
92
98
|
```
|
93
99
|
|
94
|
-
@param {String} fixture name
|
100
|
+
@param {String} fixtureName fixture name
|
101
|
+
@param {Object} opts options
|
95
102
|
@returns {Function} wrapper function that will build the association json
|
96
103
|
*/
|
97
104
|
association: function (fixtureName, opts) {
|
data/src/model_definition.js
CHANGED
@@ -26,13 +26,22 @@ ModelDefinition = function (model, config) {
|
|
26
26
|
}
|
27
27
|
|
28
28
|
/**
|
29
|
-
Call the next method on the named sequence function
|
29
|
+
Call the next method on the named sequence function. If the name
|
30
|
+
is a function, create the sequence with that function
|
30
31
|
|
31
|
-
@param
|
32
|
+
@param {String} name previously declared sequence name or
|
33
|
+
an the random name generate for inline functions
|
34
|
+
@param {Function} sequenceFn optional function to use as sequence
|
32
35
|
@returns {String} output of sequence function
|
33
36
|
*/
|
34
|
-
this.generate = function (
|
35
|
-
|
37
|
+
this.generate = function (name, sequenceFn) {
|
38
|
+
if (sequenceFn) {
|
39
|
+
if (!sequences[name]) {
|
40
|
+
// create and add that sequence function on the fly
|
41
|
+
sequences[name] = new Sequence(sequenceFn);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
var sequence = sequences[name];
|
36
45
|
if (!sequence) {
|
37
46
|
throw new MissingSequenceError("Can not find that sequence named [" + sequenceName + "] in '" + model + "' definition")
|
38
47
|
}
|
data/tests/factory_guy_test.js
CHANGED
@@ -37,6 +37,9 @@ test("Using sequences in definitions", function() {
|
|
37
37
|
},
|
38
38
|
bro: {
|
39
39
|
type: FactoryGuy.generate('broType')
|
40
|
+
},
|
41
|
+
dude_inline: {
|
42
|
+
type: FactoryGuy.generate(function(num) { return 'Dude #' + num})
|
40
43
|
}
|
41
44
|
});
|
42
45
|
|
@@ -53,6 +56,11 @@ test("Using sequences in definitions", function() {
|
|
53
56
|
"throws error when sequence name not found"
|
54
57
|
)
|
55
58
|
|
59
|
+
var json = FactoryGuy.build('dude_inline');
|
60
|
+
deepEqual(json, {id: 3, name: 'person #3', type: 'Dude #1'}, 'as inline sequence function #1');
|
61
|
+
|
62
|
+
var json = FactoryGuy.build('dude_inline');
|
63
|
+
deepEqual(json, {id: 4, name: 'person #4', type: 'Dude #2'}, 'as inline sequence function #2');
|
56
64
|
});
|
57
65
|
|
58
66
|
|
@@ -42,13 +42,22 @@ ModelDefinition = function (model, config) {
|
|
42
42
|
}
|
43
43
|
|
44
44
|
/**
|
45
|
-
Call the next method on the named sequence function
|
45
|
+
Call the next method on the named sequence function. If the name
|
46
|
+
is a function, create the sequence with that function
|
46
47
|
|
47
|
-
@param
|
48
|
+
@param {String} name previously declared sequence name or
|
49
|
+
an the random name generate for inline functions
|
50
|
+
@param {Function} sequenceFn optional function to use as sequence
|
48
51
|
@returns {String} output of sequence function
|
49
52
|
*/
|
50
|
-
this.generate = function (
|
51
|
-
|
53
|
+
this.generate = function (name, sequenceFn) {
|
54
|
+
if (sequenceFn) {
|
55
|
+
if (!sequences[name]) {
|
56
|
+
// create and add that sequence function on the fly
|
57
|
+
sequences[name] = new Sequence(sequenceFn);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
var sequence = sequences[name];
|
52
61
|
if (!sequence) {
|
53
62
|
throw new MissingSequenceError("Can not find that sequence named [" + sequenceName + "] in '" + model + "' definition")
|
54
63
|
}
|
@@ -209,13 +218,19 @@ FactoryGuy = {
|
|
209
218
|
|
210
219
|
```
|
211
220
|
|
212
|
-
@param {String}
|
221
|
+
@param {String|Function} value previously declared sequence name or
|
222
|
+
an inline function to use as the sequence
|
213
223
|
@returns {Function} wrapper function that is called by the model
|
214
224
|
definition containing the sequence
|
215
225
|
*/
|
216
|
-
generate: function (
|
226
|
+
generate: function (nameOrFunction) {
|
227
|
+
var sortaRandomName = Math.floor((1 + Math.random()) * 0x10000).toString(16) + Date.now()
|
217
228
|
return function () {
|
218
|
-
|
229
|
+
if (Em.typeOf(nameOrFunction) == "function") {
|
230
|
+
return this.generate(sortaRandomName, nameOrFunction);
|
231
|
+
} else {
|
232
|
+
return this.generate(nameOrFunction);
|
233
|
+
}
|
219
234
|
}
|
220
235
|
},
|
221
236
|
|
@@ -235,7 +250,8 @@ FactoryGuy = {
|
|
235
250
|
|
236
251
|
```
|
237
252
|
|
238
|
-
@param {String} fixture name
|
253
|
+
@param {String} fixtureName fixture name
|
254
|
+
@param {Object} opts options
|
239
255
|
@returns {Function} wrapper function that will build the association json
|
240
256
|
*/
|
241
257
|
association: function (fixtureName, opts) {
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ember-data-factory-guy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Sudol
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-03 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Easily create Fixtures for Ember Data
|
15
15
|
email:
|