From 81ccb3d4130ece8805b0f147552b5b36e0fb082a Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Sun, 27 Sep 2015 09:35:26 -0300 Subject: [PATCH 1/2] rosie typing files --- rosie/rosie-tests.ts | 16 ++++++++++++++++ rosie/rosie.d.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 rosie/rosie-tests.ts create mode 100644 rosie/rosie.d.ts diff --git a/rosie/rosie-tests.ts b/rosie/rosie-tests.ts new file mode 100644 index 0000000000..ee6e65dc1e --- /dev/null +++ b/rosie/rosie-tests.ts @@ -0,0 +1,16 @@ +/// + +let resultObj: Object; +let resultFactory: rosie.IFactory; + +declare var Factory:rosie.IFactoryStatic; + +resultFactory = Factory.define('person').attr('name', 'John').sequence('id'); +resultObj = Factory.build('person'); + +resultFactory = Factory.define('some').sequence('id').attr('name', ['id'], (id: number) => { return 'Name ' + id.toString() }); +resultObj = Factory.build('some'); + + + +var rosieFactory = require('rosie').Factory; diff --git a/rosie/rosie.d.ts b/rosie/rosie.d.ts new file mode 100644 index 0000000000..7c20449fd0 --- /dev/null +++ b/rosie/rosie.d.ts @@ -0,0 +1,30 @@ +declare module 'rosie' { + export = rosie; +} + +declare module rosie { + interface IFactoryStatic { + + define(name: String, constructor?: Function): IFactory; + + build(name: string, attributes?: any[], options?: Object): Object; + + buildList(name: string, size: number, attributes?: any[], options?: Object): Object[]; + + attributes(attributes: Object, options?: Object): Object; + + options(options: Object): Object; + + extend(name: string): IFactory; + } + + interface IFactory { + + attr(name: string, dependenciesOrValue: any | string[], value?: any): IFactory; + + sequence(name: string, dependencies?: string[], builder?: Function) : IFactory; + + } + +} + From 3f7e0fe145c439e17450a1952c6f78b4f5b55efe Mon Sep 17 00:00:00 2001 From: Abner Oliveira Date: Sun, 27 Sep 2015 21:08:47 -0300 Subject: [PATCH 2/2] added rosie-tests and comments for methods from project --- rosie/rosie-tests.ts | 18 +++- rosie/rosie.d.ts | 197 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 201 insertions(+), 14 deletions(-) diff --git a/rosie/rosie-tests.ts b/rosie/rosie-tests.ts index ee6e65dc1e..740f1e40c5 100644 --- a/rosie/rosie-tests.ts +++ b/rosie/rosie-tests.ts @@ -11,6 +11,22 @@ resultObj = Factory.build('person'); resultFactory = Factory.define('some').sequence('id').attr('name', ['id'], (id: number) => { return 'Name ' + id.toString() }); resultObj = Factory.build('some'); +Factory.define('coach') + .option('buildPlayer', false) + .sequence('id') + .attr('players', ['id', 'buildPlayer'], function(id: any, buildPlayer: boolean) { + if (buildPlayer) { + return [Factory.build('player', {coach_id: id})]; + } + }) + .after(function(coach: any, options: any) { + if (options.buildPlayer) { + console.log('built player:', coach.players[0]); + } + }); +Factory.build('coach', {}, {buildPlayer: true}); -var rosieFactory = require('rosie').Factory; +import rosie = require('rosie'); + +var Factory = rosie.Factory; diff --git a/rosie/rosie.d.ts b/rosie/rosie.d.ts index 7c20449fd0..31c7aa6f97 100644 --- a/rosie/rosie.d.ts +++ b/rosie/rosie.d.ts @@ -1,30 +1,201 @@ -declare module 'rosie' { - export = rosie; -} - +// Type definitions for rosie +// Project: https://github.com/rosiejs/rosie +// Definitions by: Abner Oliveira +// Definitions: https://github.com/borisyankov/DefinitelyTyped declare module rosie { interface IFactoryStatic { - define(name: String, constructor?: Function): IFactory; - build(name: string, attributes?: any[], options?: Object): Object; + /** + * Defines a factory by name and constructor function. Call #attr and #option + * on the result to define the properties of this factory. + * + * @param {!string} name + * @param {function(object): *=} constructor + * @return {Factory} + */ + define(name: String, constructor?: Function): IFactory; - buildList(name: string, size: number, attributes?: any[], options?: Object): Object[]; + /** + * Locates a factory by name and calls #build on it. + * + * @param {string} name + * @param {object=} attributes + * @param {object=} options + * @return {*} + */ + build(name: string, attributes?: any, options?: Object): Object; - attributes(attributes: Object, options?: Object): Object; + /** + * Builds a collection of objects using the named factory. + * + * @param {string} name + * @param {number} size + * @param {object=} attributes + * @param {object=} options + * @return {Array.<*>} + */ + buildList(name: string, size: number, attributes?: any, options?: Object): Object[]; - options(options: Object): Object; - - extend(name: string): IFactory; + /** + * Locates a factory by name and calls #attributes on it. + * + * @param {string} name + * @param {object} attributes + * @param {object} options + * @return {object} + */ + attributes(name: string, attributes: Object, options?: Object): Object; } interface IFactory { + /** + * Define an attribute on this factory. Attributes can optionally define a + * default value, either as a value (e.g. a string or number) or as a builder + * function. For example: + * + * // no default value for age + * Factory.define('Person').attr('age') + * + * // static default value for age + * Factory.define('Person').attr('age', 18) + * + * // dynamic default value for age + * Factory.define('Person').attr('age', function() { + * return Math.random() * 100; + * }) + * + * Attributes with dynamic default values can depend on options or other + * attributes: + * + * Factory.define('Person').attr('age', ['name'], function(name) { + * return name === 'Brian' ? 30 : 18; + * }); + * + * By default if the consumer of your factory provides a value for an + * attribute your builder function will not be called. You can override this + * behavior by declaring that your attribute depends on itself: + * + * Factory.define('Person').attr('spouse', ['spouse'], function(spouse) { + * return Factory.build('Person', spouse); + * }); + * + * As in the example above, this can be a useful way to fill in + * partially-specified child objects. + * + * @param {string} attr + * @param {Array.=} | any dependenciesOrValue + * @param any + * @return {Factory} + */ + attr(name: string, dependenciesOrValue: any | string[], value?: any): IFactory; - attr(name: string, dependenciesOrValue: any | string[], value?: any): IFactory; + /** + * Define an option for this factory. Options are values that may inform + * dynamic attribute behavior but are not included in objects built by the + * factory. Like attributes, options may have dependencies. Unlike + * attributes, options may only depend on other options. + * + * Factory.define('Person') + * .option('includeRelationships', false) + * .attr( + * 'spouse', + * ['spouse', 'includeRelationships'], + * function(spouse, includeRelationships) { + * return includeRelationships ? + * Factory.build('Person', spouse) : + * null; + * }); + * + * Factory.build('Person', null, { includeRelationships: true }); + * + * Options may have either static or dynamic default values, just like + * attributes. Options without default values must have a value specified + * when building. + * + * @param {string} opt + * @param {Array.=} | any dependencies or value + * @param {*=} value + * @return {Factory} + */ + option(name: string, dependenciesOrValue: any | string[], value?: any): IFactory; - sequence(name: string, dependencies?: string[], builder?: Function) : IFactory; + /** + * Defines an attribute that, by default, simply has an auto-incrementing + * numeric value starting at 1. You can provide your own builder function + * that accepts the number of the sequence and returns whatever value you'd + * like it to be. + * + * Sequence values are inherited such that a factory derived from another + * with a sequence will share the state of that sequence and they will never + * conflict. + * + * Factory.define('Person').sequence('id'); + * + * @param {string} attr + * @param {Array.=} dependencies + * @param {function(number): *=} builder + * @return {Factory} + */ + sequence(name: string, dependencies?: string[], builder?: Function) : IFactory; + + /** + * Sets a post-processor callback that will receive built objects and the + * options for the build just before they are returned from the #build + * function. + * + * @param {function(object, ?object)} callback + * @return {Factory} + */ + after(functionArg: Function): IFactory; + + /** + * Sets the constructor for this factory to be another factory. This can be + * used to create more specific sub-types of factories. + * + * @param {Factory} parentFactory + * @return {Factory} + */ + inherits(functionArg: Function): IFactory; + + /** + * Builds a plain object containing values for each of the declared + * attributes. The result of this is the same as the result when using #build + * when there is no constructor registered. + * + * @param {object=} attributes + * @param {object=} options + * @return {object} + */ + attributes(attributes:Object, options: Object): Object; + + /** + * Generates values for all the registered options using the values given. + * + * @private + * @param {object} options + * @return {object} + */ + options(options: Object): Object; + + /** + * Builds objects by getting values for all attributes and optionally passing + * the result to a constructor function. + * + * @param {object=} attributes + * @param {object=} options + * @return {*} + */ + build(attributes: Object, options: Object): Object; + + buildList(size: number, attributes: Object, options: Object): Object[]; } } +declare var rosie: { Factory: rosie.IFactoryStatic }; + +declare module 'rosie' { + export = rosie; +}