janus
Version:
the two-faced application library-framework
130 lines (115 loc) • 4.2 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Base, Library, util,
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;
util = require('../util/util');
Base = require('../core/base').Base;
Library = (function(superClass1) {
extend(Library, superClass1);
Library.prototype.isLibrary = true;
function Library() {
Library.__super__.constructor.call(this);
this.bookcase = {};
this.classTypes = [];
}
Library.prototype.register = function(klass, book, options) {
var base, bookId, classShelf, contextShelf, name, ref;
if (options == null) {
options = {};
}
bookId = this._classIdForConstructor(klass);
classShelf = (base = this.bookcase)[bookId] != null ? base[bookId] : base[bookId] = {};
contextShelf = classShelf[name = (ref = options.context) != null ? ref : 'default'] != null ? classShelf[name] : classShelf[name] = [];
contextShelf.push({
book: book,
options: options
});
if (options.priority != null) {
contextShelf.sort(function(a, b) {
var ref1, ref2;
return ((ref1 = b.options.priority) != null ? ref1 : 0) - ((ref2 = a.options.priority) != null ? ref2 : 0);
});
}
};
Library.prototype.get = function(obj, criteria) {
var ref, ref1, result;
if (criteria == null) {
criteria = {};
}
result = (ref = this._get(obj, obj != null ? obj.constructor : void 0, (ref1 = criteria.context) != null ? ref1 : 'default', criteria)) != null ? ref : this._get(obj, obj != null ? obj.constructor : void 0, 'default', criteria);
if (result != null) {
this.emit('got', obj, result, criteria);
} else {
this.emit('missed', obj, criteria);
}
return result != null ? result : null;
};
Library.prototype._get = function(obj, klass, context, criteria) {
var bookId, contextShelf, i, isMatch, k, len, record, ref, ref1, superClass, v;
bookId = (ref = this._classIdForInstance(obj)) != null ? ref : this._classIdForConstructor(klass);
contextShelf = (ref1 = this.bookcase[bookId]) != null ? ref1[context] : void 0;
if (contextShelf != null) {
for (i = 0, len = contextShelf.length; i < len; i++) {
record = contextShelf[i];
isMatch = true;
for (k in criteria) {
v = criteria[k];
if (k !== 'context' && k !== 'priority') {
if (record.options[k] !== v) {
isMatch = false;
break;
}
}
}
if (isMatch === true) {
return record.book;
}
}
}
if (klass != null) {
if ((superClass = util.superClass(klass)) != null) {
return this._get(obj, superClass, context, criteria);
}
}
};
Library.prototype._classIdForConstructor = function(klass) {
var id;
if (klass == null) {
return 'null';
} else if (klass === Number) {
return 'number';
} else if (klass === String) {
return 'string';
} else if (klass === Boolean) {
return 'boolean';
} else {
if (klass.isCase === true) {
klass = klass.type;
}
if ((id = this.classTypes.indexOf(klass)) >= 0) {
return id;
} else {
id = this.classTypes.length;
this.classTypes.push(klass);
return id;
}
}
};
Library.prototype._classIdForInstance = function(obj) {
if (obj == null) {
return 'null';
} else if (util.isNumber(obj)) {
return 'number';
} else if (util.isString(obj)) {
return 'string';
} else if (obj === true || obj === false) {
return 'boolean';
}
};
return Library;
})(Base);
module.exports = {
Library: Library
};
}).call(this);