-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassLoader.js
More file actions
74 lines (57 loc) · 2.06 KB
/
classLoader.js
File metadata and controls
74 lines (57 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var MultiFileLoader = require('./Utilities/multiFileLoader');
var innerLoader = function(path, baseClass) {
var suffix = 'Controller';
this.path = path;
this.cache = {};
this.baseClass = baseClass;
var cache = this.cache;
this.load = function(className) {
className = className.toLowerCase();
if (cache[className]) {
var instance = new cache[className]();
this.prepare(className, instance);
return instance;
}
return null;
};
this.prepare = function(className, instance) {
this.attachBaseMethods(instance);
this.addCaseInsensitivity(instance);
instance._name = className.toLowerCase();
};
this.attachBaseMethods = function(instance) {
Utils.extend(instance, baseClass);
};
this.addCaseInsensitivity = function(instance) {
for (var action in instance) {
var lower = action.toLowerCase();
instance[lower] = instance[lower] || instance[action];
}
};
var mfl = new MultiFileLoader(path, function(fileName, constructor) {
Logger.log('Loaded ' + suffix + ': ' + fileName);
var className = fileName.toLowerCase().replace(suffix.toLowerCase(), '');
cache[className] = constructor;
}, 'require');
mfl.load();
}
ClassLoader = module.exports = (function() {
return function ClassLoader(baseController) {
var self = this;
var ControllersPath = './Controllers/';
baseController.classLoader = this;
var controllerLoader = new innerLoader(ControllersPath, baseController, this);
var setServerVars = function(instance, request, response) {
instance.Request = request;
instance.Response = response;
};
this.getController = function(className, request, response) {
var instance = controllerLoader.load(className);
if (instance) {
setServerVars(instance, request, response);
}
return instance;
};
return this;
};
})();