Skip to content
This repository was archived by the owner on Jan 13, 2018. It is now read-only.

Commit c3e41ab

Browse files
author
David Luecke
committed
Added namespacing adding to global namespace
1 parent 24425a7 commit c3e41ab

File tree

7 files changed

+136
-43
lines changed

7 files changed

+136
-43
lines changed

examples/monsters.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Class = require('../lib/class').Class;
22

3-
var Monster = Class.extend(
3+
Class.extend('Monster',
44
/* @static */
55
{
66
count : 0
@@ -41,7 +41,7 @@ console.log("Hydra health: " + hydra.health); // -> 12
4141
dragon.fight();
4242
console.log("Dragon health: " + dragon.health); // -> 8
4343

44-
var SeaMonster = Monster.extend(
44+
Monster.extend('SeaMonster',
4545
{
4646
eat : function(smallChildren)
4747
{

examples/response_handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var Class = require('../lib/class').Class;
22

3-
var ResponseHandler = Class.extend({
3+
Class.extend('Server.Response.Handler', {
44
/* Static */
55
}, {
66
init : function(content, headers)
@@ -22,7 +22,7 @@ var ResponseHandler = Class.extend({
2222
}
2323
});
2424

25-
var handler = new ResponseHandler('Hello World from ResponseHandler\n', { 'Content-Type': 'text/plain' });
25+
var handler = new Server.Response.Handler('Hello World from ResponseHandler\n', { 'Content-Type': 'text/plain' });
2626

2727
var http = require('http');
2828
http.createServer(handler.callback('handle')).listen(1337, "127.0.0.1");

examples/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var cls = require('../lib/class');
22

3-
var Server = cls.Class.extend({
3+
cls.extend('Server', {
44
/* Static */
55
}, {
66
/* Prototype */

lib/class.js

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,65 +126,94 @@ util.extend(
126126
return inst;
127127
},
128128

129-
extend: function(klass, proto ) {
129+
getObject : util.getObject,
130+
131+
extend: function( fullName, klass, proto ) {
130132
// figure out what was passed
133+
if ( typeof fullName != 'string' ) {
134+
proto = klass;
135+
klass = fullName;
136+
fullName = null;
137+
}
131138
if (!proto ) {
132139
proto = klass;
133140
klass = null;
134141
}
135-
142+
136143
proto = proto || {};
137-
var _super_class = this, _super = this.prototype, prototype;
138-
144+
var _super_class = this,
145+
_super = this.prototype,
146+
name, shortName, namespace, prototype;
147+
139148
// Instantiate a base class (but only create the instance,
140149
// don't run the init constructor)
141150
initializing = true;
142151
prototype = new this();
143152
initializing = false;
144153
// Copy the properties over onto the new prototype
145154
inheritProps(proto, _super, prototype);
146-
155+
147156
// The dummy class constructor
148-
157+
149158
function Class() {
150159
// All construction is actually done in the init method
151160
if ( initializing ) return;
152-
161+
153162
if ( this.constructor !== Class && arguments.length ) { //we are being called w/o new
154163
return arguments.callee.extend.apply(arguments.callee, arguments);
155164
} else { //we are being called w/ new
156165
return this.Class.newInstance.apply(this.Class, arguments);
157166
}
158167
}
159-
160168
// Copy old stuff onto class
161169
for ( name in this ) {
162-
if ( this.hasOwnProperty(name) && ['prototype', 'defaults'].indexOf(name) == -1 ) {
170+
if ( this.hasOwnProperty(name) && ['prototype', 'defaults', 'getObject'].indexOf(name) == -1 ) {
163171
Class[name] = this[name];
164172
}
165173
}
166-
174+
167175
// do static inheritance
168176
inheritProps(klass, this, Class);
169-
177+
178+
// do namespace stuff
179+
if ( fullName ) {
180+
var parts = fullName.split(/\./),
181+
shortName = parts.pop(),
182+
current = clss.getObject(parts.join('.'), global, true),
183+
namespace = current;
184+
185+
if(current[shortName]){
186+
steal.dev.warn("class.js There's already something called "+fullName);
187+
}
188+
189+
current[shortName] = Class;
190+
}
191+
170192
// set things that can't be overwritten
171193
util.extend(Class, {
172194
prototype: prototype,
173-
constructor: Class
195+
namespace: namespace,
196+
shortName: shortName,
197+
constructor: Class,
198+
fullName: fullName
174199
});
175-
200+
176201
//make sure our prototype looks nice
177202
Class.prototype.Class = Class.prototype.constructor = Class;
178-
203+
179204
var args = Class.setup.apply(Class, concatArgs([_super_class],arguments));
180-
205+
181206
if ( Class.init ) {
182207
Class.init.apply(Class, args || []);
183208
}
184-
209+
185210
/* @Prototype*/
186211
return Class;
187212
}
188213
});
189214

190-
clss.prototype.callback = clss.callback;
215+
clss.prototype.callback = clss.callback;
216+
exports.extend = function(name, klass, proto)
217+
{
218+
return clss.extend(name, klass, proto);
219+
};

lib/util.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ var makeArray = exports.makeArray = function(array, results)
2929
if (array != null)
3030
{
3131
var type = typeof array;
32-
if ( array.length == null || type === "string" || type === "function" || type === "regexp")
32+
if (array.length == null || type === "string" || type === "function"
33+
|| type === "regexp")
3334
{
3435
ret.push(array);
3536
} else
@@ -73,8 +74,7 @@ var isPlainObject = exports.isPlainObject = function(obj)
7374
var has_is_property_of_method = hasOwnProperty.call(
7475
obj.constructor.prototype, "isPrototypeOf");
7576
// Not own constructor property must be Object
76-
if (obj.constructor && !has_own_constructor
77-
&& !has_is_property_of_method)
77+
if (obj.constructor && !has_own_constructor && !has_is_property_of_method)
7878
{
7979
return false;
8080
}
@@ -153,4 +153,55 @@ var extend = exports.extend = function()
153153

154154
// Return the modified object
155155
return target;
156+
};
157+
158+
var getObject = exports.getObject = function(objectName, roots, add)
159+
{
160+
var isContainer = function(current)
161+
{
162+
var type = typeof current;
163+
return type && (type == 'function' || type == 'object');
164+
}, regs =
165+
{
166+
undHash : /_|-/,
167+
colons : /::/,
168+
words : /([A-Z]+)([A-Z][a-z])/g,
169+
lowUp : /([a-z\d])([A-Z])/g,
170+
dash : /([a-z\d])([A-Z])/g,
171+
replacer : /\{([^\}]+)\}/g,
172+
dot : /\./
173+
}, getNext = function(current, nextPart, add){
174+
return current[nextPart] || ( add && (current[nextPart] = {}) );
175+
};
176+
177+
var parts = objectName ? objectName.split(regs.dot) : [], length = parts.length, currents = isArray(roots) ? roots
178+
: [ roots || globals ], current, ret, i, c = 0, type;
179+
180+
if (length == 0)
181+
{
182+
return currents[0];
183+
}
184+
while (current = currents[c++])
185+
{
186+
for (i = 0; i < length - 1 && isContainer(current); i++)
187+
{
188+
current = getNext(current, parts[i], add);
189+
}
190+
if (isContainer(current))
191+
{
192+
193+
ret = getNext(current, parts[i], add);
194+
195+
if (ret !== undefined)
196+
{
197+
198+
if (add === false)
199+
{
200+
delete current[parts[i]];
201+
}
202+
return ret;
203+
204+
}
205+
}
206+
}
156207
};

test/class.test.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
var Class = require('../lib/class').Class;
1+
var Class = require('../lib/class');
22

33
/**
44
* Tests creating a simple class
55
*/
66
exports.Simple = function (test) {
7-
var Animal = Class.extend({}, {
7+
Class.extend("Test.Animal", {}, {
88
doSomething : function()
99
{
1010
return this.somethingElse() + ' done';
@@ -16,15 +16,19 @@ exports.Simple = function (test) {
1616
}
1717
});
1818

19-
test.equals(new Animal().doSomething(), 'doing done', "Prototype method call should return this string");
19+
var myanimal = new Test.Animal();
20+
test.equals(myanimal.doSomething(), 'doing done', "Prototype method call should return this string");
21+
test.equals(myanimal.Class.shortName, "Animal", "Testing introspection shortname");
22+
test.equals(myanimal.Class.fullName, "Test.Animal", "Testing introspection fullname");
23+
test.ok(myanimal.Class.namespace.Animal, "Testing introspection namespace");
2024
test.done();
2125
};
2226

2327
/**
2428
* Tests the class constructor and setting of instance properties.
2529
*/
2630
exports.Constructor = function (test) {
27-
var Test = Class.extend({}, {
31+
Class.extend("Test.Person", {}, {
2832
init : function(age)
2933
{
3034
this._age = age;
@@ -36,31 +40,31 @@ exports.Constructor = function (test) {
3640
}
3741
});
3842

39-
test.equals(new Test(23).doSomething(), 23, "Instance property should be set properly");
43+
test.equals(new Test.Person(23).doSomething(), 23, "Instance property should be set properly");
4044
test.done();
4145
};
4246

4347
/**
4448
* Tests extending an existing class
4549
*/
4650
exports.Extend = function (test) {
47-
var Animal = Class.extend({}, {
51+
Class.extend("Test.Mammal", {}, {
4852
doSomething : function()
4953
{
5054
return 'done';
5155
}
5256
});
5357

54-
test.equals(new Animal().doSomething(), 'done', "Prototype method call should return this string");
58+
test.equals(new Test.Mammal().doSomething(), 'done', "Prototype method call should return this string");
5559

56-
var Dog = Animal.extend({}, {
60+
Test.Mammal.extend("Test.Dog", {}, {
5761
doSomething : function()
5862
{
5963
var oldresult = this._super();
6064
return 'bark ' + oldresult;
6165
}
6266
});
63-
test.equals(new Dog().doSomething(), 'bark done', "Extended class should return different string");
67+
test.equals(new Test.Dog().doSomething(), 'bark done', "Extended class should return different string");
6468

6569
test.done();
6670
};
@@ -69,7 +73,7 @@ exports.Extend = function (test) {
6973
* Tests accessing static properties
7074
*/
7175
exports.Static = function (test) {
72-
var Test = Class.extend({
76+
Class.extend("Test.Static", {
7377
staticProperty : 'test',
7478
staticMethod : function()
7579
{
@@ -82,8 +86,8 @@ exports.Static = function (test) {
8286
}
8387
});
8488

85-
test.equals(Test.staticProperty, 'test', "Static properties are always accessible");
86-
test.equals(new Test().doSomething(), 'test static', "Static properties should be available");
89+
test.equals(Test.Static.staticProperty, 'test', "Static properties are always accessible");
90+
test.equals(new Test.Static().doSomething(), 'test static', "Static properties should be available");
8791

8892
test.done();
8993
};
@@ -92,7 +96,7 @@ exports.Static = function (test) {
9296
* Tests the setup() function and currying the return value to the constructor
9397
*/
9498
exports.Setup = function (test) {
95-
var Test = Class.extend({
99+
Class.extend("Test.Setup", {
96100
}, {
97101
setup : function(arg)
98102
{
@@ -105,7 +109,7 @@ exports.Setup = function (test) {
105109
this._wrapped = arg;
106110
}
107111
});
108-
test.equals(new Test('test')._wrapped, 'wrapped(test)', "setup() wraps arguments before calling init");
112+
test.equals(new Test.Setup('test')._wrapped, 'wrapped(test)', "setup() wraps arguments before calling init");
109113

110114
test.done();
111115
};
@@ -114,7 +118,7 @@ exports.Setup = function (test) {
114118
* Tests callback creation
115119
*/
116120
exports.Callback = function (test) {
117-
var Test = Class.extend({
121+
Class.extend("Test.Callback", {
118122
}, {
119123
init : function()
120124
{
@@ -127,7 +131,7 @@ exports.Callback = function (test) {
127131
return this._property + value;
128132
}
129133
});
130-
var tester = new Test();
134+
var tester = new Test.Callback();
131135

132136
var callback = tester.callback('doSomething');
133137
test.equals(callback(), 'local', "Callback wrapped function should have access to object properties");
@@ -145,7 +149,7 @@ exports.Callback = function (test) {
145149
* Tests nesting of callback
146150
*/
147151
exports.CallbackNesting = function (test) {
148-
var Test = Class.extend({
152+
Class.extend("Test.CallbackNested", {
149153
}, {
150154
processor : function(arg)
151155
{
@@ -157,7 +161,7 @@ exports.CallbackNesting = function (test) {
157161
return arg + ' done';
158162
}
159163
});
160-
var tester = new Test();
164+
var tester = new Test.CallbackNested();
161165
var callback = tester.callback(['processor', 'doSomething']);
162166
test.equals(callback('first'), 'first processed done', "Calbacks can be chained and get their previous callbacks result");
163167
test.done();

test/util.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
var util = require('../lib/util');
22

3+
exports.getObject = function (test) {
4+
var obj = {};
5+
util.getObject("My.Test.Object", obj, true);
6+
test.ok(obj.My, "First level object created");
7+
test.ok(obj.My.Test, "Second level object created");
8+
test.ok(obj.My.Test.Object, "Third level object created");
9+
test.done();
10+
};
11+
312
exports.merge = function (test) {
413
var first = [1, 2];
514
var second = [3, 4, 5];

0 commit comments

Comments
 (0)