Skip to content

Commit a4a1e6e

Browse files
committed
Add missing section in factories article
1 parent 72ad6a6 commit a4a1e6e

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

content/snippets/js/s/complex-object-factories.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,41 @@ const author = AuthorFactory.build(
240240
>
241241
> Some of you might be wondering why I didn't choose to make the customized object contain functions, so that `email` can be specified as a function, instead of passing two parameters. This is a **design choice** I stand by, as the cost of calling a function for each property can easily pile up. Instead, most of the time, we can get away with passing a function that generates multiple properties at once. At most, we'll end up with an object and a function for each factory call.
242242
243+
### Clearing objects
244+
245+
In some cases, we might want to **clear out objects**, to make sure they don't interfere with our tests. This is especially useful when we're counting records or checking relationships, for example.
246+
247+
We can add two new methods, `clear` and `clearAll`, to our `Factory` class to handle this. These methods will simply access the **static variables** (`instances`, `indexedInstances` and `getterCache`) of the `Model` class and reset them.
248+
249+
```js [src/core/factory.js]
250+
import Model from '#src/core/model.js';
251+
252+
export default class Factory {
253+
// ...
254+
255+
static clear(model) {
256+
Model.instances[model] = [];
257+
Model.indexedInstances[model] = new Map();
258+
Model.getterCache[model] = {};
259+
}
260+
261+
static clearAll() {
262+
Model.instances = {};
263+
Model.indexedInstances = {};
264+
Model.getterCache = {};
265+
}
266+
}
267+
```
268+
269+
And here they are in action, clearing instances created by the `PostFactory`.
270+
271+
```js
272+
PostFactory.build();
273+
Post.all.length; // 1
274+
Factory.clear('Post');
275+
Post.all.length; // 0
276+
```
277+
243278
### Convenience methods
244279
245280
As you're well aware by this point, convenience methods are a staple of my coding style. I definitely dislike having to find the appropriate factory to call every time I need to create an object. I'd much rather call a method on the `Factory` class itself, specifying the model I want to create.
@@ -671,6 +706,8 @@ export default class Serializer {
671706
```
672707
673708
```js [src/core/factory.js]
709+
import Model from '#src/core/model.js';
710+
674711
const sequenceSymbol = Symbol('sequence');
675712

676713
const isSequence = value =>
@@ -728,6 +765,18 @@ export default class Factory {
728765
Factory.build(model, ...desiredTraits)
729766
);
730767
}
768+
769+
static clear(model) {
770+
Model.instances[model] = [];
771+
Model.indexedInstances[model] = new Map();
772+
Model.getterCache[model] = {};
773+
}
774+
775+
static clearAll() {
776+
Model.instances = {};
777+
Model.indexedInstances = {};
778+
Model.getterCache = {};
779+
}
731780
}
732781
```
733782

0 commit comments

Comments
 (0)