ember-data-factory-guy 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,6 +21,8 @@ module.exports = function(grunt) {
21
21
  gem: {
22
22
  files: {
23
23
  "vendor/assets/javascripts/ember_data_factory_guy.js": [
24
+ 'src/sequence.js',
25
+ 'src/model_definition.js',
24
26
  'src/factory_guy.js',
25
27
  'src/store.js',
26
28
  'src/factory_guy_test_mixin.js'],
data/README.md CHANGED
@@ -42,11 +42,12 @@ Add fixtures to the store using the:
42
42
  * DS.ActiveModelAdapter
43
43
 
44
44
  NOTE: The benefit of using FactoryGuy is that you can run your tests with the
45
- default adapter that you store normally uses. In other words: You do not have
46
- to use the DS.FixtureAdapter. But if you do choose to use the Fixture adapter,
47
- which does not run any faster, and does not handle associations as elegantly ( if at all ),
45
+ default adapter that your application's store normally uses. In other words:
46
+ You do not have to use the DS.FixtureAdapter. But if you do choose to use the Fixture adapter,
47
+ which does not run any faster, and does not handle associations as elegantly
48
+ ( and in some cases not at all ),
48
49
  you may run into problems with accessing associations.
49
- If you do get these errors try requiring the factory_guy_has_many.js file
50
+ If you do get these types of errors try requiring the factory_guy_has_many.js file
50
51
  ( located in dist dir and vendor dir ) AFTER you require ember-data,
51
52
  but BEFORE you require your models.
52
53
 
@@ -78,12 +79,13 @@ but BEFORE you require your models.
78
79
 
79
80
  // default values for 'user' attributes
80
81
  default: {
81
- type: 'normal'
82
- name: FactoryGuy.generate('userName'),
82
+ type: 'normal',
83
+ // use the 'userName' sequence for this attribute
84
+ name: FactoryGuy.generate('userName')
83
85
  },
84
86
  // named 'user' type with custom attributes
85
87
  admin: {
86
- type: 'superuser'
88
+ type: 'superuser',
87
89
  name: 'Admin'
88
90
  }
89
91
  });
@@ -155,6 +157,7 @@ but BEFORE you require your models.
155
157
  // start asking for data, as soon as you get the model
156
158
  //
157
159
  var user = store.makeFixture('user'); // user.toJSON() = {id: 1, name: 'User1', type: 'normal'}
160
+ // note that the user name is a sequence
158
161
  var user = store.makeFixture('user'); // user.toJSON() = {id: 2, name: 'User2', type: 'normal'}
159
162
  var user = store.makeFixture('user', {name: 'bob'}); // user.toJSON() = {id: 3, name: 'bob', type: 'normal'}
160
163
  var user = store.makeFixture('admin'); // user.toJSON() = {id: 4, name: 'Admin', type: 'superuser'}
@@ -169,7 +172,7 @@ but BEFORE you require your models.
169
172
  user.get('projects.firstObject.user') == user;
170
173
 
171
174
  // and to create lists
172
- var users = store.makeList('user');
175
+ var users = store.makeList('user', 3);
173
176
 
174
177
  ```
175
178
 
data/bower.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-data-factory-guy",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "authors": [
5
5
  "Daniel Sudol <dansudol@yahoo.com>",
6
6
  "Opak Alex <opak.alexandr@gmail.com>"
@@ -113,7 +113,7 @@ ModelDefinition = function (model, config) {
113
113
  // initialize
114
114
  parseConfig(config);
115
115
  }
116
- FactoryGuy = Ember.Object.reopenClass({
116
+ FactoryGuy = {
117
117
  modelDefinitions: {},
118
118
 
119
119
  /**
@@ -280,7 +280,7 @@ FactoryGuy = Ember.Object.reopenClass({
280
280
  modelClass['FIXTURES'].push(fixture);
281
281
  return fixture;
282
282
  }
283
- })
283
+ }
284
284
  DS.Store.reopen({
285
285
 
286
286
  usingFixtureAdapter: function() {
@@ -1 +1 @@
1
- Sequence=function(fn){var index=1;var fn=fn;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(sequenceName){if(!sequences[sequenceName]){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequences[sequenceName].next()};this.build=function(name,opts){var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,opts);for(attr in fixture){if(typeof fixture[attr]=="function"){fixture[attr]=fixture[attr].call(this)}}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};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=Ember.Object.reopenClass({modelDefinitions:{},define:function(model,config){if(this.modelDefinitions[model]){this.modelDefinitions[model].merge(config)}else{this.modelDefinitions[model]=new ModelDefinition(model,config)}},generate:function(sequenceName){return function(){return this.generate(sequenceName)}},lookupModelForName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition.model}}},lookupDefinitionForName: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.lookupDefinitionForName(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.lookupDefinitionForName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,opts)},resetModels:function(store){var typeMaps=store.typeMaps;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}});DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var modelName=FactoryGuy.lookupModelForName(name);var fixture=FactoryGuy.build(name,options);var modelType=this.modelFor(modelName);if(this.usingFixtureAdapter()){this.setBelongsToFixturesAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var self=this;var model;Em.run(function(){model=self.push(modelName,fixture);self.setBelongsToRESTAdapter(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},setBelongsToFixturesAdapter:function(modelType,modelName,parentFixture){var store=this;var adapter=this.adapterFor("application");var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(relationship){var hasManyModel=store.modelFor(Em.String.singularize(relationship));if(parentFixture[relationship]){parentFixture[relationship].forEach(function(id){var hasManyfixtures=adapter.fixturesForType(hasManyModel);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=parentFixture.id})}})}},setBelongsToRESTAdapter:function(modelType,modelName,parent){var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(name){var children=parent.get(name);if(children.get("length")>0){children.forEach(function(child){child.set(modelName,parent)})}})}},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);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.lookupModelForName(name);this.stubEndpointForHttpRequest("/"+Em.String.pluralize(model),this.buildAjaxResponse(name,opts),{type:"POST"})},buildAjaxResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var model=FactoryGuy.lookupModelForName(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
+ Sequence=function(fn){var index=1;var fn=fn;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(sequenceName){if(!sequences[sequenceName]){throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+model+"' definition")}return sequences[sequenceName].next()};this.build=function(name,opts){var modelAttributes=namedModels[name]||{};var fixture=$.extend({},defaultAttributes,modelAttributes,opts);for(attr in fixture){if(typeof fixture[attr]=="function"){fixture[attr]=fixture[attr].call(this)}}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};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(sequenceName){return function(){return this.generate(sequenceName)}},lookupModelForName:function(name){for(model in this.modelDefinitions){var definition=this.modelDefinitions[model];if(definition.matchesName(name)){return definition.model}}},lookupDefinitionForName: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.lookupDefinitionForName(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.lookupDefinitionForName(name);if(!definition){throw new Error("Can't find that factory named ["+name+"]")}return definition.buildList(name,number,opts)},resetModels:function(store){var typeMaps=store.typeMaps;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}};DS.Store.reopen({usingFixtureAdapter:function(){var adapter=this.adapterFor("application");return adapter instanceof DS.FixtureAdapter},makeFixture:function(name,options){var modelName=FactoryGuy.lookupModelForName(name);var fixture=FactoryGuy.build(name,options);var modelType=this.modelFor(modelName);if(this.usingFixtureAdapter()){this.setBelongsToFixturesAdapter(modelType,modelName,fixture);return FactoryGuy.pushFixture(modelType,fixture)}else{var self=this;var model;Em.run(function(){model=self.push(modelName,fixture);self.setBelongsToRESTAdapter(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},setBelongsToFixturesAdapter:function(modelType,modelName,parentFixture){var store=this;var adapter=this.adapterFor("application");var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(relationship){var hasManyModel=store.modelFor(Em.String.singularize(relationship));if(parentFixture[relationship]){parentFixture[relationship].forEach(function(id){var hasManyfixtures=adapter.fixturesForType(hasManyModel);var fixture=adapter.findFixtureById(hasManyfixtures,id);fixture[modelName]=parentFixture.id})}})}},setBelongsToRESTAdapter:function(modelType,modelName,parent){var relationShips=Ember.get(modelType,"relationshipNames");if(relationShips.hasMany){relationShips.hasMany.forEach(function(name){var children=parent.get(name);if(children.get("length")>0){children.forEach(function(child){child.set(modelName,parent)})}})}},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);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.lookupModelForName(name);this.stubEndpointForHttpRequest("/"+Em.String.pluralize(model),this.buildAjaxResponse(name,opts),{type:"POST"})},buildAjaxResponse:function(name,opts){var fixture=FactoryGuy.build(name,opts);var model=FactoryGuy.lookupModelForName(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.1.2"
4
+ s.version = "0.1.3"
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"]
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-data-factory-guy",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "authors": [
5
5
  "Daniel Sudol <dansudol@yahoo.com>",
6
6
  "Opak Alex <opak.alexandr@gmail.com>"
@@ -1,4 +1,4 @@
1
- FactoryGuy = Ember.Object.reopenClass({
1
+ FactoryGuy = {
2
2
  modelDefinitions: {},
3
3
 
4
4
  /**
@@ -165,4 +165,4 @@ FactoryGuy = Ember.Object.reopenClass({
165
165
  modelClass['FIXTURES'].push(fixture);
166
166
  return fixture;
167
167
  }
168
- })
168
+ }
@@ -45,8 +45,9 @@ test("Using sequences in definitions", function() {
45
45
  deepEqual(json, {id: 2, name: 'person #2', type: 'person type #1'}, 'in named attributes');
46
46
 
47
47
  throws( function() {
48
- FactoryGuy.build('bro')
49
- }, MissingSequenceError,
48
+ FactoryGuy.build('bro')
49
+ },
50
+ MissingSequenceError,
50
51
  "throws error when sequence name not found"
51
52
  )
52
53
  });
@@ -1,4 +1,119 @@
1
- FactoryGuy = Ember.Object.reopenClass({
1
+ Sequence = function (fn) {
2
+ var index = 1;
3
+ var fn = fn;
4
+
5
+ this.next = function () {
6
+ return fn.call(this, index++);
7
+ }
8
+
9
+ this.reset = function () {
10
+ index = 1;
11
+ }
12
+ }
13
+
14
+ function MissingSequenceError(message) {
15
+ this.toString = function() { return message }
16
+ }
17
+
18
+ ModelDefinition = function (model, config) {
19
+ var sequences = {};
20
+ var defaultAttributes = {};
21
+ var namedModels = {};
22
+ var modelId = 1;
23
+ this.model = model;
24
+
25
+ /**
26
+ @param name model name like 'user' or named type like 'admin'
27
+ @return boolean true if name is this definitions model or this definition
28
+ contains a named model with that name
29
+ */
30
+ this.matchesName = function (name) {
31
+ return model == name || namedModels[name];
32
+ }
33
+
34
+ this.merge = function (config) {
35
+ }
36
+
37
+ /**
38
+ @param sequenceName
39
+ @returns output of sequence function
40
+ */
41
+ this.generate = function (sequenceName) {
42
+ if (!sequences[sequenceName]) {
43
+ throw new MissingSequenceError("Can not find that sequence named ["+sequenceName+"] in '"+ model+"' definition")
44
+ }
45
+ return sequences[sequenceName].next();
46
+ }
47
+
48
+ this.build = function (name, opts) {
49
+ var modelAttributes = namedModels[name] || {};
50
+ var fixture = $.extend({}, defaultAttributes, modelAttributes, opts);
51
+ for (attr in fixture) {
52
+ if (typeof fixture[attr] == 'function') {
53
+ fixture[attr] = fixture[attr].call(this)
54
+ }
55
+ }
56
+ if (!fixture.id) {
57
+ fixture.id = modelId++;
58
+ }
59
+ return fixture;
60
+ }
61
+
62
+ /**
63
+ Build a list of fixtures
64
+
65
+ @param name model name or named model type
66
+ @param number of fixtures to build
67
+ @param opts attribute options
68
+ @returns array of fixtures
69
+ */
70
+ this.buildList = function (name, number, opts) {
71
+ var arr = [];
72
+ for (var i = 0; i < number; i++) {
73
+ arr.push(this.build(name, opts))
74
+ }
75
+ return arr;
76
+ }
77
+
78
+ this.reset = function () {
79
+ modelId = 1;
80
+ }
81
+
82
+ var parseDefault = function (object) {
83
+ if (!object) {
84
+ return
85
+ }
86
+ defaultAttributes = object;
87
+ }
88
+
89
+ var parseSequences = function (object) {
90
+ if (!object) {
91
+ return
92
+ }
93
+ for (sequenceName in object) {
94
+ var sequenceFn = object[sequenceName];
95
+ if (typeof sequenceFn != 'function') {
96
+ throw new Error('Problem with [' + sequenceName + '] sequence definition. Sequences must be functions')
97
+ }
98
+ object[sequenceName] = new Sequence(sequenceFn);
99
+ }
100
+ sequences = object;
101
+ }
102
+
103
+ var parseConfig = function (config) {
104
+ parseSequences(config.sequences);
105
+ delete config.sequences;
106
+
107
+ parseDefault(config.default);
108
+ delete config.default;
109
+
110
+ namedModels = config;
111
+ }
112
+
113
+ // initialize
114
+ parseConfig(config);
115
+ }
116
+ FactoryGuy = {
2
117
  modelDefinitions: {},
3
118
 
4
119
  /**
@@ -165,7 +280,7 @@ FactoryGuy = Ember.Object.reopenClass({
165
280
  modelClass['FIXTURES'].push(fixture);
166
281
  return fixture;
167
282
  }
168
- })
283
+ }
169
284
  DS.Store.reopen({
170
285
 
171
286
  usingFixtureAdapter: function() {
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.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: