Created
November 23, 2013 09:12
-
-
Save kristianmandrup/7612501 to your computer and use it in GitHub Desktop.
namespaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# namespaces.js | |
# Contains top level app namespaces | |
exports.do = | |
Models: {} | |
Controllers: {} | |
new-ns: (ns) -> | |
# split on "." and add recursively to self | |
var nsParts = namespace.split("."), | |
nsPart = nsParts.shift(), | |
parent = @ | |
myGlobal = parent; | |
while(nsPart = nsParts.shift()) { | |
parent = parent[nsPart] = parent[nsPart] || {}; | |
} | |
myGlobal; | |
get-ns: (ns) -> | |
var nsParts = namespace.split("."), | |
nsPart = nsParts.shift(), | |
parent = ^^@ | |
while(nsPart = nsParts.shift()) { | |
throw new Error("no such namespace: #{nsPart} at #{nsParts * '.'}") | |
parent = parent[nsPart] | |
} | |
parent; | |
add-ns: (base, ns) -> | |
var parent = @find-ns base | |
# split on "." and add recursively to self | |
nsParts = ns.split("."), | |
nsPart = nsParts.shift(), | |
myGlobal = parent; | |
while(nsPart = nsParts.shift()) { | |
parent = parent[nsPart] = parent[nsPart] || {}; | |
} | |
myGlobal; | |
app = require \namespaces | |
app.add-namespace 'Model.Validator' | |
class app.Model.User extends app.Model.Base |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment