Created
January 7, 2010 18:51
-
-
Save subtleGradient/271453 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*= | |
| name: Class | |
| description: Code with Class | |
| requires: Accessor | |
| =*/ | |
| MooTools.define(function(){ | |
| var Class = this.Class = new Type('Class', function(params){ | |
| if (instanceOf(params, Function)) params = {'initialize': params}; | |
| var newClass = function(){ | |
| reset(this); | |
| if (newClass.$prototyping) return this; | |
| this.$caller = null; | |
| var value = (this.initialize) ? this.initialize.apply(this, arguments) : this; | |
| this.$caller = this.caller = null; | |
| return value; | |
| }.extend(this); | |
| newClass.implement(params); | |
| newClass.constructor = Class; | |
| newClass.prototype.constructor = newClass; | |
| newClass.prototype.parent = parent; | |
| return newClass; | |
| }); | |
| var parent = function(){ | |
| if (!this.$caller) throw new Error('The method "parent" cannot be called.'); | |
| var name = this.$caller.$name, parent = this.$caller.$owner.parent; | |
| var previous = (parent) ? parent.prototype[name] : null; | |
| if (!previous) throw new Error('The method "' + name + '" has no parent.'); | |
| return previous.apply(this, arguments); | |
| }; | |
| var reset = function(object){ | |
| for (var key in object){ | |
| var value = object[key]; | |
| switch (typeOf(value)){ | |
| case 'object': | |
| var F = function(){}; | |
| F.prototype = value; | |
| var instance = new F; | |
| object[key] = reset(instance); | |
| break; | |
| case 'array': object[key] = value.clone(); break; | |
| } | |
| } | |
| return object; | |
| }; | |
| var wrap = function(self, key, method){ | |
| if (method.$origin) method = method.$origin; | |
| return function(){ | |
| if (method.$protected && this.$caller == null) throw new Error('The method "' + key + '" cannot be called.'); | |
| var caller = this.caller, current = this.$caller; | |
| this.caller = current; this.$caller = arguments.callee; | |
| var result = method.apply(this, arguments); | |
| this.$caller = current; this.caller = caller; | |
| return result; | |
| }.extend({$owner: self, $origin: method, $name: key}); | |
| }; | |
| Class.extend(new Accessor('Mutator')); | |
| var implement = function(key, value, retain){ | |
| var mutator = Class.matchMutator(key) || Class.lookupMutator(key); | |
| if (mutator){ | |
| value = mutator.call(this, value); | |
| if (value == null) return this; | |
| } | |
| if (typeOf(value) == 'function'){ | |
| if (value.$hidden) return this; | |
| this.prototype[key] = (retain) ? value : wrap(this, key, value); | |
| } else { | |
| Object.merge(this.prototype, key, value); | |
| } | |
| return this; | |
| }; | |
| var getInstance = function(klass){ | |
| klass.$prototyping = true; | |
| var proto = new klass; | |
| delete klass.$prototyping; | |
| return proto; | |
| }; | |
| Class.implement('implement', (function(object){ | |
| for (var key in object) implement.call(this, key, object[key]); | |
| return this; | |
| }).overload(Function.overloadPair)); | |
| Class.defineMutators({ | |
| Extends: function(parent){ | |
| this.parent = parent; | |
| this.prototype = getInstance(parent); | |
| }, | |
| Implements: function(items){ | |
| Array.from(items).each(function(item){ | |
| var instance = new item; | |
| for (var key in instance) implement.call(this, key, instance[key], true); | |
| }, this); | |
| } | |
| }); | |
| Class.defineMutator(/^protected\s(\w+)/, function(fn, name){ | |
| implement.call(this, name, fn.protect()); | |
| }); | |
| Class.defineMutator(/^linked\s(\w+)/, function(value, name){ | |
| this.prototype[name] = value; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment