Browse files

add beginning of namespace utility and add it to the build

  • Loading branch information...
1 parent 7532036 commit 396c16266a856063ba92343d1b1b578bda3b888e @semmypurewal committed Jul 24, 2012
Showing with 106 additions and 79 deletions.
  1. +1 −0 build.properties
  2. +1 −0 build.xml
  3. +1 −1 build/jermaine-min.js
  4. +43 −37 build/jermaine.js
  5. +10 −0 lib/util/test.html
  6. +33 −0 lib/util/util.js
  7. +3 −0 spec/index.html
  8. +1 −1 spec/validator.js
  9. +2 −6 src/attr.js
  10. +3 −6 src/attr_list.js
  11. +3 −9 src/method.js
  12. +2 −12 src/model.js
  13. +3 −7 src/validator.js
View
1 build.properties
@@ -16,4 +16,5 @@ jasmine.script = ${lib.dir}/jasmine/runscript/run-jasmine.js
yuicompressor = ${lib.dir}/yuicompressor/build/yuicompressor-2.4.7.jar
yuicompressor.options =
+lib.dependencies = util/util.js
concatenate.order = validator.js
View
1 build.xml
@@ -11,6 +11,7 @@
<target name="concatenate">
<concat destfile="${build.file}" fixlastline="yes" eol="lf">
+ <filelist dir="${lib.dir}" files="${lib.dependencies}" />
<filelist dir="${src.dir}" files="${concatenate.order}" />
<fileset dir="${src.dir}" includes="**/*.js" excludes="${concatenate.order}" />
</concat>
View
2 build/jermaine-min.js
@@ -1 +1 @@
-if(!window.jermaine){window.jermaine={}}(function(c){var d=this,b,a={};b=function(e){var f=function(h){var g,j={},i;g=e.call(j,h);if(!g){i=j.message||"validator failed with parameter "+h;throw new Error(i)}return g};return f};b.addValidator=function(f,e){if(f===undefined||typeof(f)!=="string"){throw new Error("addValidator requires a name to be specified as the first parameter")}if(e===undefined||typeof(e)!=="function"){throw new Error("addValidator requires a function as the second parameter")}if(a[f]===undefined){a[f]=function(g){return new b(function(j){var i={actual:j,param:j},h=e.call(i,g);this.message=i.message;return h})}}else{throw new Error("Validator '"+f+"' already defined")}};b.getValidator=function(f){var e;if(f===undefined){throw new Error("Validator: getValidator method requires a string parameter")}else{if(typeof(f)!=="string"){throw new Error("Validator: parameter to getValidator method must be a string")}}e=a[f];if(e===undefined){throw new Error("Validator: '"+f+"' does not exist")}return e};b.validators=function(){var f,e=[];for(f in a){if(a.hasOwnProperty(f)){e.push(f)}}return e};b.addValidator("isGreaterThan",function(e){this.message=this.param+" should be greater than "+e;return this.param>e});b.addValidator("isLessThan",function(e){this.message=this.param+" should be less than "+e;return this.param<e});b.addValidator("isA",function(f){var e=["string","number","boolean","function","object"];if(typeof(f)==="string"&&e.indexOf(f)>-1){this.message=this.param+" should be a "+f;return typeof(this.param)===f}else{if(f==="integer"){if(this.param.toString!==undefined){this.message=this.param.toString()+" should be an integer"}else{this.message="parameter should be an integer"}return(typeof(this.param)==="number")&&(parseInt(this.param,10)===this.param)}else{if(typeof(f)==="string"){throw new Error("Validator: isA accepts a string which is one of "+e)}else{throw new Error("Validator: isA only accepts a string for a primitive types for the time being")}}}});b.addValidator("isOneOf",function(e){this.message=this.param+" should be one of the set: "+e;return e.indexOf(this.param)>-1});c.Validator=b}(window.jermaine));if(!window.jermaine){window.jermaine={}}(function(b){var a={};var c=function(g){var j=[],n=this,o="invalid setter call for "+g,k,l,m,f,d,p=false,e,q=window.jermaine.AttrList,h=window.jermaine.Validator;e=function(i){for(m=0;m<j.length;++m){j[m](i)}return true};l=function(){return(typeof(k)==="function")?k():k};if(g===undefined||typeof(g)!=="string"){throw new Error("Attr: constructor requires a name parameter which must be a string")}this.validatesWith=function(i){if(typeof(i)==="function"){j.push(new h(i));return this}else{throw new Error("Attr: validator must be a function")}};this.defaultsTo=function(i){k=i;return this};this.isImmutable=function(){p=true;return this};this.isMutable=function(){p=false;return this};this.clone=function(){var r=(this instanceof q)?new q(g):new c(g),s;for(s=0;s<j.length;++s){r.validatesWith(j[s])}r.defaultsTo(k);if(p){r.isImmutable()}return r};this.and=this;this.which=this;this.eachOfWhich=this;this.validator=function(){return e};this.addTo=function(s){var r,i;if(!s||typeof(s)!=="object"){throw new Error("Attr: addAttr method requires an object parameter")}i=l();if(i!==undefined&&e(i)){r=i}else{if(i!==undefined&&!e(i)){throw new Error("Attr: Default value of "+i+" does not pass validation for "+g)}}s[g]=function(t){if(t!==undefined){if(p&&r!==undefined){throw new Error("cannot set the immutable property "+g+" after it has been set")}else{if(!e(t)){throw new Error(o)}else{r=t}}return s}else{return r}}};d=function(i){n[i]=function(r){j.push(h.getValidator(i)(r));return n}};for(m=0;m<h.validators().length;++m){d(h.validators()[m])}};b.Attr=c}(window.jermaine));if(!window.jermaine){window.jermaine={}}(function(a){function b(c){var e=this;a.Attr.call(this,c);var d=function(g,f){return function(){return g[f].apply(e,arguments)}};this.validateWith=this.validatesWith;this.defaultsTo=function(){};this.isImmutable=function(){};this.isMutable=function(){};this.addTo=function(h){var i,f=[],g={};if(!h||typeof(h)!=="object"){throw new Error("AttrList: addTo method requires an object parameter")}else{g.pop=d(f,"pop");g.add=function(j){if((e.validator())(j)){f.push(j);return this}else{throw new Error(e.errorMessage())}};g.at=function(j){if(j<0||j>=this.size()){throw new Error("AttrList: Index out of bounds")}return f[j]};g.get=g.at;g.size=function(){return f.length};h[c]=function(){return g}}}}b.prototype=new window.jermaine.Attr(name);a.AttrList=b}(window.jermaine));if(!window.jermaine){window.jermaine={}}(function(a){var b=function(c,d){if(!c||typeof(c)!=="string"){throw new Error("Method: constructor requires a name parameter which must be a string")}else{if(!d||typeof(d)!=="function"){throw new Error("Method: second parameter must be a function")}}this.addTo=function(e){if(!e||typeof(e)!=="object"){throw new Error("Method: addTo method requires an object parameter")}e[c]=d}};a.Method=b}(window.jermaine));if(!window.multigraph){window.multigraph={}}if(!window.jermaine){window.jermaine={}}(function(a){function b(u){var h=this,q={},i={},p,f=false,d=[],n=[],r=[],c=a.Method,s=a.Attr,l=a.AttrList,g,v,k,j,t=function(){},o=function(){},e=function(){if(f){k()}return o.apply(this,arguments)};if(arguments.length>1){u=arguments[arguments.length-1]}if(u&&typeof(u)==="function"){e=new b();u.call(e);return e}else{if(u){throw new Error("Model: specification parameter must be a function")}}var m=function(y,x){var A=y==="Attr"?s:l,w=y==="Attr"?"hasA":"hasMany",z;f=true;if(typeof(x)==="string"){z=new A(x);i[x]=z;return z}else{throw new Error("Model: "+w+" parameter must be a string")}};g=function(y,x){var w;if(typeof(x)!=="string"){throw new Error("Model: expected string argument to "+y+" method, but recieved "+x)}w=y==="attribute"?i[x]:q[x];if(w===undefined){throw new Error("Model: "+y+" "+x+" does not exist!")}return w};v=function(y){var x,z=[],w=y==="attributes"?i:q;for(x in w){if(w.hasOwnProperty(x)){z.push(x)}}return z};k=function(w){var z=this,x,y;e.validate();o=function(){var B,A=function(F,E){var D=E==="attributes"?i:q,C;for(C in D){if(D.hasOwnProperty(C)){if(D===i&&j){D[C].isImmutable()}D[C].addTo(F)}}};A(this,"attributes");A(this,"methods");this.toString=p;if(arguments.length>0){if(arguments.length<d.length){y="Constructor requires ";for(B=0;B<d.length;++B){y+=d[B];y+=B===d.length-1?"":", "}y+=" to be specified";throw new Error(y)}else{for(B=0;B<arguments.length;++B){if(B<d.length){this[d[B]](arguments[B])}else{this[n[B-d.length]](arguments[B])}}}}t.call(this)};return o};e.hasA=function(w){return m("Attr",w)};e.hasAn=e.hasA;e.hasMany=function(w){return m("AttrList",w)};e.isA=function(y){var x,w,A,z;f=true;z=function(C){var B,D=new b();for(B in D){if(D.hasOwnProperty(B)&&typeof(C[B])!==typeof(D[B])){return false}}return true};if(typeof(y)!=="function"||!z(y)){throw new Error("Model: parameter sent to isA function must be a Model")}if(r.length===0){r.push(y)}else{throw new Error("Model: Model only supports single inheritance at this time")}w=r[0].attributes();for(x=0;x<w.length;++x){if(i[w[x]]===undefined){i[w[x]]=r[0].attribute(w[x]).clone();i[w[x]].isMutable()}}A=r[0].methods();for(x=0;x<A.length;++x){if(q[A[x]]===undefined){q[A[x]]=r[0].method(A[x])}}for(x=0;x<r.length;x++){e.prototype=new r[x]()}};e.isAn=e.isA;e.parent=function(){return r[0].apply(this,arguments)};e.attribute=function(w){return g("attribute",w)};e.attributes=function(){return v("attributes")};e.method=function(w){return g("method",w)};e.methods=function(){return v("methods")};e.isBuiltWith=function(){var w=false,x;f=true;d=[];n=[];for(x=0;x<arguments.length;++x){if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)!=="%"){if(w){throw new Error("Model: isBuiltWith requires parameters preceded with a % to be the final parameters before the optional function")}else{d.push(arguments[x])}}else{if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)==="%"){w=true;n.push(arguments[x].slice(1))}else{if(typeof(arguments[x])==="function"&&x===arguments.length-1){t=arguments[x]}else{throw new Error("Model: isBuiltWith parameters must be strings except for a function as the optional final parameter")}}}}};e.isImmutable=function(){j=true};e.looksLike=function(w){f=true;p=w};e.respondsTo=function(x,y){var w=new c(x,y);f=true;q[x]=w};e.validate=function(){var y,x=this.attributes(),w=this.methods();for(y=0;y<d.length;++y){try{this.attribute(d[y])}catch(z){throw new Error(d[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<n.length;++y){try{this.attribute(n[y])}catch(z){throw new Error(n[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<x.length;y++){if(w.indexOf(x[y])>-1){throw new Error("Model: invalid model specification to "+x[y]+" being both an attribute and method")}}if(j){for(y=0;y<x.length;y++){if(d.indexOf(x[y])<0){throw new Error("immutable objects must have all attributes required in a call to isBuiltWith")}}}f=false};return e}a.Model=b}(window.jermaine));
+if(!window.util){window.util={}}(function(a){a.namespace=function(e,g){var f,d,c,b={};f=e.split(".");d=window;for(c=1;c<f.length;++c){if(d[f[c]]===undefined){console.log("building subnamespace "+f[c]);d[f[c]]={}}d=d[f[c]]}g(d)}}(window.util));window.util.namespace("window.jermaine",function(c){var d=this,b,a={};b=function(e){var f=function(h){var g,j={},i;g=e.call(j,h);if(!g){i=j.message||"validator failed with parameter "+h;throw new Error(i)}return g};return f};b.addValidator=function(f,e){if(f===undefined||typeof(f)!=="string"){throw new Error("addValidator requires a name to be specified as the first parameter")}if(e===undefined||typeof(e)!=="function"){throw new Error("addValidator requires a function as the second parameter")}if(a[f]===undefined){a[f]=function(g){return new b(function(j){var i={actual:j,param:j},h=e.call(i,g);this.message=i.message;return h})}}else{throw new Error("Validator '"+f+"' already defined")}};b.getValidator=function(f){var e;if(f===undefined){throw new Error("Validator: getValidator method requires a string parameter")}else{if(typeof(f)!=="string"){throw new Error("Validator: parameter to getValidator method must be a string")}}e=a[f];if(e===undefined){throw new Error("Validator: '"+f+"' does not exist")}return e};b.validators=function(){var f,e=[];for(f in a){if(a.hasOwnProperty(f)){e.push(f)}}return e};b.addValidator("isGreaterThan",function(e){this.message=this.param+" should be greater than "+e;return this.param>e});b.addValidator("isLessThan",function(e){this.message=this.param+" should be less than "+e;return this.param<e});b.addValidator("isA",function(f){var e=["string","number","boolean","function","object"];if(typeof(f)==="string"&&e.indexOf(f)>-1){this.message=this.param+" should be a "+f;return typeof(this.param)===f}else{if(f==="integer"){if(this.param.toString!==undefined){this.message=this.param.toString()+" should be an integer"}else{this.message="parameter should be an integer"}return(typeof(this.param)==="number")&&(parseInt(this.param,10)===this.param)}else{if(typeof(f)==="string"){throw new Error("Validator: isA accepts a string which is one of "+e)}else{throw new Error("Validator: isA only accepts a string for a primitive types for the time being")}}}});b.addValidator("isOneOf",function(e){this.message=this.param+" should be one of the set: "+e;return e.indexOf(this.param)>-1});c.Validator=b});window.util.namespace("window.jermaine",function(b){var a={};var c=function(g){var j=[],n=this,o="invalid setter call for "+g,k,l,m,f,d,p=false,e,q=window.jermaine.AttrList,h=window.jermaine.Validator;e=function(i){for(m=0;m<j.length;++m){j[m](i)}return true};l=function(){return(typeof(k)==="function")?k():k};if(g===undefined||typeof(g)!=="string"){throw new Error("Attr: constructor requires a name parameter which must be a string")}this.validatesWith=function(i){if(typeof(i)==="function"){j.push(new h(i));return this}else{throw new Error("Attr: validator must be a function")}};this.defaultsTo=function(i){k=i;return this};this.isImmutable=function(){p=true;return this};this.isMutable=function(){p=false;return this};this.clone=function(){var r=(this instanceof q)?new q(g):new c(g),s;for(s=0;s<j.length;++s){r.validatesWith(j[s])}r.defaultsTo(k);if(p){r.isImmutable()}return r};this.and=this;this.which=this;this.eachOfWhich=this;this.validator=function(){return e};this.addTo=function(s){var r,i;if(!s||typeof(s)!=="object"){throw new Error("Attr: addAttr method requires an object parameter")}i=l();if(i!==undefined&&e(i)){r=i}else{if(i!==undefined&&!e(i)){throw new Error("Attr: Default value of "+i+" does not pass validation for "+g)}}s[g]=function(t){if(t!==undefined){if(p&&r!==undefined){throw new Error("cannot set the immutable property "+g+" after it has been set")}else{if(!e(t)){throw new Error(o)}else{r=t}}return s}else{return r}}};d=function(i){n[i]=function(r){j.push(h.getValidator(i)(r));return n}};for(m=0;m<h.validators().length;++m){d(h.validators()[m])}};b.Attr=c});window.util.namespace("window.jermaine",function(a){function b(c){var e=this;a.Attr.call(this,c);var d=function(g,f){return function(){return g[f].apply(e,arguments)}};this.validateWith=this.validatesWith;this.defaultsTo=function(){};this.isImmutable=function(){};this.isMutable=function(){};this.addTo=function(h){var i,f=[],g={};if(!h||typeof(h)!=="object"){throw new Error("AttrList: addTo method requires an object parameter")}else{g.pop=d(f,"pop");g.add=function(j){if((e.validator())(j)){f.push(j);return this}else{throw new Error(e.errorMessage())}};g.at=function(j){if(j<0||j>=this.size()){throw new Error("AttrList: Index out of bounds")}return f[j]};g.get=g.at;g.size=function(){return f.length};h[c]=function(){return g}}}}b.prototype=new window.jermaine.Attr(name);a.AttrList=b});window.util.namespace("window.jermaine",function(a){var b=function(c,d){if(!c||typeof(c)!=="string"){throw new Error("Method: constructor requires a name parameter which must be a string")}else{if(!d||typeof(d)!=="function"){throw new Error("Method: second parameter must be a function")}}this.addTo=function(e){if(!e||typeof(e)!=="object"){throw new Error("Method: addTo method requires an object parameter")}e[c]=d}};a.Method=b});window.util.namespace("window.jermaine",function(a){function b(u){var h=this,q={},i={},p,f=false,d=[],n=[],r=[],c=a.Method,s=a.Attr,l=a.AttrList,g,v,k,j,t=function(){},o=function(){},e=function(){if(f){k()}return o.apply(this,arguments)};if(arguments.length>1){u=arguments[arguments.length-1]}if(u&&typeof(u)==="function"){e=new b();u.call(e);return e}else{if(u){throw new Error("Model: specification parameter must be a function")}}var m=function(y,x){var A=y==="Attr"?s:l,w=y==="Attr"?"hasA":"hasMany",z;f=true;if(typeof(x)==="string"){z=new A(x);i[x]=z;return z}else{throw new Error("Model: "+w+" parameter must be a string")}};g=function(y,x){var w;if(typeof(x)!=="string"){throw new Error("Model: expected string argument to "+y+" method, but recieved "+x)}w=y==="attribute"?i[x]:q[x];if(w===undefined){throw new Error("Model: "+y+" "+x+" does not exist!")}return w};v=function(y){var x,z=[],w=y==="attributes"?i:q;for(x in w){if(w.hasOwnProperty(x)){z.push(x)}}return z};k=function(w){var z=this,x,y;e.validate();o=function(){var B,A=function(F,E){var D=E==="attributes"?i:q,C;for(C in D){if(D.hasOwnProperty(C)){if(D===i&&j){D[C].isImmutable()}D[C].addTo(F)}}};A(this,"attributes");A(this,"methods");this.toString=p;if(arguments.length>0){if(arguments.length<d.length){y="Constructor requires ";for(B=0;B<d.length;++B){y+=d[B];y+=B===d.length-1?"":", "}y+=" to be specified";throw new Error(y)}else{for(B=0;B<arguments.length;++B){if(B<d.length){this[d[B]](arguments[B])}else{this[n[B-d.length]](arguments[B])}}}}t.call(this)};return o};e.hasA=function(w){return m("Attr",w)};e.hasAn=e.hasA;e.hasMany=function(w){return m("AttrList",w)};e.isA=function(y){var x,w,A,z;f=true;z=function(C){var B,D=new b();for(B in D){if(D.hasOwnProperty(B)&&typeof(C[B])!==typeof(D[B])){return false}}return true};if(typeof(y)!=="function"||!z(y)){throw new Error("Model: parameter sent to isA function must be a Model")}if(r.length===0){r.push(y)}else{throw new Error("Model: Model only supports single inheritance at this time")}w=r[0].attributes();for(x=0;x<w.length;++x){if(i[w[x]]===undefined){i[w[x]]=r[0].attribute(w[x]).clone();i[w[x]].isMutable()}}A=r[0].methods();for(x=0;x<A.length;++x){if(q[A[x]]===undefined){q[A[x]]=r[0].method(A[x])}}for(x=0;x<r.length;x++){e.prototype=new r[x]()}};e.isAn=e.isA;e.parent=function(){return r[0].apply(this,arguments)};e.attribute=function(w){return g("attribute",w)};e.attributes=function(){return v("attributes")};e.method=function(w){return g("method",w)};e.methods=function(){return v("methods")};e.isBuiltWith=function(){var w=false,x;f=true;d=[];n=[];for(x=0;x<arguments.length;++x){if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)!=="%"){if(w){throw new Error("Model: isBuiltWith requires parameters preceded with a % to be the final parameters before the optional function")}else{d.push(arguments[x])}}else{if(typeof(arguments[x])==="string"&&arguments[x].charAt(0)==="%"){w=true;n.push(arguments[x].slice(1))}else{if(typeof(arguments[x])==="function"&&x===arguments.length-1){t=arguments[x]}else{throw new Error("Model: isBuiltWith parameters must be strings except for a function as the optional final parameter")}}}}};e.isImmutable=function(){j=true};e.looksLike=function(w){f=true;p=w};e.respondsTo=function(x,y){var w=new c(x,y);f=true;q[x]=w};e.validate=function(){var y,x=this.attributes(),w=this.methods();for(y=0;y<d.length;++y){try{this.attribute(d[y])}catch(z){throw new Error(d[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<n.length;++y){try{this.attribute(n[y])}catch(z){throw new Error(n[y]+", specified in the isBuiltWith method, is not an attribute")}}for(y=0;y<x.length;y++){if(w.indexOf(x[y])>-1){throw new Error("Model: invalid model specification to "+x[y]+" being both an attribute and method")}}if(j){for(y=0;y<x.length;y++){if(d.indexOf(x[y])<0){throw new Error("immutable objects must have all attributes required in a call to isBuiltWith")}}}f=false};return e}a.Model=b});
View
80 build/jermaine.js
@@ -1,8 +1,37 @@
-if (!window.jermaine) {
- window.jermaine = {};
+if (!window.util) {
+ window.util = {};
}
(function (ns) {
+ ns.namespace = function (ns, func) {
+ var nsArray,
+ currentNS,
+ i,
+ nsAdditions = {};
+
+ //TODO: check to make sure ns is a properly formatted namespace string
+
+ //TODO: confirm func is actually a function
+
+ //parse namespace string
+ nsArray = ns.split(".");
+
+ //TODO: make sure root exists (can we do this for window?!?!?!?!)
+ currentNS = window;
+
+ //build namespace
+ for (i = 1; i < nsArray.length; ++i) {
+ if (currentNS[nsArray[i]] === undefined) {
+ console.log("building subnamespace " + nsArray[i]);
+ currentNS[nsArray[i]] = {};
+ }
+ currentNS = currentNS[nsArray[i]];
+ }
+
+ func(currentNS);
+ };
+}(window.util));
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var that = this,
Validator,
@@ -40,7 +69,7 @@ if (!window.jermaine) {
this.message = resultObject.message;
return result;
});
- }
+ };
} else {
throw new Error("Validator '" + name +"' already defined");
}
@@ -114,17 +143,13 @@ if (!window.jermaine) {
});
ns.Validator = Validator;
-}(window.jermaine));
+});
/*
+ what about isNotGreaterThan()?, isNotLessThan()? Or, better still: a general 'not' operator, as in jasmine?
+ use of deprecated errorsWith in implementation of clone()?
*/
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var staticValidators = {};
@@ -259,13 +284,10 @@ if(!window.jermaine) {
};
ns.Attr = Attr;
-}(window.jermaine));
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+});
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
+
function AttrList(name) {
var that = this;
@@ -338,12 +360,8 @@ if(!window.jermaine) {
AttrList.prototype = new window.jermaine.Attr(name);
ns.AttrList = AttrList;
-}(window.jermaine));
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+});
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var Method = function (name, method) {
@@ -352,9 +370,7 @@ if(!window.jermaine) {
} else if (!method || typeof(method) !== "function") {
throw new Error("Method: second parameter must be a function");
}
-
-
-
+
this.addTo = function (obj) {
if (!obj || typeof(obj) !== 'object') {
throw new Error("Method: addTo method requires an object parameter");
@@ -364,16 +380,8 @@ if(!window.jermaine) {
};
};
ns.Method = Method;
-}(window.jermaine));
-if(!window.multigraph) {
- window.multigraph = {};
-}
-
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+});
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
function Model(specification) {
var that = this,
@@ -709,7 +717,5 @@ if(!window.jermaine) {
return model;
}
-
-
ns.Model = Model;
-}(window.jermaine));
+});
View
10 lib/util/test.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <script src="util.js"></script>
+ </head>
+
+ <body>
+
+ </body>
+</html>
View
33 lib/util/util.js
@@ -0,0 +1,33 @@
+if (!window.util) {
+ window.util = {};
+}
+
+(function (ns) {
+ ns.namespace = function (ns, func) {
+ var nsArray,
+ currentNS,
+ i,
+ nsAdditions = {};
+
+ //TODO: check to make sure ns is a properly formatted namespace string
+
+ //TODO: confirm func is actually a function
+
+ //parse namespace string
+ nsArray = ns.split(".");
+
+ //TODO: make sure root exists (can we do this for window?!?!?!?!)
+ currentNS = window;
+
+ //build namespace
+ for (i = 1; i < nsArray.length; ++i) {
+ if (currentNS[nsArray[i]] === undefined) {
+ console.log("building subnamespace " + nsArray[i]);
+ currentNS[nsArray[i]] = {};
+ }
+ currentNS = currentNS[nsArray[i]];
+ }
+
+ func(currentNS);
+ };
+}(window.util));
View
3 spec/index.html
@@ -13,6 +13,9 @@
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'
type='text/javascript'></script>
+ <!-- include lib here -->
+ <script type="text/javascript" src="../lib/util/util.js"></script>
+
<!-- include src here -->
<script type="text/javascript" src="../src/validator.js"></script>
<script type="text/javascript" src="../src/attr.js"></script>
View
2 spec/validator.js
@@ -2,7 +2,7 @@
describe("Validator", function () {
"use strict";
- var Validator = jermaine.Validator;
+ var Validator = window.jermaine.Validator;
xit("should throw an error on an empty parameter", function () {
View
8 src/attr.js
@@ -3,11 +3,7 @@
+ use of deprecated errorsWith in implementation of clone()?
*/
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var staticValidators = {};
@@ -142,4 +138,4 @@ if(!window.jermaine) {
};
ns.Attr = Attr;
-}(window.jermaine));
+});
View
9 src/attr_list.js
@@ -1,9 +1,6 @@
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
+
function AttrList(name) {
var that = this;
@@ -76,4 +73,4 @@ if(!window.jermaine) {
AttrList.prototype = new window.jermaine.Attr(name);
ns.AttrList = AttrList;
-}(window.jermaine));
+});
View
12 src/method.js
@@ -1,8 +1,4 @@
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var Method = function (name, method) {
@@ -11,9 +7,7 @@ if(!window.jermaine) {
} else if (!method || typeof(method) !== "function") {
throw new Error("Method: second parameter must be a function");
}
-
-
-
+
this.addTo = function (obj) {
if (!obj || typeof(obj) !== 'object') {
throw new Error("Method: addTo method requires an object parameter");
@@ -23,4 +17,4 @@ if(!window.jermaine) {
};
};
ns.Method = Method;
-}(window.jermaine));
+});
View
14 src/model.js
@@ -1,12 +1,4 @@
-if(!window.multigraph) {
- window.multigraph = {};
-}
-
-if(!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
function Model(specification) {
var that = this,
@@ -342,7 +334,5 @@ if(!window.jermaine) {
return model;
}
-
-
ns.Model = Model;
-}(window.jermaine));
+});
View
10 src/validator.js
@@ -1,8 +1,4 @@
-if (!window.jermaine) {
- window.jermaine = {};
-}
-
-(function (ns) {
+window.util.namespace("window.jermaine", function (ns) {
"use strict";
var that = this,
Validator,
@@ -40,7 +36,7 @@ if (!window.jermaine) {
this.message = resultObject.message;
return result;
});
- }
+ };
} else {
throw new Error("Validator '" + name +"' already defined");
}
@@ -114,4 +110,4 @@ if (!window.jermaine) {
});
ns.Validator = Validator;
-}(window.jermaine));
+});

0 comments on commit 396c162

Please sign in to comment.