Skip to content

Conversation

@gskifstad
Copy link
Owner

@gskifstad gskifstad commented Mar 10, 2021

This PR adds an alternate type for rosie's attr to allow option arguments that serve purely as a tool to dynamically generate other attributes on the factory, and are not part of the resultant object's attributes. See https://github.com/rosiejs/rosie#programmatic-generation-of-attributes as an example of this use case:

const moment = require('moment');

Factory.define('matches')
  .attr('seasonStart', '2016-01-01')
  .option('numMatches', 2)
  .attr('matches', ['numMatches', 'seasonStart'], (numMatches, seasonStart) => {
    const matches = [];
    for (const i = 1; i <= numMatches; i++) {
      matches.push({
        matchDate: moment(seasonStart).add(i, 'week').format('YYYY-MM-DD'),
        homeScore: Math.floor(Math.random() * 5),
        awayScore: Math.floor(Math.random() * 5),
      });
    }
    return matches;
  });

Factory.build('matches', { seasonStart: '2016-03-12' }, { numMatches: 3 });
// Built object (note scores are random):
//{
//  seasonStart: '2016-03-12',
//  matches: [
//    { matchDate: '2016-03-19', homeScore: 3, awayScore: 1 },
//    { matchDate: '2016-03-26', homeScore: 0, awayScore: 4 },
//    { matchDate: '2016-04-02', homeScore: 1, awayScore: 0 }
//  ]
//}

(option 'numMatches' is just a dependency for the 'matches' attribute)

Please fill in this template.

Select one of these and delete the others:

If changing an existing definition:

n/a

…to dynamically generate other attributes on the factory, and are not part of the resultant object's attributes. See https://github.com/rosiejs/rosie#programmatic-generation-of-attributes as an example of this use case (option 'numMatches' is just a dependency for the 'matches' attribute)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants