-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbatman.utilitybelt.coffee
52 lines (43 loc) · 1.72 KB
/
batman.utilitybelt.coffee
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
class Batman.UtilityBelt extends Batman.Object
constructor: (app) ->
@app = app
displayRoutes: ->
# grab all of the routes
routeMap = @app.get('routes').routeMap.childrenByOrder
# find the max length of controller / action / pattern
maxControllerLength = 0
maxActionLength = 0
maxPatternLength = 0
routesArray = []
for num in [0..routeMap.length - 1] by 1
maxControllerLength = Math.max(routeMap[num].controller.length, maxControllerLength)
maxActionLength = Math.max(routeMap[num].action.length, maxActionLength)
maxPatternLength = Math.max(routeMap[num].pattern.length, maxPatternLength)
routesArray.push(routeMap[num])
# display # of routes and headers
log('Total number of routes: ' + routeMap.length)
log(fixWidth('__Controller__', maxControllerLength) + fixWidth('__Action__', maxActionLength) + fixWidth('__Pattern__', maxPatternLength))
# sort the routes
routesArray.sort(sortRouteMap)
# pad the string with spaces
for num in [0..routeMap.length - 1] by 1
log(fixWidth(routesArray[num].controller, maxControllerLength) + fixWidth(routesArray[num].action, maxActionLength) + fixWidth(routesArray[num].pattern, maxPatternLength))
return undefined
# sort route function
sortRouteMap = (a,b) ->
if (a.controller < b.controller)
return -1
if (a.controller > b.controller)
return 1
if (a.controller is b.controller)
if (a.action < b.action)
return -1
if (a.action > b.action)
return 1
return 0;
fixWidth = (strang, width) ->
# tack on the padding amount
width = width + 12
return strang + new Array(width - strang.length).join(" ");
log = (msg) ->
console?.log msg