Last active
December 30, 2021 18:03
-
-
Save Golodhros/597bca34394ea855bc9b to your computer and use it in GitHub Desktop.
JavaScript Object Builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @summary Generates a testObjectBuilder example object | |
* @requires underscore, requirejs/commonjs | |
*/ | |
var testObjectBuilder = function(require) { | |
'use strict'; | |
var _ = require('underscore'), | |
OrderModel = require('./order_model'); | |
/** | |
* @summary Generates Order Model test Objects | |
* @constructor | |
* @param {config} object | |
* @example | |
* var orderModel = new OrderModelBuilder() | |
* .withPaidTicket() | |
* .withSoldOutState() | |
* .build(); | |
* | |
*/ | |
function OrderModelBuilder(config) { | |
this.Klass = OrderModelBuilder; | |
this.config = _.defaults(config || {}, { | |
// Default properties | |
}); | |
// Categories | |
this.withPaidTicket = function() { | |
var attributes = _.extend({}, this.config, { | |
// Paid order attributes | |
}); | |
return new this.Klass(attributes); | |
}; | |
// States | |
this.withSoldOutState = function() { | |
var attributes = _.extend({}, this.config, { | |
// SoldOut order attributes | |
}); | |
return new this.Klass(attributes); | |
}; | |
this.build = function() { | |
return new OrderModel(this.config); | |
}; | |
} | |
return { | |
OrderModelBuilder: OrderModelBuilder | |
}; | |
}; | |
// Example of use along with a factory function: | |
function aModel() { | |
return new testObjectBuilder.OrderModelBuilder(); | |
} | |
// Within the beforeEach statement | |
this.model = aModel() | |
.withPaidTicket() | |
.withSoldOutState() | |
.build(); | |
// Here is a basic object builder template: | |
function NAMEBuilder(config) { | |
this.Klass = NAMEBuilder; | |
this.config = _.defaults(config || {}, { | |
// Default config | |
}); | |
this.withFEATURE = function() { | |
var attributes = _.extend({}, this.config, { | |
// Specific config | |
}); | |
return new this.Klass(attributes); | |
}; | |
this.build = function() { | |
return new NameOfObject(this.config); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment