Comparing version 1.4.0 to 1.6.0
{ | ||
"name": "rosie", | ||
"version": "1.4.0", | ||
"version": "1.6.0", | ||
"description": "factory for building JavaScript objects, mostly useful for setting up test data. Inspired by factory_girl", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -74,3 +74,3 @@ # Rosie | ||
You can specify options that are used to programatically generate the attributes: | ||
You can specify options that are used to programmatically generate the attributes: | ||
@@ -111,2 +111,17 @@ ```js | ||
### Batch Specification of Attributes | ||
The convenience function `attrs` simplifies the common case of specifying multiple attributes in a batch. Rewriting the `game` example from above: | ||
```js | ||
Factory.define('game') | ||
.sequence('id') | ||
.attrs({ | ||
is_over: false, | ||
created_at: function() { return new Date(); }), | ||
random_seed: function() { return Math.random(); }) | ||
}) | ||
.attr('players', ['players'], function(players) { /* etc. */ }) | ||
``` | ||
### Post Build Callback | ||
@@ -216,7 +231,11 @@ | ||
.sequence('id') | ||
.attr('is_over', false); | ||
.attrs({ | ||
is_over: false, | ||
created_at: () => new Date(), | ||
random_seed: () => Math.random() | ||
}) | ||
// etc | ||
// index.js | ||
import Game from './factories/game'); | ||
import Game from './factories/game'; | ||
@@ -241,3 +260,3 @@ const game = Game.build({is_over: true}); | ||
* **Factory.define(``factory_name``)** - Defines a factory by name. Return an instance of a Factory that you call `.attr`, `.option`, `.sequence`, and `.after` on the result to define the properties of this factory. | ||
* **Factory.define(`factory_name`, `constructor`)** - Optionally pass a constuctor function, and the objects produced by `.build` will be passed through the `constructor` funtion. | ||
* **Factory.define(`factory_name`, `constructor`)** - Optionally pass a constuctor function, and the objects produced by `.build` will be passed through the `constructor` function. | ||
@@ -253,2 +272,10 @@ | ||
#### instance.attrs: | ||
Use this as a convenience function instead of calling `instance.attr` multiple times | ||
* **instance.attrs(`{attribute_1: value_1, attribute_2: value_2, ...}`)** - `attribute_i` is a string, `value_i` is either an object or generator function. | ||
See `instance.attr` above for details. Note: there is no way to specify dependencies using this method, so if you need that, you should use `instance.attr` instead. | ||
#### instance.option: | ||
@@ -255,0 +282,0 @@ |
@@ -11,3 +11,3 @@ /** | ||
this.construct = constructor; | ||
this.attrs = {}; | ||
this._attrs = {}; | ||
this.opts = {}; | ||
@@ -66,3 +66,3 @@ this.sequences = {}; | ||
builder = typeof value === 'function' ? value : function() { return value; }; | ||
this.attrs[attr] = { dependencies: dependencies || [], builder: builder }; | ||
this._attrs[attr] = { dependencies: dependencies || [], builder: builder }; | ||
return this; | ||
@@ -72,2 +72,26 @@ }, | ||
/** | ||
* Convenience function for defining a set of attributes on this object as | ||
* builder functions or static values. If you need to specify dependencies, | ||
* use #attr instead. | ||
* | ||
* For example: | ||
* | ||
* Factory.define('Person').attrs({ | ||
* name: 'Michael', | ||
* age: function() { return Math.random() * 100; } | ||
* }); | ||
* | ||
* @param {object} attributes | ||
* @return {Factory} | ||
*/ | ||
attrs: function(attributes) { | ||
for (var attr in attributes) { | ||
if (Factory.util.hasOwnProp(attributes, attr)) { | ||
this.attr(attr, attributes[attr]); | ||
} | ||
} | ||
return this; | ||
}, | ||
/** | ||
* Define an option for this factory. Options are values that may inform | ||
@@ -187,3 +211,3 @@ * dynamic attribute behavior but are not included in objects built by the | ||
options = this.options(options); | ||
for (var attr in this.attrs) { | ||
for (var attr in this._attrs) { | ||
this._attrValue(attr, attributes, options, [attr]); | ||
@@ -210,3 +234,3 @@ } | ||
var value = this._buildWithDependencies(this.attrs[attr], function(dep) { | ||
var value = this._buildWithDependencies(this._attrs[attr], function(dep) { | ||
if (Factory.util.hasOwnProp(options, dep)) { | ||
@@ -235,3 +259,3 @@ return options[dep]; | ||
_alwaysCallBuilder: function(attr) { | ||
var attrMeta = this.attrs[attr]; | ||
var attrMeta = this._attrs[attr]; | ||
return attrMeta.dependencies.indexOf(attr) >= 0; | ||
@@ -340,3 +364,3 @@ }, | ||
if (this.construct === undefined) { this.construct = factory.construct; } | ||
Factory.util.extend(this.attrs, factory.attrs); | ||
Factory.util.extend(this._attrs, factory._attrs); | ||
Factory.util.extend(this.opts, factory.opts); | ||
@@ -343,0 +367,0 @@ // Copy the parent's callbacks |
28933
440
325