Skip to content
Browse files

Bug if you inject a method twice. Now throws an error unless you forc…

…eInject it.
  • Loading branch information...
1 parent 44ec79e commit 0d013e45bca0a6c65f211124a4161f25c58ec3a4 @easternbloc committed Feb 9, 2012
Showing with 9 additions and 10 deletions.
  1. +9 −10 lib/syringe.js
View
19 lib/syringe.js
@@ -3,29 +3,28 @@ var modules = {};
function Syringe(moduleName) {
var moduleCache = {},
module = require(moduleName);
-
- modules[moduleName] = module;
this.inject = function(methodName, method) {
- if(module[methodName]) {
- this.forceInject(methodName, method);
+ if(module[methodName] && !moduleCache[methodName]) {
+ moduleCache[methodName] = module[methodName];
+ this.forceInject(methodName, method);
return this;
+ } else if (moduleCache[methodName]) {
+ throw new Error(methodName + ' has already been injected from this module. Please retore it first.');
} else {
throw new Error(methodName + ' does not exist in the module.');
}
};
this.forceInject = function(methodName, method) {
- if(module[methodName]) {
- moduleCache[methodName] = module[methodName];
- }
module[methodName] = method;
return this;
};
this.restore = function(methodName) {
if(moduleCache[methodName]) {
module[methodName] = moduleCache[methodName];
+ delete moduleCache[methodName];
return this;
} else {
throw new Error(methodName + ' does not exist in the module so cannot be restored.');
@@ -68,8 +67,8 @@ function Syringe(moduleName) {
module.exports = function(moduleName) {
if (!modules[moduleName]) {
- return new Syringe(moduleName);
- } else {
- return modules[moduleName];
+ modules[moduleName] = new Syringe(moduleName);
}
+
+ return modules[moduleName];
};

0 comments on commit 0d013e4

Please sign in to comment.
Something went wrong with that request. Please try again.