Comparing version 0.5.1 to 0.5.2
91
index.js
/*-------------------------------------------------------------------------- | ||
Pang - A simple dependency injection lbrary for nodejs | ||
pang - A simple dependency injection library for nodejs | ||
The MIT License (MIT) | ||
@@ -23,5 +23,5 @@ Copyright (c) 2014 Haydn Paterson (sinclair) <haydn.developer@gmail.com> | ||
(function (pang) { | ||
var KernelDependency = (function () { | ||
function KernelDependency(kernel, name, names, initializer, initialized, instance) { | ||
this.kernel = kernel; | ||
var Dependency = (function () { | ||
function Dependency(domain, name, names, initializer, initialized, instance) { | ||
this.domain = domain; | ||
this.name = name; | ||
@@ -33,7 +33,7 @@ this.names = names; | ||
} | ||
KernelDependency.prototype.initialize = function () { | ||
Dependency.prototype.initialize = function () { | ||
var _this = this; | ||
if (!this.initialized) { | ||
var dependancies = this.names.map(function (name, index, list) { | ||
var dependency = _this.kernel.get(name); | ||
var dependency = _this.domain.dependency(name); | ||
@@ -54,13 +54,22 @@ if (dependency) { | ||
}; | ||
return KernelDependency; | ||
return Dependency; | ||
})(); | ||
pang.KernelDependency = KernelDependency; | ||
pang.Dependency = Dependency; | ||
var Kernel = (function () { | ||
function Kernel() { | ||
var Domain = (function () { | ||
function Domain() { | ||
this.dependencies = []; | ||
} | ||
Kernel.prototype.factory = function (name, names, initializer) { | ||
var dependency = new KernelDependency(this, name, names, initializer, false, null); | ||
/** | ||
* sets up a factory on this domain | ||
*/ | ||
Domain.prototype.factory = function (name, initializer) { | ||
if (!this.isfunction(initializer)) { | ||
throw Error('pang: initializer should be a function.'); | ||
} | ||
var names = this.extractargs(initializer); | ||
var dependency = new Dependency(this, name, names, initializer, false, null); | ||
this.dependencies.push(dependency); | ||
@@ -71,5 +80,21 @@ | ||
Kernel.prototype.get = function (name) { | ||
/** | ||
* returns the instance associated with this name | ||
*/ | ||
Domain.prototype.get = function (name) { | ||
for (var i = 0; i < this.dependencies.length; i++) { | ||
if (this.dependencies[i].name == name) { | ||
return this.dependencies[i].instance; | ||
} | ||
} | ||
return null; | ||
}; | ||
/** | ||
* returns the dependency type on this domain | ||
*/ | ||
Domain.prototype.dependency = function (name) { | ||
for (var i = 0; i < this.dependencies.length; i++) { | ||
if (this.dependencies[i].name == name) { | ||
return this.dependencies[i]; | ||
@@ -82,3 +107,6 @@ } | ||
Kernel.prototype.start = function () { | ||
/** | ||
* starts the domain | ||
*/ | ||
Domain.prototype.start = function () { | ||
for (var i = 0; i < this.dependencies.length; i++) { | ||
@@ -88,10 +116,35 @@ this.dependencies[i].initialize(); | ||
}; | ||
return Kernel; | ||
/** | ||
* tests this object to ensure its a function | ||
*/ | ||
Domain.prototype.isfunction = function (obj) { | ||
var getType = {}; | ||
return obj && getType.toString.call(obj) === '[object Function]'; | ||
}; | ||
/** | ||
* extracts the arguments from this function | ||
*/ | ||
Domain.prototype.extractargs = function (func) { | ||
var match = /\(([^)]+)/.exec(func.toString()); | ||
if (match[1]) { | ||
var arguments = match[1].split(/\s*,\s*/); | ||
} | ||
return arguments; | ||
}; | ||
return Domain; | ||
})(); | ||
pang.Kernel = Kernel; | ||
pang.Domain = Domain; | ||
function kernel() { | ||
return new Kernel(); | ||
/** | ||
* returns a new domain | ||
*/ | ||
function domain() { | ||
return new Domain(); | ||
} | ||
pang.kernel = kernel; | ||
pang.domain = domain; | ||
})(pang || (pang = {})); | ||
@@ -98,0 +151,0 @@ |
{ | ||
"name": "pang", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "A simple dependency injection library for node", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,5 +16,8 @@ # pang | ||
var kernel = pang.kernel() | ||
var domain = pang.domain() | ||
kernel.factory('configuration', [], function(){ | ||
//--------------------------------------- | ||
// no injection | ||
//--------------------------------------- | ||
domain.factory('configuration', function(){ | ||
@@ -24,14 +27,26 @@ return new Configuration() | ||
kernel.factory('repository', function(repository) { | ||
//--------------------------------------- | ||
// configuration injected on repository | ||
//--------------------------------------- | ||
domain.factory('repository', function(configuration) { | ||
return new Repository(repository) | ||
return new Repository(configuration) | ||
}) | ||
kernel.factory('server', ['configuration', 'repository'], function(configuration, repository) { | ||
//-------------------------------------------------- | ||
// configuration and repository injected on server | ||
//-------------------------------------------------- | ||
domain.factory('server', function(configuration, repository) { | ||
return new Server(configuration, repository) | ||
}) | ||
kernel.start() | ||
domain.start() | ||
//-------------------------------------------------- | ||
// get a instance from the domain | ||
//-------------------------------------------------- | ||
var server = domain.get('server') | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7902
124
51