factory-mate
Advanced tools
Comparing version 1.2.1 to 1.3.0
export declare class FactoryMate { | ||
static definedConstructors: any[]; | ||
static define(cns: any, initFunction: () => void): void; | ||
static defineWithName(cns: any, alias: string, initFunction: () => void): void; | ||
static build(itemName: string, overrideFn?: (clazz: any) => any): any; | ||
@@ -5,0 +6,0 @@ static buildMany(itemName: string, numberToBuild?: number, overrideFn?: (clazz: any) => any): any[]; |
@@ -7,3 +7,7 @@ "use strict"; | ||
FactoryMate.define = function (cns, initFunction) { | ||
FactoryMate.defineWithName(cns, cns.name, initFunction); | ||
}; | ||
FactoryMate.defineWithName = function (cns, alias, initFunction) { | ||
FactoryMate.definedConstructors.push({ | ||
classAlias: alias, | ||
classConstructor: cns, | ||
@@ -34,5 +38,3 @@ initializationFunction: initFunction | ||
FactoryMate.locateConstructor = function (objectName) { | ||
var clazz = FactoryMate.definedConstructors.find(function (stored) { | ||
return stored.classConstructor.name === objectName; | ||
}); | ||
var clazz = FactoryMate.definedConstructors.find(function (stored) { return stored.classAlias === objectName; }); | ||
if (clazz === undefined) { | ||
@@ -39,0 +41,0 @@ throw new Error("Class with name " + objectName + " is not registered to FactoryMate."); |
{ | ||
"name": "factory-mate", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "TypeScript library for building domain objects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -72,2 +72,29 @@ # FactoryMate | ||
### Additional Building Methods | ||
#### Named Templates | ||
In certain cases, it may be desirable to have more than one template per class. In order to create more than one template per class, or to give a template a name other than the class it is representing, a 'named template' can be created using ```defineWithName```: | ||
``` typescript | ||
// GroceryItemFactory.ts | ||
import { FactoryMate, FactoryMateAware } from 'factory-mate'; | ||
import { GroceryItem } from './GroceryItem'; | ||
@FactoryMateAware | ||
export class GroceryItemFactory { | ||
// The @FactoryMateAware annotation will automatically call the define() function at runtime | ||
public define() { | ||
FactoryMate.define(GroceryItem, (): GroceryItem => { | ||
const groceryItem = new GroceryItem(); | ||
groceryItem.groceryName = 'crispy chips'; | ||
return groceryItem; | ||
}); | ||
FactoryMate.defineWithName(GroceryItem, 'specialChips', (): GroceryItem => { | ||
const groceryItem = new GroceryItem(); | ||
groceryItem.groceryName = 'limited edition flavor chips'; | ||
return groceryItem; | ||
}); | ||
} | ||
} | ||
``` | ||
#### Overriding a Template's Variables | ||
@@ -90,3 +117,3 @@ If for specific tests there is a need to override one or more variables in the template, this can be accomplished via an optional parameter to `build`: | ||
### Sequence Generation | ||
FactoryMate supports infinite, numerical sequence generation via the ```NumberGenerator``` class. This can be helpful for the purposes of generating ID values for domain objects to better represent real world scenarios (e.g. keys in a data store) | ||
FactoryMate supports infinite, numerical sequence generation via the ```NumberGenerator``` class. This can be helpful for the purposes of generating ID values for domain objects to better represent real world scenarios (e.g. IDs in a data store) | ||
@@ -93,0 +120,0 @@ In order to add sequential generation support to an entity, it can be imported into it's factory as such: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16495
147
190