diegodfsd
8/26/2014 - 4:44 AM

factory

factory

// var FactoryGirl = require('factory_girl');

// FactoryGirl.define('user', function() {
//     this.id = Math.random()*101|0;
//     this.title = 'That\'s awesome day';
//     this.emotion = 'happy';
//     this.hasMany('places', 'place');
// })

// FactoryGirl.define('place', function() {
//     this.id = 2;
//     this.label = 'New York';    
// })

// var sqladapter = require("./sqlserver-adapter")(['user', 'place']);

// var user = FactoryGirl.create('user');

// console.log( user.toJson() );

var factory = require("./factory");

factory.define('content', function() {
		this.title = "title";
		this.description = "description";
		this.active = [true, false][Math.round(Math.random())];
	}
);

factory.define('class', function () {
		this.title = "title class";
		this.description = "description class";
		this.active = true;
		this.hasManyToMany('content', 'classcontent', 1);
	}
);

//var content = factory.build('content', 3);
var content = factory.build('class', 2, { title: 'new title' });
console.log( content );
"use strict";

(function() {

	var factory = {},
		definitions = [];

	var find = function(name) {		
		if( definitions.length === 0 ) return definitions;
		
		var result = definitions.filter(function(def){
			return def.name === name;
		});

		return result;
	};

	var merge = function(){
		var target = {},
			i = 0,
			limit = arguments.length;

		for (; i < limit; i++) {
			for (var key in arguments[i]) {
				if( arguments[i].hasOwnProperty(key) ) {
					target[key] = arguments[i][key];	
				}
			};
		};

		return target;
	};

	function Model() {
		this.manyToMany = [];
	};

	Model.prototype.hasManyToMany = function(name, table, qtd) {
		this.manyToMany.push({ name: name, table: table, qtd: qtd || 1 });
	};

	Model.prototype.getManyToMany = function() {
		return this.manyToMany;
	};

	factory.define = function(name, def) {
		var found = find(name);

		if( !found.length ) {
			def.prototype = new Model();
			definitions.push( { 'name': name, 'def': def } );
		}
	};

	factory.build = function(name) {
		var args = Array.prototype.slice.apply(arguments),
			times = 1,
			attributes = {},
			instances = [],
			definition = find(name);

		if( !definition.length ) throw "cannot be found the definition";

		if( typeof(args[1]) === "number" )
			times = args[1];

		if( typeof(args[args.length-1]) === "object" )
			attributes = args[args.length-1];

		while( times > 0 ) {
			times--;

			var obj = definition[0]['def'];
			
			var instance = new obj();
			//instance = merge(instance, attributes);
			
			var children = instance.getManyToMany();
			for (var i = 0; i < children.length; i++) {
				var child = children[i],
					items = this.build( child.name, child.qtd );

				instance['relationship'] = instance['relationship'] || [];	
				instance['relationship'].push( { 'table': child.table, items: items } );
			};

			instances.push( instance );
		};

		return instances;
	};

	module.exports = factory;
	
})();