-
Notifications
You must be signed in to change notification settings - Fork 135
/
namespace.js
96 lines (87 loc) · 2.38 KB
/
namespace.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Module dependencies.
*/
var utils = require('./utils');
/**
* `Namespace` constructor.
*
* @api private
*/
function Namespace(path, options, parent) {
options = options || {};
options.module = (options.module !== undefined) ? options.module : path;
options.helper = (options.helper !== undefined) ? options.helper : path;
this.path = path || '';
this.module = utils.moduleize(options.module) || '';
this.helper = utils.helperize(options.helper) || '';
this.parent = parent || null;
};
/**
* Fully qualified path.
*
* @param {String} controller
* @return {String}
* @api protected
*/
Namespace.prototype.qpath = function(path) {
var qual = path;
var ns = this;
while (ns) {
qual = (ns.path.length) ? ((qual[0] === '/') ? (ns.path + qual) : (ns.path + '/' + qual)) : (qual);
ns = ns.parent;
}
qual = (qual[0] === '/') ? (qual) : ('/' + qual);
qual = (qual.length > 1 && qual[qual.length - 1] === '/') ? (qual.slice(0, qual.length - 1)) : (qual);
return qual;
}
/**
* Fully qualified controller.
*
* Contructs a fully qualified name for `controller`, including any module
* segments corresponding to namespaces. For instance, `PostsControlller`
* within an "admin" namespace would have a fully qualified name of
* `Admin::PostsController`.
*
* @param {String} controller
* @return {String}
* @api protected
*/
Namespace.prototype.qcontroller = function(controller) {
var root = false;
if (controller[0] === '/') {
controller = controller.slice(1);
root = true;
}
var qual = utils.controllerize(controller);
var ns = this;
while (ns && ! root) {
qual = (ns.module.length) ? (ns.module + '::' + qual) : (qual);
ns = ns.parent;
}
return qual;
}
/**
* Fully qualified helper.
*
* Contructs a fully qualified helper function name, including any components
* corresponding to namespaces or nested resources. For instance, an "albums"
* resource nested under a "bands" resource would yeild helpers in the form of
* `bandAlbumsPath`, `newBandAlbumPath`, etc.
*
* @param {String} controller
* @return {String}
* @api protected
*/
Namespace.prototype.qhelper = function(helper) {
var comps = [helper];
var ns = this;
while (ns) {
if (ns.helper.length) { comps.unshift(ns.helper); }
ns = ns.parent;
}
return utils.helperize.apply(undefined, comps);
}
/**
* Expose `Namespace`.
*/
module.exports = Namespace;