Sha256: 0e2ee7c7f263ea8dd58ba84e58bf647d64e8aa5cb0bf43490d84906781a5e030

Contents?: true

Size: 1.28 KB

Versions: 34

Compression:

Stored size: 1.28 KB

Contents

var Factory = function(constructor) {
  this.construct = constructor;
  this.attrs = {};
  this.sequences = {};
};

Factory.prototype = {
  attr: function(attr, value) {
    callback = typeof value == 'function' ? value : function() { return value; };
    this.attrs[attr] = callback;
    return this;
  },

  sequence: function(attr, callback) {
    var factory = this;
    callback = callback || function(i) { return i; };
    this.attrs[attr] = function() {
      factory.sequences[attr] = factory.sequences[attr] || 0;
      return callback(++factory.sequences[attr]);
    };
    return this;
  },

  attributes: function(attrs) {
    attrs = attrs || {};
    for(var attr in this.attrs) {
      if(!attrs.hasOwnProperty(attr)) {
        attrs[attr] =  this.attrs[attr]();
      }
    }
    return attrs;
  },

  build: function(attrs) {
    var result = this.attributes(attrs);
    return this.construct ? new this.construct(result) : result;
  }
};

Factory.factories = {};

Factory.define = function(name, constructor) {
  var factory = new Factory(constructor);
  this.factories[name] = factory;
  return factory;
};

Factory.build = function(name, attrs) {
  return this.factories[name].build(attrs);
};

Factory.attributes = function(name, attrs) {
  return this.factories[name].attributes(attrs);
};

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
quby-5.6.6 spec/javascripts/support/rosie.js
quby-5.6.5 spec/javascripts/support/rosie.js
quby-5.6.3 spec/javascripts/support/rosie.js
quby-5.6.2 spec/javascripts/support/rosie.js
quby-5.6.1 spec/javascripts/support/rosie.js
quby-5.6.0 spec/javascripts/support/rosie.js
quby-5.5.0 spec/javascripts/support/rosie.js
quby-5.4.0 spec/javascripts/support/rosie.js
quby-5.3.1 spec/javascripts/support/rosie.js
quby-5.3.0 spec/javascripts/support/rosie.js
quby-5.2.0 spec/javascripts/support/rosie.js
quby-5.1.3 spec/javascripts/support/rosie.js
quby-5.1.2 spec/javascripts/support/rosie.js
quby-5.1.1 spec/javascripts/support/rosie.js
quby-5.1.0 spec/javascripts/support/rosie.js
quby-5.0.5 spec/javascripts/support/rosie.js
quby-5.0.4 spec/javascripts/support/rosie.js
quby-5.0.3 spec/javascripts/support/rosie.js
quby-5.0.2 spec/javascripts/support/rosie.js
quby-5.0.1 spec/javascripts/support/rosie.js