Skip to content

Commit

Permalink
Changing Module so that it provides included() and extended() hooks, …
Browse files Browse the repository at this point in the history
…so that modules can define class methods as well as instance methods. Made exclusion of methods include(), included(), extend() and extended() consistent between instance and class methods, and changed Forwardable to reflect these changes.
  • Loading branch information
jcoglan committed Mar 11, 2008
1 parent 53c4b02 commit 6f784ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
13 changes: 7 additions & 6 deletions source/class.js
Expand Up @@ -32,7 +32,8 @@ JS = {
}
};

(function(list, JS, extend, INSTANCE_METHODS, CLASS_METHODS, prototype, superclass, subclasses, addMethod, method, lambda) {
(function(list, JS, extend, INSTANCE_METHODS, CLASS_METHODS,
prototype, superclass, subclasses, addMethod, excluded, method, lambda) {

var Class = JS.Class = function() {
var args = list(arguments), arg,
Expand Down Expand Up @@ -161,7 +162,7 @@ JS = {
this.extend(modules[i], overwrite);
}
for (var method in source) {
!/^(?:included?|extend)$/.test(method) &&
!excluded.test(method) &&
this.instanceMethod(method, source[method], overwrite);
}
lambda(source.included) && source.included(this);
Expand All @@ -177,7 +178,7 @@ JS = {
extend: function(source, overwrite) {
lambda(source) && (source = Class.properties(source));
for (var method in source) {
source.hasOwnProperty(method) && method != 'extended' &&
source.hasOwnProperty(method) && !excluded.test(method) &&
this.classMethod(method, source[method], overwrite);
}
lambda(source.extended) && source.extended(this);
Expand Down Expand Up @@ -225,9 +226,8 @@ JS = {

Module: function(source) {
return {
included: function(klass) {
klass.include(source);
}
included: function(klass) { klass.include(source); },
extended: function(klass) { klass.extend(source); }
};
}
});
Expand All @@ -244,6 +244,7 @@ JS = {
JS, JS.extend,
'INSTANCE_METHODS', 'CLASS_METHODS',
'prototype', 'superclass', 'subclasses', 'addMethod',
/^(included?|extend(ed)?)$/,

function(name) {
var self = this, cache = self._methods = self._methods || {};
Expand Down
35 changes: 15 additions & 20 deletions source/forwardable.js
@@ -1,22 +1,17 @@
JS.Forwardable = {
extended: function(klass) {
klass.extend({

defineDelegator: function(subject, method, alias) {
alias = alias || method;
this.instanceMethod(alias, function() {
var object = this[subject], property = object[method];
return typeof property == 'function'
? property.apply(object, arguments)
: property;
});
},

defineDelegators: function() {
var methods = Array.from(arguments), subject = methods.shift();
for (var i = 0, n = methods.length; i < n; i++)
this.defineDelegator(subject, methods[i]);
}
JS.Forwardable = JS.Module({
defineDelegator: function(subject, method, alias) {
alias = alias || method;
this.instanceMethod(alias, function() {
var object = this[subject], property = object[method];
return typeof property == 'function'
? property.apply(object, arguments)
: property;
});
},

defineDelegators: function() {
var methods = Array.from(arguments), subject = methods.shift();
for (var i = 0, n = methods.length; i < n; i++)
this.defineDelegator(subject, methods[i]);
}
};
});

0 comments on commit 6f784ae

Please sign in to comment.