factory-js
Version:
Building JavaScript objects inspired by rosie and factory_girl
2 lines • 4.97 kB
JavaScript
/*! factory-js 1.3.1 */
var Factory,__hasProp={}.hasOwnProperty,__extends=function(a,b){function c(){this.constructor=a}for(var d in b)__hasProp.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},__slice=[].slice;Factory=function(){function a(){}var b;return a.Adapter=b=function(){function a(a){this.factory=a}return a.prototype.build=function(a,b){return b},a.prototype.create=function(a,b){return b},a.prototype.push=function(a,b){return this[a].push(b)},a}(),a.factories={},a.adapter=a.Adapter,a.clear=function(){return this.factories={},this.adapter=a.Adapter},a.reset=function(){var a,b,c,d;c=this.factories,d=[];for(b in c)a=c[b],d.push(a.sequences={});return d},a.define=function(a,b){var c,d;if(c=new FactoryDefinition(a),"function"==typeof b)b.call(c);else for(d in b)c.attr(d,b[d]);return this.factories[a]=c},a.getFactory=function(a){var b;return b=this.factories[a],null!=b?b:{factory:function(){throw new Error('undefined factory "'+a+'"')}()}},a.getTrait=function(a,b){var c;return c=a.traits[b],null!=c?c:{trait:function(){throw new Error('undefined trait "'+b+'" for factory "'+this.name+'"')}.call(this)}},a.abstractBuild=function(a,b,c){var d,e,f,g,h;return b=b.split(/\s+/),f=b[0],e=this.getFactory(f),h=b.slice(1,+b.length+1||9e9).map(function(a){return function(b){return a.getTrait(e,b)}}(this)),d=e.attributes(c,h),g=e.build(a,f,d.withoutIgnored),h.unshift(e),h.map(function(a){return a.applyCallbacks(g,d.withIgnored)}),g},a.abstractBuildList=function(a,b,c,d){var e;return"number"==typeof c?function(){e=[];for(var a=0;0<=c?a<c:a>c;0<=c?a++:a--)e.push(a);return e}.apply(this).map(function(c){return function(){return c.abstractBuild(a,b,d)}}(this)):c instanceof Array?c.map(function(c){return function(e){return"string"==typeof e?c.abstractBuild(a,""+b+" "+e,d):e.constructor===Object?c.abstractBuild(a,b,c.hash.merge({},d,e)):e}}(this)):void 0},a.attributes=function(a,b){return this.abstractBuild("attributes",a,b)},a.build=function(a,b){return this.abstractBuild("build",a,b)},a.create=function(a,b){return this.abstractBuild("create",a,b)},a.buildList=function(a,b,c){return this.abstractBuildList("build",a,b,c)},a.createList=function(a,b,c){return this.abstractBuildList("create",a,b,c)},a.setupForEmber=function(b){if(null==b)throw new Error('undefined "'+b+'"');return a.EmberDataAdapter=function(a){function c(){return c.__super__.constructor.apply(this,arguments)}return __extends(c,a),c.prototype.build=function(a,c){return Ember.run(function(){return b.__container__.lookup("store:main").createRecord(a,c)})},c.prototype.create=function(a,b){return this.build(a,b)},c.prototype.push=function(a,b){return Ember.run(function(c){return function(){return c.get(a).addObject(b)}}(this))},c}(a.Adapter),a.adapter=a.EmberDataAdapter},a.hash={merge:function(){var a,b,c,d,e,f,g;for(a=arguments[0],d=2<=arguments.length?__slice.call(arguments,1):[],f=0,g=d.length;f<g;f++){c=d[f];for(b in c)e=c[b],a[b]=e}return a},evaluate:function(a){var b,c;c=[];for(b in a)c.push(a[b]="function"==typeof a[b]?a[b]():a[b]);return c}},a}(),("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=Factory:window.Factory=Factory;var FactoryDefinition;FactoryDefinition=function(){function a(a){this.name=a,this.buildAdapter=new Factory.adapter(this),this.attrs={},this.ignores={},this.sequences={},this.traits={},this.callbacks=[]}return a.prototype.adapter=function(a){return this.buildAdapter=new a(this),this},a.prototype.build=function(a,b,c){return this.buildAdapter[a]?this.buildAdapter[a](b,c):c},a.prototype.after=function(a){return this.callbacks.push(a),this},a.prototype.attr=function(a,b){return this.attrs[a]="function"==typeof b?b:function(){return b},this},a.prototype.hasMany=function(a,b){return this.ignore(a,[]),this.after(function(c,d){return this[a]instanceof Array||(this[a]=[]),Factory.buildList(b,c[a]).forEach(function(b){return function(c){return d.buildAdapter.push.call(b,a,c)}}(this))}),this},a.prototype.ignore=function(a,b){return this.ignores[a]="function"==typeof b?b:function(){return b},this},a.prototype.sequence=function(a,b){var c;return c=this,b=b||function(a){return a},this.attrs[a]=function(){return c.sequences[a]=c.sequences[a]||0,b.call(this,++c.sequences[a])},this},a.prototype.trait=function(b,c){var d;return d=new a(b),"function"==typeof c&&c.call(d),this.traits[b]=d,this},a.prototype.attributes=function(a,b){var c,d,e;d=Factory.hash.merge({},a),e={},b.forEach(function(a){var b,c;c=[];for(b in a.attrs)c.push(d[b]=a.attrs[b]);return c});for(c in this.attrs)d.hasOwnProperty(c)||(d[c]=this.attrs[c]);for(c in this.ignores)e[c]=d.hasOwnProperty(c)?d[c]:this.ignores[c],delete d[c];return Factory.hash.evaluate(d),Factory.hash.evaluate(e),{withIgnored:Factory.hash.merge({},d,e),withoutIgnored:d}},a.prototype.applyCallbacks=function(a,b){return this.callbacks.forEach(function(c){return function(d){return d.call(a,b,c)}}(this))},a}();