Skip to content

Commit

Permalink
Changing name and dropping namespaces again
Browse files Browse the repository at this point in the history
  • Loading branch information
David Luecke committed Nov 12, 2011
1 parent 76c191d commit 6a96d8c
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 263 deletions.
5 changes: 2 additions & 3 deletions examples/monsters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Class = require('../lib/class').Class;

Class.extend('Monster',
var Monster = Class.extend(
/* @static */
{
count : 0
Expand Down Expand Up @@ -41,8 +41,7 @@ console.log("Hydra health: " + hydra.health); // -> 12
dragon.fight();
console.log("Dragon health: " + dragon.health); // -> 8

Monster.extend('SeaMonster',
{
var SeaMonster = Monster.extend({
eat : function(smallChildren)
{
this._super(smallChildren / 2);
Expand Down
8 changes: 4 additions & 4 deletions examples/response_handler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var Class = require('../lib/class').Class;

Class.extend('Server.Response.Handler', {
/* Static */
}, {
var Handler = Class.extend(
/* Prototype */
{
init : function(content, headers)
{
this._headers = headers;
Expand All @@ -22,7 +22,7 @@ Class.extend('Server.Response.Handler', {
}
});

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

var http = require('http');
http.createServer(handler.callback('handle')).listen(1337, "127.0.0.1");
6 changes: 2 additions & 4 deletions examples/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var cls = require('../lib/class');
var Class = require('../lib/class');

cls.extend('Server', {
/* Static */
}, {
var Server = Class.extend({
/* Prototype */
init : function(port, listen)
{
Expand Down
49 changes: 11 additions & 38 deletions lib/class.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// See https://github.com/jupiterjs/jquerymx/blob/master/class/class.js
// if we are initializing a new class
var util = require('./util'),
initializing = false, makeArray = util.makeArray, isFunction = util.isFunction, isArray = util.isArray,
initializing = false, makeArray = util.makeArray, isFunction = util.isFunction, isArray = util.isArray, extend = util.extend,
concatArgs = function(arr, args)
{
return arr.concat(makeArray(args));
Expand Down Expand Up @@ -48,7 +48,7 @@ var clss = exports.Class = function()
}
};

util.extend(
extend(
clss,
{
callback : function(funcs)
Expand Down Expand Up @@ -116,7 +116,7 @@ util.extend(
},

setup: function( baseClass, fullName ) {
this.defaults = util.extend(true, {}, baseClass.defaults, this.defaults);
this.defaults = extend(true, {}, baseClass.defaults, this.defaults);
return arguments;
},

Expand All @@ -127,24 +127,14 @@ util.extend(
return inst;
},

getObject : util.getObject,

extend: function( fullName, klass, proto ) {
// figure out what was passed
if ( typeof fullName != 'string' ) {
proto = klass;
klass = fullName;
fullName = null;
}
if (!proto ) {
extend: function(klass, proto) {
if(!proto) {
proto = klass;
klass = null;
}

proto = proto || {};
var _super_class = this,
_super = this.prototype,
name, shortName, namespace, prototype;
_super = this.prototype, prototype;

// Instantiate a base class (but only create the instance,
// don't run the init constructor)
Expand All @@ -168,35 +158,18 @@ util.extend(
}
// Copy old stuff onto class
for ( name in this ) {
if ( this.hasOwnProperty(name) && ['prototype', 'defaults', 'getObject'].indexOf(name) == -1 ) {
if ( this.hasOwnProperty(name) && ['prototype', 'defaults'].indexOf(name) == -1 ) {
Class[name] = this[name];
}
}

// do static inheritance
inheritProps(klass, this, Class);

// do namespace stuff
if ( fullName ) {
var parts = fullName.split(/\./),
shortName = parts.pop(),
current = clss.getObject(parts.join('.'), global, true),
namespace = current;

if(current[shortName]){
steal.dev.warn("class.js There's already something called "+fullName);
}

current[shortName] = Class;
}

// set things that can't be overwritten
util.extend(Class, {
extend(Class, {
prototype: prototype,
namespace: namespace,
shortName: shortName,
constructor: Class,
fullName: fullName
constructor: Class
});

//make sure our prototype looks nice
Expand All @@ -214,7 +187,7 @@ util.extend(
});

clss.prototype.callback = clss.callback;
exports.extend = function(name, klass, proto)
exports.extend = function()
{
return clss.extend(name, klass, proto);
return clss.extend.apply(clss, arguments);
};
57 changes: 0 additions & 57 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,60 +197,3 @@ var extend = exports.extend = function()
// Return the modified object
return target;
};

/**
* @see https://github.com/jupiterjs/jquerymx/blob/master/lang/lang.js
* @param objectName
* @param roots
* @param add
* @returns
*/
var getObject = exports.getObject = function(objectName, roots, add)
{
var isContainer = function(current)
{
var type = typeof current;
return type && (type == 'function' || type == 'object');
}, regs =
{
undHash : /_|-/,
colons : /::/,
words : /([A-Z]+)([A-Z][a-z])/g,
lowUp : /([a-z\d])([A-Z])/g,
dash : /([a-z\d])([A-Z])/g,
replacer : /\{([^\}]+)\}/g,
dot : /\./
}, getNext = function(current, nextPart, add){
return current[nextPart] || ( add && (current[nextPart] = {}) );
};

var parts = objectName ? objectName.split(regs.dot) : [], length = parts.length, currents = isArray(roots) ? roots
: [ roots || globals ], current, ret, i, c = 0, type;

if (length == 0)
{
return currents[0];
}
while (current = currents[c++])
{
for (i = 0; i < length - 1 && isContainer(current); i++)
{
current = getNext(current, parts[i], add);
}
if (isContainer(current))
{

ret = getNext(current, parts[i], add);

if (ret !== undefined)
{

if (add === false)
{
delete current[parts[i]];
}
return ret;
}
}
}
};
File renamed without changes.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "js-class",
"name": "uberclass",
"description": "A class framework based on JavaScriptMVC $.Class and John Resig's Simple JavaScript inheritance.",
"version": "1.0.0",
"homepage": "http://github.com/daffl/js.class",
"homepage": "http://daffl.github.com/uberclass",
"repository": {
"type": "git",
"url": "git://github.com/daffl/js.class.git"
"url": "git://github.com/daffl/uberclass.git"
},
"author": "David Luecke <daff@neyeon.de> (http://neyeon.com)",
"main": "lib/class",
Expand Down
62 changes: 13 additions & 49 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
JS.Class
Uberclass
========

JS.Class is a class framework based on [JavaScriptMVC $.Class](http://javascriptmvc.com/docs.html#&who=jQuery.Class)
Uberclass is a class framework based on [JavaScriptMVC $.Class](http://javascriptmvc.com/docs.html#&who=jQuery.Class)
and [John Resig's Simple JavaScript inheritance](http://ejohn.org/blog/simple-javascript-inheritance/) for NodeJS.
It encourages a hybrid (as made popular by e.g. Scala) approach between functional and object oriented programming.
It encourages a hybrid approach between functional and object oriented programming.

Features:

- Prototypal inheritance
- Static inheritance
- Introspection
- Namespaces
- Setup and initialization methods
- Callback creation
- Easy callback creation


Install and require
-------------------

You can either use npm

npm install js-class
npm install uberclass

Or clone the [github repository](https://github.com/daffl/JS.Class).
Loading the base class is done via require. The base Class object is being
exported as well as an extend convenience method:

var cls = require('js-class');
var Class = cls.Class;
Class.extend(name, static, proto);
// Or the equivalent
cls.extend(name, static, proto);
Or clone the [github repository](https://github.com/daffl/ueberclass).


Creating a Class
Expand All @@ -39,11 +29,9 @@ Creating a Class
The following creates a Monster class, static, and prototype members.
The prototype init is called as the constructor. Every time a monster instance is created, the static count is incremented:

var Class = require('js-class').Class;
var Class = require('ueberclass');

Class.extend('Monster',
/* @static */
{
var Monster = Class.extend(/* @static */ {
count: 0
},
/* @prototype */
Expand Down Expand Up @@ -88,8 +76,7 @@ If you overwrite a function, you can call the base class's function by calling t
Lets create a SeaMonster class. SeaMonsters are less efficient at eating small children, but more powerful fighters.


Monster.extend('SeaMonster',
{
var SeaMonster = Monster.extend({
eat : function(smallChildren)
{
this._super(smallChildren / 2);
Expand All @@ -108,25 +95,6 @@ Lets create a SeaMonster class. SeaMonsters are less efficient at eating small c
lochNess.fight();
console.log("Loch Ness fought. Health: " + lochNess.health); // -> 11


Namespaces and Introspection
----------------------------

Namespaces help avoiding naming conflicts with other code and help you add more structure
to your own:

Class.extend("MyNamespace.MyClass",{},{});
new MyNamespace.MyClass()

Often, it's nice to create classes whose name helps determine functionality.
JavaScript doesn't have a native way of determining an object's name,
so the developer must provide a name.
Class fixes this by taking a String name for the class.

Class.extend("MyOrg.MyClass",{},{})
MyOrg.MyClass.shortName //-> 'MyClass'
MyOrg.MyClass.fullName //-> 'MyOrg.MyClas


Callbacks
---------
Expand All @@ -136,9 +104,7 @@ The following example creates a ResponseHandler class that takes the reponse tex
and provides it's handle method as a callback to the http.createServer function:


Class.extend('Http.Response.Handler', {
/* Static */
}, {
var Handler = Class.extend({
init : function(content, headers)
{
this._headers = headers;
Expand All @@ -158,7 +124,7 @@ and provides it's handle method as a callback to the http.createServer function:
}
});

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

var http = require('http');
http.createServer(handler.callback('handle')).listen(1337, "127.0.0.1");
Expand All @@ -167,10 +133,8 @@ and provides it's handle method as a callback to the http.createServer function:
Exporting
---------

Classes are added to the global scope, so they are available everywhere as soon as
the module defining it has been loaded once using require(). I recommend
using the name of the module as the top level namespace:
Just add the class object to your module export:

// my_module.js
Class.extend("MyModule.MyClass",{},{});
module.exports.MyClass = Class.extend({ /* Static */ }, { /* Prototype */ });

Loading

0 comments on commit 6a96d8c

Please sign in to comment.