Skip to content

Commit

Permalink
* underoop.js: serious amount of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
David Nolen committed May 20, 2010
1 parent a193a71 commit d231f1c
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions underoop.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@
function Module() {}; function Module() {};


_.mixin({ _.mixin({
// check if an object is using a module
includes: function(x, module) {
return _(x.includes).indexOf(module) != -1;
},

// check if an object is a Module
isModule: function() {
return x instanceof Module;
},

// create grouping of functions. Can be used with classes // create grouping of functions. Can be used with classes
// to combine functionality. // to combine functionality.
Module: function(obj) { Module: function(obj) {
obj.prototype = new Module; var m = new Module;
obj.toString = function() { m._module = true;
_(obj).each(function(v, k) {
m[k] = v;
});
m.toString = function() {
return ["<Module: ", (obj.name || _uniqueId("UnnamedModule")), ">"].join(""); return ["<Module: ", (obj.name || _uniqueId("UnnamedModule")), ">"].join("");
}; };
return obj; return m;
}, },


// check if an instance derives from class // check if an object is a Module
isMember: function(x, _class) { isModule: function(x) {
return x instanceof _class; return x._module === true;
}, },


// check if the instance has the same class // check if an object is using a module
isInstance: function(x, _class) { includes: function(x, module) {
return x._class = _class; return _(module).isModule() && _(x.includes).indexOf(module) != -1;
}, },


// creates a class. does not support inheritance because inheritance // creates a class. does not support inheritance because inheritance
Expand All @@ -53,7 +47,7 @@
var modules = obj.includes || [], var modules = obj.includes || [],
name = obj.name = (obj.name || _.uniqueId("UnnamedClass")), name = obj.name = (obj.name || _.uniqueId("UnnamedClass")),
methodMap = modules.map(function(x) { methodMap = modules.map(function(x) {
x = _(x).clone(); delete x.name; delete x.toString; return x; x = _(x).clone(); delete x.name; delete x.toString; delete x._module; return x;
}); });
obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) { obj =_.reduce(methodMap.concat(obj), {}, function(memo, m) {
return _.extend(memo, m); return _.extend(memo, m);
Expand All @@ -65,13 +59,13 @@
}); });


klass._name = name; klass._name = name;
klass.prototype = new Class(); klass._class = true;
klass.prototype._class = klass; klass._modules = klass._modules = modules;
klass.prototype._modules = klass._modules = modules; klass.prototype = new Class;


_(obj).each(function(v, k) { _(obj).each(function(v, k) {
klass.prototype[k] = v; klass.prototype[k] = v;
if(_.isUndefined(_sel[k])) { if(_.isFunction(v) && _.isUndefined(_sel[k])) {
_sel[k] = function() { _sel[k] = function() {
var args = arguments; var args = arguments;
return function(x) { return function(x) {
Expand All @@ -82,6 +76,10 @@
}); });


return klass; return klass;
},

isClass: function(x) {
return x._class === true;
} }
}); });


Expand Down

0 comments on commit d231f1c

Please sign in to comment.