injector-js
Advanced tools
Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "InjectorJS", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"ignore": [ | ||
@@ -11,4 +11,4 @@ "node_modules", | ||
"dependencies": { | ||
"ClazzJS": "~0.3.0" | ||
"ClazzJS": "~0.3.1" | ||
} | ||
} |
@@ -45,17 +45,19 @@ ; | ||
clazz('Injector', function() { | ||
clazz('Injector', function(self) { | ||
return { | ||
properties: { | ||
defaultType: { | ||
type: ['string'], | ||
defaultFactory: { | ||
type: ['object', { | ||
instanceOf: 'Factories/Abstract' | ||
}], | ||
constraints: { | ||
typeFactoryExists: function(type) { | ||
return this.hasTypeFactory(type); | ||
exists: function(factory) { | ||
return this.hasFactory(factory.getName()); | ||
} | ||
} | ||
}, | ||
typeFactory: { | ||
factory: { | ||
type: ['hash', { | ||
element: ['object', { | ||
instanceof: 'TypeFactories/Abstract' | ||
instanceOf: 'Factories/Abstract' | ||
}] | ||
@@ -65,9 +67,12 @@ }], | ||
}, | ||
createdObject: { | ||
object: { | ||
type: ['hash'], | ||
default: {} | ||
}, | ||
objectFactory: { | ||
objectCreator: { | ||
type: ['hash'], | ||
default: {} | ||
}, | ||
getter: { | ||
type: ['function'] | ||
} | ||
@@ -77,28 +82,26 @@ }, | ||
has: function(name) { | ||
return this.hasCreatedObject(name) || this.hasObjectFactory(name); | ||
return this.hasObject(name) || this.hasObjectCreator(name); | ||
}, | ||
get: function(name) { | ||
if (!this.hasCreatedObject(name)) { | ||
if (!this.hasObjectFactory(name)) { | ||
throw new Error('Factory for object "' + name + "' does not exists!'"); | ||
if (!this.hasObject(name)) { | ||
if (!this.hasObjectCreator(name)) { | ||
throw new Error('Factory method for object "' + name + "' does not exists!'"); | ||
} | ||
this.setCreatedObject(name, this.getObjectFactory(name).call()); | ||
this.removeObjectFactory(name); | ||
this.setObject(name, this.getObjectCreator(name).call()); | ||
this.removeObjectCreator(name); | ||
} | ||
return this.getCreatedObject(name); | ||
return this.getObject(name); | ||
}, | ||
set: function(name, type, params) { | ||
if (_.isObject(name)) { | ||
var objects = name; | ||
for (name in objects) { | ||
set: function( /* (name, type, factory) | (types) */ ) { | ||
if (_.isObject(arguments[0])) { | ||
var factories = arguments[0]; | ||
params = objects[name]; | ||
type = params.type; | ||
delete params.type; | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
for (var factory in factories) { | ||
for (var name in factories[factory]) { | ||
this.setObjectCreator(name, this.createObjectCreator(factory, factories[factory][name])); | ||
} | ||
} | ||
} else { | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
this.setObjectCreator(arguments[0], this.createObjectCreator(arguments[1], arguments[2])); | ||
} | ||
@@ -108,17 +111,30 @@ return this; | ||
createFactoryMethod: function(type, params) { | ||
getGetterMethod: function() { | ||
if (!this.hasGetter()) { | ||
var that = this; | ||
if (_.isUndefined(params)) { | ||
params = type; | ||
type = undefined; | ||
this.setGetter(function(name) { | ||
return that.get(name); | ||
}) | ||
} | ||
if (_.isUndefined(type)) { | ||
if (!this.hasDefaultType()) { | ||
throw new Error('You must specify type for object "' + name + '"!'); | ||
} | ||
type = this.getDefaultType(); | ||
return this.getGetter(); | ||
}, | ||
createObjectCreator: function(factoryName, factoryMethod) { | ||
if (_.isUndefined(factoryName)) { | ||
factoryMethod = factoryName; | ||
factoryName = undefined; | ||
} | ||
return this.getTypeFactory(type).getFactoryMethod(params); | ||
var that = this; | ||
var factory = !_.isUndefined(factoryName) ? this.getFactory(factoryName) : this.getDefaultFactory(); | ||
return function() { | ||
var params = _.isFunction(factoryMethod) ? factoryMethod.call(factory, that.getGetterMethod()) : factoryMethod; | ||
return factory.create(params); | ||
} | ||
} | ||
@@ -128,3 +144,3 @@ } | ||
}); | ||
clazz('ParameterProcessor', function() { | ||
clazz('ParameterProcessor', function(self) { | ||
return { | ||
@@ -139,18 +155,24 @@ constants: { | ||
methods: { | ||
process: function(value, meta, name, object) { | ||
for (var option in meta) { | ||
process: function(value, meta, name) { | ||
switch (option) { | ||
var options = ['converters', 'constraints', 'default', 'type']; | ||
for (var i = 0, ii = options.length; i < ii; ++i) { | ||
if (!(options[i] in meta)) { | ||
continue; | ||
} | ||
switch (options[i]) { | ||
case 'type': | ||
case 'constraints': | ||
case 'converters': | ||
value = this.const('PROCESSORS')(option)().apply(value, meta[option], name, object); | ||
value = this.const('PROCESSORS')(options[i])().apply(value, meta[options[i]], name); | ||
break; | ||
case 'default': | ||
var defaultValue = meta[option]; | ||
var defaultValue = meta[options[i]]; | ||
if (_.isFunction(defaultValue)) { | ||
defaultValue = defaultValue.call(object); | ||
defaultValue = defaultValue.call(); | ||
} | ||
@@ -168,17 +190,14 @@ if (_.isUndefined(value) || _.isNull(value)) { | ||
}); | ||
namespace('TypeFactories', function(clazz) { | ||
clazz('Abstract', function(parameterProcessor) { | ||
namespace('Factories', function(clazz) { | ||
clazz('Abstract', function(self, parameterProcessor) { | ||
return { | ||
methods: { | ||
getName: function() { | ||
this.__abstract() | ||
throw new Error('You must specify type name in child clazz!'); | ||
}, | ||
getFactoryMethod: function(params) { | ||
var self = this; | ||
return function() { | ||
return self.createObject(self.processParams(params)); | ||
} | ||
create: function(params) { | ||
return this.createObject(this.processParams(params)); | ||
}, | ||
createObject: function() { | ||
this.__abstract(); | ||
createObject: function(params) { | ||
throw new Error('You must realize "createObject" method in child clazz!'); | ||
}, | ||
@@ -196,3 +215,3 @@ getParamsDefinitions: function() { | ||
} | ||
params[param] = parameterProcessor.process(params[param], paramsDefinition[param], param, this); | ||
params[param] = parameterProcessor.process(params[param], paramsDefinition[param], param); | ||
} | ||
@@ -205,3 +224,3 @@ | ||
}); | ||
clazz('Clazz', 'Abstract', function(clazz, injector) { | ||
clazz('Clazz', 'Abstract', function(slef, clazz) { | ||
return { | ||
@@ -219,22 +238,7 @@ methods: { | ||
parent: { | ||
converters: { | ||
stringOrClazz: function(value) { | ||
if (_.isString(value)) { | ||
value = injector.get(value); | ||
} | ||
return value; | ||
} | ||
} | ||
type: ['function'] | ||
}, | ||
deps: { | ||
type: ['array'], | ||
default: [], | ||
converters: { | ||
resolve: function(value) { | ||
if (_.isString(value)) { | ||
value = injector.get(value); | ||
} | ||
return value; | ||
} | ||
} | ||
default: [] | ||
} | ||
@@ -249,3 +253,3 @@ } | ||
}); | ||
clazz('Parameter', 'Abstract', function() { | ||
clazz('Parameter', 'Abstract', function(self) { | ||
return { | ||
@@ -256,10 +260,4 @@ methods: { | ||
}, | ||
processParams: function(value) { | ||
if (_.isFunction(value)) { | ||
value = value(); | ||
} | ||
create: function(value) { | ||
return value; | ||
}, | ||
createObject: function(value) { | ||
return value; | ||
} | ||
@@ -269,3 +267,3 @@ } | ||
}); | ||
clazz('Service', 'Abstract', function(injector) { | ||
clazz('Service', 'Abstract', function(self) { | ||
return { | ||
@@ -280,26 +278,7 @@ methods: { | ||
type: ['function'], | ||
required: true, | ||
converters: { | ||
resolve: function(klass) { | ||
if (_.isString(klass)) { | ||
klass = injector.get(klass); | ||
} | ||
return klass; | ||
} | ||
} | ||
required: true | ||
}, | ||
init: { | ||
type: ['array'], | ||
default: [], | ||
converters: { | ||
resolve: function(initParams) { | ||
initParams = [].concat(initParams); | ||
for (var i = 0, ii = initParams.length; i < ii; ++i) { | ||
if (_.isString(initParams[i])) { | ||
initParams[i] = injector.get(initParams[i]); | ||
} | ||
} | ||
return initParams; | ||
} | ||
} | ||
default: [] | ||
}, | ||
@@ -312,18 +291,3 @@ call: { | ||
}], | ||
default: {}, | ||
converters: { | ||
resolve: function(methods) { | ||
for (var method in methods) { | ||
methods[method] = [].concat(methods[method]); | ||
for (var i = 0, ii = methods[method].length; i < ii; ++i) { | ||
if (_.isString(methods[method][i])) { | ||
methods[method][i] = injector.get(methods[method][i]); | ||
} | ||
} | ||
} | ||
return methods; | ||
} | ||
} | ||
default: {} | ||
} | ||
@@ -364,24 +328,24 @@ } | ||
var AbstractTypeFactory = clazz('/InjectorJS/TypeFactories/Abstract', [parameterProcessor]); | ||
var AbstractFactory = clazz('/InjectorJS/Factories/Abstract', [parameterProcessor]); | ||
var ParameterTypeFactory = clazz('/InjectorJS/TypeFactories/Parameter', AbstractTypeFactory); | ||
var ClazzTypeFactory = clazz('/InjectorJS/TypeFactories/Clazz', AbstractTypeFactory, [clazz, injector]); | ||
var ServiceTypeFactory = clazz('/InjectorJS/TypeFactories/Service', AbstractTypeFactory, [injector]); | ||
var ParameterFactory = clazz('/InjectorJS/Factories/Parameter', AbstractFactory); | ||
var ClazzFactory = clazz('/InjectorJS/Factories/Clazz', AbstractFactory, [clazz]); | ||
var ServiceFactory = clazz('/InjectorJS/Factories/Service', AbstractFactory); | ||
var parameterTypeFactory = ParameterTypeFactory.create(); | ||
var clazzTypeFactory = ClazzTypeFactory.create(); | ||
var serviceTypeFactory = ServiceTypeFactory.create(); | ||
var parameterFactory = ParameterFactory.create(); | ||
var clazzFactory = ClazzFactory.create(); | ||
var serviceFactory = ServiceFactory.create(); | ||
injector | ||
.setTypeFactory(parameterTypeFactory.getName(), parameterTypeFactory) | ||
.setTypeFactory(clazzTypeFactory.getName(), clazzTypeFactory) | ||
.setTypeFactory(serviceTypeFactory.getName(), serviceTypeFactory) | ||
.setDefaultType(parameterTypeFactory.getName()); | ||
.setFactory(parameterFactory.getName(), parameterFactory) | ||
.setFactory(clazzFactory.getName(), clazzFactory) | ||
.setFactory(serviceFactory.getName(), serviceFactory) | ||
.setDefaultFactory(parameterFactory); | ||
return { | ||
TypeFactory: { | ||
Abstract: AbstractTypeFactory, | ||
Parameter: ParameterTypeFactory, | ||
Clazz: ClazzTypeFactory, | ||
Service: ServiceTypeFactory | ||
Factory: { | ||
Abstract: AbstractFactory, | ||
Parameter: ParameterFactory, | ||
Clazz: ClazzFactory, | ||
Service: ServiceFactory | ||
}, | ||
@@ -388,0 +352,0 @@ Injector: Injector, |
@@ -1,2 +0,2 @@ | ||
!function(a,b,c,d){if("function"==typeof define&&define.amd)define(b,c,d);else if("object"==typeof exports&&exports){for(var e=0,f=c.length;f>e;++e){var g=c[e];"string"==typeof g&&(g=g.replace(/([A-Z]+)/g,function(a){return"-"+a.toLowerCase()}).replace(/^-/,""),c[e]=require(g))}var h=d.apply(a,c);for(var i in h)exports[i]=h[i]}else{for(var e=0,f=c.length;f>e;++e){var g=c[e];if("string"==typeof g){if(!(g in a))throw new Error('"'+b+'" dependent on non exited module "'+g+'"!');c[e]=a[g]}}a[b]=d.apply(a,c)}}(new Function("return this")(),"InjectorJS",["ClazzJS"],function(a,b){var c=a.namespace,d=a.clazz,e=a.meta;c("InjectorJS",function(a,c){a("Injector",function(){return{properties:{defaultType:{type:["string"],constraints:{typeFactoryExists:function(a){return this.hasTypeFactory(a)}}},typeFactory:{type:["hash",{element:["object",{"instanceof":"TypeFactories/Abstract"}]}],"default":{}},createdObject:{type:["hash"],"default":{}},objectFactory:{type:["hash"],"default":{}}},methods:{has:function(a){return this.hasCreatedObject(a)||this.hasObjectFactory(a)},get:function(a){if(!this.hasCreatedObject(a)){if(!this.hasObjectFactory(a))throw new Error('Factory for object "'+a+"' does not exists!'");this.setCreatedObject(a,this.getObjectFactory(a).call()),this.removeObjectFactory(a)}return this.getCreatedObject(a)},set:function(a,b,c){if(_.isObject(a)){var d=a;for(a in d)c=d[a],b=c.type,delete c.type,this.setObjectFactory(a,this.createFactoryMethod(b,c))}else this.setObjectFactory(a,this.createFactoryMethod(b,c));return this},createFactoryMethod:function(a,c){if(_.isUndefined(c)&&(c=a,a=b),_.isUndefined(a)){if(!this.hasDefaultType())throw new Error('You must specify type for object "'+name+'"!');a=this.getDefaultType()}return this.getTypeFactory(a).getFactoryMethod(c)}}}}),a("ParameterProcessor",function(){return{constants:{PROCESSORS:{type:e("/ClazzJS/Property/Type"),constraints:e("/ClazzJS/Property/Constraints"),converters:e("/ClazzJS/Property/Converters")}},methods:{process:function(a,b,c,d){for(var e in b)switch(e){case"type":case"constraints":case"converters":a=this.const("PROCESSORS")(e)().apply(a,b[e],c,d);break;case"default":var f=b[e];_.isFunction(f)&&(f=f.call(d)),(_.isUndefined(a)||_.isNull(a))&&(a=f)}return a}}}}),c("TypeFactories",function(a){a("Abstract",function(a){return{methods:{getName:function(){this.__abstract()},getFactoryMethod:function(a){var b=this;return function(){return b.createObject(b.processParams(a))}},createObject:function(){this.__abstract()},getParamsDefinitions:function(){return{}},processParams:function(b){var c=this.getParamsDefinitions();for(var d in b){if(!(d in c))throw new Error('Parameter "'+d+'" does not defined!');b[d]=a.process(b[d],c[d],d,this)}return b}}}}),a("Clazz","Abstract",function(a,b){return{methods:{getName:function(){return"clazz"},getParamsDefinitions:function(){return{name:{type:["string"],required:!0},parent:{converters:{stringOrClazz:function(a){return _.isString(a)&&(a=b.get(a)),a}}},deps:{type:["array"],"default":[],converters:{resolve:function(a){return _.isString(a)&&(a=b.get(a)),a}}}}},createObject:function(b){return a(b.name,b.parent,b.deps)}}}}),a("Parameter","Abstract",function(){return{methods:{getName:function(){return"parameter"},processParams:function(a){return _.isFunction(a)&&(a=a()),a},createObject:function(a){return a}}}}),a("Service","Abstract",function(a){return{methods:{getName:function(){return"service"},getParamsDefinitions:function(){return{"class":{type:["function"],required:!0,converters:{resolve:function(b){return _.isString(b)&&(b=a.get(b)),b}}},init:{type:["array"],"default":[],converters:{resolve:function(b){b=[].concat(b);for(var c=0,d=b.length;d>c;++c)_.isString(b[c])&&(b[c]=a.get(b[c]));return b}}},call:{type:["hash",{element:{type:"array"}}],"default":{},converters:{resolve:function(b){for(var c in b){b[c]=[].concat(b[c]);for(var d=0,e=b[c].length;e>d;++d)_.isString(b[c][d])&&(b[c][d]=a.get(b[c][d]))}return b}}}}},createObject:function(a){function b(a,b){var c=function(){return a.apply(this,b)};return c.prototype=a.prototype,new c}var c=b(a.class,a.init);for(var d in a.call)c[c].apply(c,a.call[d]);return c}}}})})});var f=d("/InjectorJS/Injector"),g=d("/InjectorJS/ParameterProcessor"),h=f.create(),i=g.create(),j=d("/InjectorJS/TypeFactories/Abstract",[i]),k=d("/InjectorJS/TypeFactories/Parameter",j),l=d("/InjectorJS/TypeFactories/Clazz",j,[d,h]),m=d("/InjectorJS/TypeFactories/Service",j,[h]),n=k.create(),o=l.create(),p=m.create();return h.setTypeFactory(n.getName(),n).setTypeFactory(o.getName(),o).setTypeFactory(p.getName(),p).setDefaultType(n.getName()),{TypeFactory:{Abstract:j,Parameter:k,Clazz:l,Service:m},Injector:f,ParameterProcessor:g,injector:h,parameterProcessor:i}}); | ||
!function(a,b,c,d){if("function"==typeof define&&define.amd)define(b,c,d);else if("object"==typeof exports&&exports){for(var e=0,f=c.length;f>e;++e){var g=c[e];"string"==typeof g&&(g=g.replace(/([A-Z]+)/g,function(a){return"-"+a.toLowerCase()}).replace(/^-/,""),c[e]=require(g))}var h=d.apply(a,c);for(var i in h)exports[i]=h[i]}else{for(var e=0,f=c.length;f>e;++e){var g=c[e];if("string"==typeof g){if(!(g in a))throw new Error('"'+b+'" dependent on non exited module "'+g+'"!');c[e]=a[g]}}a[b]=d.apply(a,c)}}(new Function("return this")(),"InjectorJS",["ClazzJS"],function(a,b){var c=a.namespace,d=a.clazz,e=a.meta;c("InjectorJS",function(a,c){a("Injector",function(a){return{properties:{defaultFactory:{type:["object",{instanceOf:"Factories/Abstract"}],constraints:{exists:function(a){return this.hasFactory(a.getName())}}},factory:{type:["hash",{element:["object",{instanceOf:"Factories/Abstract"}]}],"default":{}},object:{type:["hash"],"default":{}},objectCreator:{type:["hash"],"default":{}},getter:{type:["function"]}},methods:{has:function(a){return this.hasObject(a)||this.hasObjectCreator(a)},get:function(a){if(!this.hasObject(a)){if(!this.hasObjectCreator(a))throw new Error('Factory method for object "'+a+"' does not exists!'");this.setObject(a,this.getObjectCreator(a).call()),this.removeObjectCreator(a)}return this.getObject(a)},set:function(){if(_.isObject(arguments[0])){var a=arguments[0];for(var b in a)for(var c in a[b])this.setObjectCreator(c,this.createObjectCreator(b,a[b][c]))}else this.setObjectCreator(arguments[0],this.createObjectCreator(arguments[1],arguments[2]));return this},getGetterMethod:function(){if(!this.hasGetter()){var a=this;this.setGetter(function(b){return a.get(b)})}return this.getGetter()},createObjectCreator:function(a,c){_.isUndefined(a)&&(c=a,a=b);var d=this,e=_.isUndefined(a)?this.getDefaultFactory():this.getFactory(a);return function(){var a=_.isFunction(c)?c.call(e,d.getGetterMethod()):c;return e.create(a)}}}}}),a("ParameterProcessor",function(a){return{constants:{PROCESSORS:{type:e("/ClazzJS/Property/Type"),constraints:e("/ClazzJS/Property/Constraints"),converters:e("/ClazzJS/Property/Converters")}},methods:{process:function(a,b,c){for(var d=["converters","constraints","default","type"],e=0,f=d.length;f>e;++e)if(d[e]in b)switch(d[e]){case"type":case"constraints":case"converters":a=this.const("PROCESSORS")(d[e])().apply(a,b[d[e]],c);break;case"default":var g=b[d[e]];_.isFunction(g)&&(g=g.call()),(_.isUndefined(a)||_.isNull(a))&&(a=g)}return a}}}}),c("Factories",function(a){a("Abstract",function(a,b){return{methods:{getName:function(){throw new Error("You must specify type name in child clazz!")},create:function(a){return this.createObject(this.processParams(a))},createObject:function(a){throw new Error('You must realize "createObject" method in child clazz!')},getParamsDefinitions:function(){return{}},processParams:function(a){var c=this.getParamsDefinitions();for(var d in a){if(!(d in c))throw new Error('Parameter "'+d+'" does not defined!');a[d]=b.process(a[d],c[d],d)}return a}}}}),a("Clazz","Abstract",function(a,b){return{methods:{getName:function(){return"clazz"},getParamsDefinitions:function(){return{name:{type:["string"],required:!0},parent:{type:["function"]},deps:{type:["array"],"default":[]}}},createObject:function(a){return b(a.name,a.parent,a.deps)}}}}),a("Parameter","Abstract",function(a){return{methods:{getName:function(){return"parameter"},create:function(a){return a}}}}),a("Service","Abstract",function(a){return{methods:{getName:function(){return"service"},getParamsDefinitions:function(){return{"class":{type:["function"],required:!0},init:{type:["array"],"default":[]},call:{type:["hash",{element:{type:"array"}}],"default":{}}}},createObject:function(a){function b(a,b){var c=function(){return a.apply(this,b)};return c.prototype=a.prototype,new c}var c=b(a.class,a.init);for(var d in a.call)c[c].apply(c,a.call[d]);return c}}}})})});var f=d("/InjectorJS/Injector"),g=d("/InjectorJS/ParameterProcessor"),h=f.create(),i=g.create(),j=d("/InjectorJS/Factories/Abstract",[i]),k=d("/InjectorJS/Factories/Parameter",j),l=d("/InjectorJS/Factories/Clazz",j,[d]),m=d("/InjectorJS/Factories/Service",j),n=k.create(),o=l.create(),p=m.create();return h.setFactory(n.getName(),n).setFactory(o.getName(),o).setFactory(p.getName(),p).setDefaultFactory(n),{Factory:{Abstract:j,Parameter:k,Clazz:l,Service:m},Injector:f,ParameterProcessor:g,injector:h,parameterProcessor:i}}); | ||
//# sourceMappingURL=dist/InjectorJS.min.map |
@@ -17,5 +17,5 @@ "use strict"; | ||
"src/*.js", | ||
"src/TypeFactories/.prefix", | ||
"src/TypeFactories/*.js", | ||
"src/TypeFactories/.suffix", | ||
"src/Factories/.prefix", | ||
"src/Factories/*.js", | ||
"src/Factories/.suffix", | ||
"src/.build", | ||
@@ -22,0 +22,0 @@ "src/.suffix" |
{ | ||
"name": "injector-js", | ||
"title": "InjectorJS", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "IoC container for JavaScript", | ||
@@ -28,3 +28,3 @@ "author": { | ||
"dependencies": { | ||
"clazz-js": "~0.3.0" | ||
"clazz-js": "~0.3.1" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
@@ -5,1 +5,5 @@ InjectorJS | ||
Dependency Injection for JavaScript | ||
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/alexpods/injectorjs/trend.png)](https://bitdeli.com/free "Bitdeli Badge") | ||
@@ -1,23 +0,26 @@ | ||
clazz('Injector', function() { | ||
clazz('Injector', function(self) { | ||
return { | ||
properties: { | ||
defaultType: { | ||
type: ['string'], | ||
defaultFactory: { | ||
type: ['object', { instanceOf: 'Factories/Abstract' }], | ||
constraints: { | ||
typeFactoryExists: function(type) { | ||
return this.hasTypeFactory(type); | ||
exists: function(factory) { | ||
return this.hasFactory(factory.getName()); | ||
} | ||
} | ||
}, | ||
typeFactory: { | ||
type: ['hash', { element: ['object', { instanceof: 'TypeFactories/Abstract' }] }], | ||
factory: { | ||
type: ['hash', { element: ['object', { instanceOf: 'Factories/Abstract' }] }], | ||
default: {} | ||
}, | ||
createdObject: { | ||
object: { | ||
type: ['hash'], | ||
default: {} | ||
}, | ||
objectFactory: { | ||
objectCreator: { | ||
type: ['hash'], | ||
default: {} | ||
}, | ||
getter: { | ||
type: ['function'] | ||
} | ||
@@ -27,29 +30,27 @@ }, | ||
has: function(name) { | ||
return this.hasCreatedObject(name) || this.hasObjectFactory(name); | ||
return this.hasObject(name) || this.hasObjectCreator(name); | ||
}, | ||
get: function(name) { | ||
if (!this.hasCreatedObject(name)) { | ||
if (!this.hasObjectFactory(name)) { | ||
throw new Error('Factory for object "' + name + "' does not exists!'"); | ||
if (!this.hasObject(name)) { | ||
if (!this.hasObjectCreator(name)) { | ||
throw new Error('Factory method for object "' + name + "' does not exists!'"); | ||
} | ||
this.setCreatedObject(name, this.getObjectFactory(name).call()); | ||
this.removeObjectFactory(name); | ||
this.setObject(name, this.getObjectCreator(name).call()); | ||
this.removeObjectCreator(name); | ||
} | ||
return this.getCreatedObject(name); | ||
return this.getObject(name); | ||
}, | ||
set: function(name, type, params) { | ||
if (_.isObject(name)) { | ||
var objects = name; | ||
for (name in objects) { | ||
set: function(/* (name, type, factory) | (types) */) { | ||
if (_.isObject(arguments[0])) { | ||
var factories = arguments[0]; | ||
params = objects[name]; | ||
type = params.type; | ||
delete params.type; | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
for (var factory in factories) { | ||
for (var name in factories[factory]) { | ||
this.setObjectCreator(name, this.createObjectCreator(factory, factories[factory][name])); | ||
} | ||
} | ||
} | ||
else { | ||
this.setObjectFactory(name, this.createFactoryMethod(type, params)); | ||
this.setObjectCreator(arguments[0], this.createObjectCreator(arguments[1], arguments[2])); | ||
} | ||
@@ -59,17 +60,32 @@ return this; | ||
createFactoryMethod: function(type, params) { | ||
getGetterMethod: function() { | ||
if (!this.hasGetter()) { | ||
var that = this; | ||
if (_.isUndefined(params)) { | ||
params = type; | ||
type = undefined; | ||
this.setGetter(function(name) { | ||
return that.get(name); | ||
}) | ||
} | ||
if (_.isUndefined(type)) { | ||
if (!this.hasDefaultType()) { | ||
throw new Error('You must specify type for object "' + name + '"!'); | ||
} | ||
type = this.getDefaultType(); | ||
return this.getGetter(); | ||
}, | ||
createObjectCreator: function(factoryName, factoryMethod) { | ||
if (_.isUndefined(factoryName)) { | ||
factoryMethod = factoryName; | ||
factoryName = undefined; | ||
} | ||
return this.getTypeFactory(type).getFactoryMethod(params); | ||
var that = this; | ||
var factory = !_.isUndefined(factoryName) ? this.getFactory(factoryName) : this.getDefaultFactory(); | ||
return function() { | ||
var params = _.isFunction(factoryMethod) | ||
? factoryMethod.call(factory, that.getGetterMethod()) | ||
: factoryMethod; | ||
return factory.create(params); | ||
} | ||
} | ||
@@ -76,0 +92,0 @@ } |
@@ -1,2 +0,2 @@ | ||
clazz('ParameterProcessor', function() { | ||
clazz('ParameterProcessor', function(self) { | ||
return { | ||
@@ -11,18 +11,24 @@ constants: { | ||
methods: { | ||
process: function(value, meta, name, object) { | ||
for (var option in meta) { | ||
process: function(value, meta, name) { | ||
switch (option) { | ||
var options = ['converters', 'constraints', 'default', 'type']; | ||
for (var i = 0, ii = options.length; i < ii; ++i) { | ||
if (!(options[i] in meta)) { | ||
continue; | ||
} | ||
switch (options[i]) { | ||
case 'type': | ||
case 'constraints': | ||
case 'converters': | ||
value = this.const('PROCESSORS')(option)().apply(value, meta[option], name, object); | ||
value = this.const('PROCESSORS')(options[i])().apply(value, meta[options[i]], name); | ||
break; | ||
case 'default': | ||
var defaultValue = meta[option]; | ||
var defaultValue = meta[options[i]]; | ||
if (_.isFunction(defaultValue)) { | ||
defaultValue = defaultValue.call(object); | ||
defaultValue = defaultValue.call(); | ||
} | ||
@@ -29,0 +35,0 @@ if (_.isUndefined(value) || _.isNull(value)) { |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9
2
32905
596
Updatedclazz-js@~0.3.1