ember-data-factory-guy 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,137 +1,162 @@
1
1
  FactoryGuy = Ember.Object.reopenClass({
2
- fixtureStore: {},
3
- fixtureLookup: {},
4
- modelIds: {},
2
+ modelDefinitions: {},
5
3
 
6
4
  /**
7
5
  ```javascript
8
6
 
9
- User = DS.Model.extend({
10
- name: DS.attr('string'),
11
- })
12
-
13
- FactoryGuy.define('user', {
14
- default: {
15
- name: "Fred"
16
- },
17
-
18
- bob: {
19
- name: "Bob"
20
- }
21
- });
22
-
23
- ```
24
-
25
- For the User model, you can define fixtures like 'bob' or just use 'user'
26
- and get default values.
27
-
28
- And to get those fixtures you would call them this way:
29
-
30
- FactoryGuy.build('user') or FactoryGuy.build('bob')
31
-
32
- @param model the model to define
33
- @param config your default and named fixtures
7
+ Person = DS.Model.extend({
8
+ type: DS.attr('string'),
9
+ name: DS.attr('string')
10
+ })
11
+
12
+ FactoryGuy.define('person', {
13
+ sequences: {
14
+ personName: function(num) {
15
+ return 'person #' + num;
16
+ },
17
+ personType: function(num) {
18
+ return 'person type #' + num;
19
+ }
20
+ },
21
+ default: {
22
+ type: 'normal',
23
+ name: FactoryGuy.generate('personName')
24
+ },
25
+ dude: {
26
+ type: FactoryGuy.generate('personType')
27
+ },
28
+ });
29
+
30
+ ```
31
+
32
+ For the Person model, you can define fixtures like 'dude' or just use 'person'
33
+ and get default values.
34
+
35
+ And to get those fixtures you would call them this way:
36
+
37
+ FactoryGuy.build('person') or FactoryGuy.build('dude')
38
+
39
+ @param model the model to define
40
+ @param config your model definition object
34
41
  */
35
42
  define: function (model, config) {
36
- var info = this.getModelInfo(model);
37
- for (key in config) {
38
- var value = config[key];
39
- info[key] = value;
40
- if (key != 'default') {
41
- this.fixtureLookup[key] = model;
42
- }
43
+ if (this.modelDefinitions[model]) {
44
+ this.modelDefinitions[model].merge(config);
45
+ } else {
46
+ this.modelDefinitions[model] = new ModelDefinition(model, config);
43
47
  }
44
- // setup id
45
- this.modelIds[model] = 0;
46
48
  },
47
49
 
48
- /**
49
-
50
- @param model
51
- */
52
- getModelInfo: function (model) {
53
- if (!this.fixtureStore[model]) {
54
- this.fixtureStore[model] = {};
50
+ generate: function (sequenceName) {
51
+ return function () {
52
+ return this.generate(sequenceName);
55
53
  }
56
- return this.fixtureStore[model];
57
54
  },
58
55
 
56
+
59
57
  /**
60
58
 
61
- @param name fixture name
62
- @returns model associated with fixture name
59
+ @param name fixture name could be model name like 'user'
60
+ or specific user like 'admin'
61
+ @returns model associated with fixture name
63
62
  */
64
63
  lookupModelForName: function (name) {
65
- var model = this.fixtureLookup[name];
66
- if (!model) {
67
- if (this.fixtureStore[name]) {
68
- model = name;
64
+ for (model in this.modelDefinitions) {
65
+ var definition = this.modelDefinitions[model];
66
+ if (definition.matchesName(name)) {
67
+ return definition.model;
69
68
  }
70
69
  }
71
- return model;
72
70
  },
73
71
 
74
72
  /**
75
- Generate next id for model
73
+
74
+ @param name fixture name could be model name like 'user'
75
+ or specific user like 'admin'
76
+ @returns definition associated with model
76
77
  */
77
- generateId: function (model) {
78
- var lastId = this.modelIds[model] || 0;
79
- this.modelIds[model] = lastId + 1;
80
- return this.modelIds[model];
78
+ lookupDefinitionForName: function (name) {
79
+ for (model in this.modelDefinitions) {
80
+ var definition = this.modelDefinitions[model];
81
+ if (definition.matchesName(name)) {
82
+ return definition;
83
+ }
84
+ }
81
85
  },
82
86
 
87
+
83
88
  /**
84
- Build fixtures for model or specific fixture name. For example:
89
+ Build fixtures for model or specific fixture name. For example:
85
90
 
86
- FactoryGuy.build('user') for User model
87
- FactoryGuy.build('bob') for User model with bob attributes
91
+ FactoryGuy.build('user') for User model
92
+ FactoryGuy.build('bob') for User model with bob attributes
88
93
 
89
- @param name fixture name
90
- @param opts options that will override default fixture values
91
- @returns {*}
94
+ @param name fixture name
95
+ @param opts options that will override default fixture values
96
+ @returns {*}
92
97
  */
93
98
  build: function (name, opts) {
94
- var model = this.lookupModelForName(name);
95
- if (!model) {
96
- throw new Error("can't find that factory named [" + name + "]");
99
+ var definition = this.lookupDefinitionForName(name);
100
+ if (!definition) {
101
+ throw new Error("Can't find that factory named [" + name + "]");
97
102
  }
103
+ return definition.build(name, opts);
104
+ },
105
+
106
+ /**
107
+ Build list of fixtures for model or specific fixture name. For example:
108
+
109
+ FactoryGuy.buildList('user', 2) for 2 User models
110
+ FactoryGuy.build('bob', 2) for 2 User model with bob attributes
98
111
 
99
- var modelInfo = this.fixtureStore[model];
100
- var modelAttributes = modelInfo[name] || {};
101
- var defaultModelAttributes = modelInfo.default;
102
- var fixture = $.extend({}, defaultModelAttributes, modelAttributes, opts);
103
- if(!fixture.id){
104
- fixture.id = this.generateId(model);
112
+ @param name fixture name
113
+ @param number number of fixtures to create
114
+ @param opts options that will override default fixture values
115
+ @returns list of fixtures
116
+ */
117
+ buildList: function (name, number, opts) {
118
+ var definition = this.lookupDefinitionForName(name);
119
+ if (!definition) {
120
+ throw new Error("Can't find that factory named [" + name + "]");
105
121
  }
106
- return fixture;
122
+ return definition.buildList(name, number, opts);
107
123
  },
108
124
 
109
125
  /**
110
- Clear model instances from FIXTURES array, and from store cache.
111
- Reset the id sequence for the models back to zero.
126
+ Clear model instances from FIXTURES array, and from store cache.
127
+ Reset the id sequence for the models back to zero.
112
128
  */
113
129
  resetModels: function (store) {
114
130
  var typeMaps = store.typeMaps;
115
- if (store.usingFixtureAdapter()) {
116
- for (typeKey in this.fixtureStore) {
117
- var modelType = store.modelFor(typeKey);
118
- modelType.FIXTURES = [];
131
+ for (model in this.modelDefinitions) {
132
+ var definition = this.modelDefinitions[model];
133
+ definition.reset();
134
+ try {
135
+ var modelType = store.modelFor(definition.model);
136
+ if (store.usingFixtureAdapter()) {
137
+ modelType.FIXTURES = [];
138
+ }
119
139
  store.unloadAll(modelType);
140
+ } catch (e) {
120
141
  }
121
- } else {
122
- for (model in typeMaps) {
123
- store.unloadAll(typeMaps[model].type);
124
- }
142
+ // } else {
143
+ // for (model in typeMaps) {
144
+ // store.unloadAll(typeMaps[model].type);
145
+ // }
146
+ // }
147
+
148
+ // for (model in this.modelDefinitions) {
149
+ // this.modelDefinitions[model].reset();
125
150
  }
126
- this.modelIds = {}
127
151
  },
128
152
 
129
153
  /**
130
- Push fixture to model's FIXTURES array.
131
- Used when store's adapter is a DS.FixtureAdapter.
154
+ Push fixture to model's FIXTURES array.
155
+ Used when store's adapter is a DS.FixtureAdapter.
132
156
 
133
- @param modelClass DS.Model type
134
- @param fixture the fixture to add
157
+ @param modelClass DS.Model type
158
+ @param fixture the fixture to add
159
+ @returns json fixture data
135
160
  */
136
161
  pushFixture: function (modelClass, fixture) {
137
162
  if (!modelClass['FIXTURES']) {
@@ -141,7 +166,6 @@ FactoryGuy = Ember.Object.reopenClass({
141
166
  return fixture;
142
167
  }
143
168
  })
144
-
145
169
  DS.Store.reopen({
146
170
 
147
171
  usingFixtureAdapter: function() {
@@ -153,9 +177,9 @@ DS.Store.reopen({
153
177
  Make new fixture and save to store. If the store is using FixtureAdapter,
154
178
  will push to FIXTURE array, otherwise will use push method on adapter.
155
179
 
156
- @param name
157
- @param options
158
- @returns {*}
180
+ @param name name of fixture
181
+ @param options fixture options
182
+ @returns json or record
159
183
  */
160
184
  makeFixture: function (name, options) {
161
185
  var modelName = FactoryGuy.lookupModelForName(name);
@@ -163,31 +187,48 @@ DS.Store.reopen({
163
187
  var modelType = this.modelFor(modelName);
164
188
 
165
189
  if (this.usingFixtureAdapter()) {
166
- this.setBelongsToFixturesAssociation(modelType, modelName, fixture);
190
+ this.setBelongsToFixturesAdapter(modelType, modelName, fixture);
167
191
  return FactoryGuy.pushFixture(modelType, fixture);
168
192
  } else {
169
193
  var self = this;
170
194
  var model;
171
195
  Em.run( function() {
172
196
  model = self.push(modelName, fixture);
173
- self.setBelongsToRestAssociation(modelType, modelName, model);
197
+ self.setBelongsToRESTAdapter(modelType, modelName, model);
174
198
  });
175
199
  return model;
176
200
  }
177
201
  },
178
202
 
179
203
  /**
180
- Trying to set the belongsTo association for FixtureAdapter,
204
+ Make a list of Fixtures
205
+
206
+ @param name name of fixture
207
+ @param number number of fixtures
208
+ @param options fixture options
209
+ @returns list of json fixtures or records depending on the adapter type
210
+ */
211
+ makeList: function (name, number, options) {
212
+ var arr = [];
213
+ for (var i = 0; i < number; i++) {
214
+ arr.push(this.makeFixture(name, options))
215
+ }
216
+ return arr;
217
+ },
218
+
219
+ /**
220
+ Set the belongsTo association for FixtureAdapter,
181
221
  with models that have a hasMany association.
182
222
 
183
- For example if a client hasMany projects, then set the client.id
184
- on each project that the client hasMany of, so that the project
185
- now has the belongsTo client association setup.
223
+ For example if a user hasMany projects, then set the user.id
224
+ on each project that the user hasMany of, so that the project
225
+ now has the belongsTo user association setup.
186
226
 
187
- @param name
188
- @param model
227
+ @param modelType model type like App.User
228
+ @param modelName model name like 'user'
229
+ @param parentFixture parent to assign as belongTo
189
230
  */
190
- setBelongsToFixturesAssociation: function (modelType, modelName, parentFixture) {
231
+ setBelongsToFixturesAdapter: function (modelType, modelName, parentFixture) {
191
232
  var store = this;
192
233
  var adapter = this.adapterFor('application');
193
234
  var relationShips = Ember.get(modelType, 'relationshipNames');
@@ -206,18 +247,18 @@ DS.Store.reopen({
206
247
  },
207
248
 
208
249
  /**
209
- * Trying to set the belongsTo association for the rest type models
210
- * with a hasMany association
211
- *
212
- * For example if a client hasMany projects, then set the client
213
- * on each project that the client hasMany of, so that the project
214
- * now has the belongsTo client association setup
215
- *
216
- * @param modelType
217
- * @param modelName
218
- * @param parent model to check for hasMany
250
+ Set the belongsTo association for the REST type models
251
+ with a hasMany association
252
+
253
+ For example if a user hasMany projects, then set the user
254
+ on each project that the user hasMany of, so that the project
255
+ now has the belongsTo user association setup
256
+
257
+ @param modelType model type like 'App.User'
258
+ @param modelName model name like 'user'
259
+ @param parent model to check for hasMany
219
260
  */
220
- setBelongsToRestAssociation: function (modelType, modelName, parent) {
261
+ setBelongsToRESTAdapter: function (modelType, modelName, parent) {
221
262
  var relationShips = Ember.get(modelType, 'relationshipNames');
222
263
 
223
264
  if (relationShips.hasMany) {
@@ -285,8 +326,9 @@ DS.FixtureAdapter.reopen({
285
326
  })
286
327
 
287
328
 
288
- FactoryGuyHelperMixin = Em.Mixin.create({
329
+ FactoryGuyTestMixin = Em.Mixin.create({
289
330
 
331
+ // Pass in the app root, which typically is App.
290
332
  setup: function(app) {
291
333
  this.set('container', app.__container__);
292
334
  return this;
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.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-04-24 00:00:00.000000000 +03:00
13
+ date: 2014-05-06 00:00:00.000000000 +03:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
  description: Easily create Fixtures for Ember Data
@@ -22,17 +22,21 @@ extensions: []
22
22
  extra_rdoc_files: []
23
23
  files:
24
24
  - .gitignore
25
+ - .travis.yml
25
26
  - Gruntfile.js
26
27
  - README.md
27
28
  - bower.json
28
29
  - dist/ember-data-factory-guy.js
29
30
  - dist/ember-data-factory-guy.min.js
31
+ - dist/factory_guy_has_many.js
30
32
  - ember-data-factory-guy.gemspec
31
33
  - lib/ember-data-factory-guy.rb
32
34
  - package.json
33
35
  - src/factory_guy.js
34
- - src/factory_guy_helper_mixin.js
36
+ - src/factory_guy_test_mixin.js
35
37
  - src/has_many.js
38
+ - src/model_definition.js
39
+ - src/sequence.js
36
40
  - src/store.js
37
41
  - tests/active_model_adapter_factory_test.js
38
42
  - tests/factory_guy_test.js
@@ -42,6 +46,7 @@ files:
42
46
  - tests/store_test.js
43
47
  - tests/support/factories/project_factory.js
44
48
  - tests/support/factories/user_factory.js
49
+ - tests/support/libs/sinon.js
45
50
  - tests/support/models/project.js
46
51
  - tests/support/models/user.js
47
52
  - tests/support/test_helper.js