GithubHelp home page GithubHelp logo

isabella232 / rosie-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tabcorp/rosie

0.0 0.0 0.0 246 KB

factory for building JavaScript objects, mostly useful for setting up test data. Inspired by factory_girl

License: Other

JavaScript 99.95% Ruby 0.05%

rosie-1's Introduction

Rosie

Rosie the Riveter

Rosie is a factory for building JavaScript objects, mostly useful for setting up test data. It is inspired by factory_girl.

Usage

Define your factory, giving it a name and optionally a constructor function:

Factory.define('game', Game)
  .sequence('id')
  .attr('is_over', false)
  .attr('created_at', function() { return new Date(); })
  .attr('random_seed', function() { return Math.random(); })

  // Default to two players. If players were given, fill in
  // whatever attributes might be missing.
  .attr('players', ['players'], function(players) {
    if (!players) { players = [{}, {}]; }
    return players.map(function(data) {
      return Factory.attributes('player', data);
    });
  });

Factory.define('player')
  .sequence('id')
  .sequence('name', function(i) { return 'player' + i; })

  // Define `position` to depend on `id`.
  .attr('position', ['id'], function(id) {
    var positions = ['pitcher', '1st base', '2nd base', '3rd base'];
    return positions[id % positions.length];
  });

Factory.define('disabled-player').extend('player').attr('state', 'disabled')

Now you can build an object, passing in attributes that you want to override:

var game = Factory.build('game', {is_over:true});

Which returns an object that looks roughly like:

{
  id:           1,
  is_over:      true,   // overriden when building
  created_at:   Fri Apr 15 2011 12:02:25 GMT-0400 (EDT),
  random_seed:  0.8999513240996748,
  players: [
                {id: 1, name:'Player 1'},
                {id: 2, name:'Player 2'}
  ]
}

For a factory with a constructor, if you want just the attributes:

Factory.attributes('game') // return just the attributes

You can also define a callback function to be run after building an object:

Factory.define('coach')
  .option('buildPlayer', false)
  .sequence('id')
  .attr('players', ['id', 'buildPlayer'], function(id, buildPlayer) {
    if (buildPlayer) {
      return [Factory.build('player', {coach_id: id})];
    }
  })
  .after(function(coach, options) {
    if (options.buildPlayer) {
      console.log('built player:', coach.players[0]);
    }
  });

Factory.build('coach', {}, {buildPlayer: true});

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Install the test dependencies (script/bootstrap - requires NodeJS and npm)
  4. Make your changes and make sure the tests pass (npm test)
  5. Commit your changes (git commit -am 'Added some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new Pull Request

Credits

Thanks to Daniel Morrison for the name and Jon Hoyt for inspiration and brainstorming the idea.

rosie-1's People

Contributors

bkeepers avatar dishwasha avatar eventualbuddha avatar freshtonic avatar jonstorer avatar maspwr avatar mdarnall avatar mkuklis avatar subakva avatar uniservo1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.