Last active
October 9, 2017 22:34
-
-
Save wallabra/e2612ab95edc1be6fd9a948217f2145f to your computer and use it in GitHub Desktop.
Abstract class library for Node.JS
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
| // Generated by CoffeeScript 1.12.6 | |
| var AbstractClassError, AbstractFunction, AbstractionError, BadInheritanceError, abstractClass, | |
| bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, | |
| extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | |
| hasProp = {}.hasOwnProperty, | |
| indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
| AbstractFunction = (function() { | |
| function AbstractFunction(name) { | |
| this.name = name; | |
| this.check = bind(this.check, this); | |
| } | |
| AbstractFunction.prototype.check = function(func) { | |
| return func.constructor.name === this.name; | |
| }; | |
| return AbstractFunction; | |
| })(); | |
| AbstractionError = (function(superClass) { | |
| extend(AbstractionError, superClass); | |
| function AbstractionError() { | |
| return AbstractionError.__super__.constructor.apply(this, arguments); | |
| } | |
| return AbstractionError; | |
| })(Error); | |
| AbstractClassError = (function(superClass) { | |
| extend(AbstractClassError, superClass); | |
| function AbstractClassError() { | |
| return AbstractClassError.__super__.constructor.apply(this, arguments); | |
| } | |
| return AbstractClassError; | |
| })(AbstractionError); | |
| BadInheritanceError = (function(superClass) { | |
| extend(BadInheritanceError, superClass); | |
| function BadInheritanceError() { | |
| return BadInheritanceError.__super__.constructor.apply(this, arguments); | |
| } | |
| return BadInheritanceError; | |
| })(AbstractionError); | |
| abstractClass = function(cls, onApply) { | |
| var AbstractedClass, k, v; | |
| AbstractedClass = (function() { | |
| function AbstractedClass() { | |
| throw new AbstractClassError("Can't instantiate abstract classes!"); | |
| } | |
| AbstractedClass.prototype.apply = function(otherClass) { | |
| var k, missingFuncs, ref, v; | |
| missingFuncs = []; | |
| ref = cls.prototype; | |
| for (k in ref) { | |
| v = ref[k]; | |
| if (v instanceof AbstractFunction && (indexOf.call(Object.keys(otherClass.prototype), k) < 0 || !v.check(otherClass.prototype[k]))) { | |
| missingFuncs.push(k); | |
| } else if (indexOf.call(Object.keys(otherClass.prototype), k) < 0) { | |
| otherClass.prototype[k] = v; | |
| } | |
| } | |
| if (missingFuncs.length > 0) { | |
| throw new BadInheritanceError("Following functions missing in the class definition for " + otherClass.name + ", which inherits " + cls.name + ": " + (missingFuncs.join(" "))); | |
| } | |
| if (onApply != null) { | |
| return onApply(otherClass); | |
| } | |
| }; | |
| return AbstractedClass; | |
| })(); | |
| for (k in AbstractedClass) { | |
| v = AbstractedClass[k]; | |
| if (v === null && k.startsWith("F_") && k.length > 2) { | |
| AbstractedClass[k.slice(2)] = new AbstractFunction(k.slice(2)); | |
| delete AbstractedClass[k]; | |
| } | |
| } | |
| if (AbstractedClass.__absInheritance__ != null) { | |
| AbstractedClass.__absInheritance__.push(cls); | |
| } else { | |
| AbstractedClass.__absInheritance__ = [cls]; | |
| } | |
| AbstractedClass.name = cls.name + "_Abstracted"; | |
| return AbstractedClass; | |
| }; | |
| module.exports = { | |
| abstractClass: abstractClass, | |
| AbstractFunction: AbstractFunction, | |
| AbstractClassError: AbstractClassError, | |
| BadInheritanceError: BadInheritanceError | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment