art-config
Version:
A powerful yet simple tool for configuring all your libraries consistently.
184 lines (153 loc) • 5.33 kB
JavaScript
// Generated by CoffeeScript 1.12.7
(function() {
var BaseClass, ConfigRegistry, Configurable, ErrorWithInfo, EventedMixin, deepMerge, defineModule, isPlainObject, log, merge, mergeInto, namespace, neq, ref,
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,
slice = [].slice;
ref = require('art-standard-lib'), defineModule = ref.defineModule, log = ref.log, merge = ref.merge, isPlainObject = ref.isPlainObject, mergeInto = ref.mergeInto, deepMerge = ref.deepMerge, isPlainObject = ref.isPlainObject, ErrorWithInfo = ref.ErrorWithInfo, neq = ref.neq;
BaseClass = require('art-class-system').BaseClass;
namespace = require('./namespace');
ConfigRegistry = require('./ConfigRegistry');
EventedMixin = require('art-events').EventedMixin;
/*
TO USE:
1) Inherit from Configurable and
2) OPTIONAL: call @defaults to set configuration defaults
3) OPTIONAL, override one of:
@configure
@preprocessConfig
@configured
*/
defineModule(module, Configurable = (function(superClass) {
extend(Configurable, superClass);
function Configurable() {
return Configurable.__super__.constructor.apply(this, arguments);
}
Configurable.abstractClass();
Configurable.declarable({
defaults: {}
});
Configurable.getDefaultConfig = function() {
return this.getDefaults();
};
Configurable.extendConfig = function() {
if (this.hasOwnProperty("config")) {
return this.config;
} else {
return this.config = {};
}
};
Configurable.reset = function() {
var config, defaults, k, ref1, v;
defaults = this.getDefaults();
config = this.extendConfig();
for (k in config) {
v = config[k];
if (defaults[k] == null) {
delete config[k];
}
}
mergeInto(config, defaults);
if (this.namespace !== namespace) {
if ((ref1 = this.namespace) != null) {
ref1.config || (ref1.config = config);
}
}
return config;
};
Configurable.getInspectedObjects = function() {
var obj;
return (
obj = {},
obj["" + (this.getConfigurationPathString())] = this.config,
obj
);
};
Configurable.getPathedDefaultConfig = function() {
var obj;
return (
obj = {},
obj["" + (this.getConfigurationPathString())] = this.getDefaults(),
obj
);
};
Configurable.configure = function(globalConfig) {
var obj;
globalConfig.verbose && log({
Configurable: (
obj = {},
obj["" + (this.getConfigurationPathString())] = this.getConfigurationFromPath(globalConfig),
obj
)
});
return mergeInto(this.reset(), this.getConfigurationFromPath(globalConfig));
};
Configurable.getConfigSave = function() {
var count, defaults, k, obj, out, ref1, ref2, v;
out = {};
defaults = this.getDefaults();
count = 0;
ref2 = (ref1 = this.config) != null ? ref1 : {};
for (k in ref2) {
v = ref2[k];
if (!(neq(v, defaults[k]))) {
continue;
}
count++;
out[k] = v;
}
if (count > 0) {
return (
obj = {},
obj["" + (this.getConfigurationPathString())] = out,
obj
);
}
};
Configurable.on = function() {
var a, ref1;
a = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return (ref1 = this.getSingleton()).on.apply(ref1, a);
};
Configurable.configured = function() {
return this.getSingleton().handleEvent("configured", {
config: this.config
});
};
Configurable.getConfigurationPath = function() {
var _Configurable, _Neptune, i, path, ref1;
ref1 = this.getNamespacePath().split('.'), _Neptune = ref1[0], path = 3 <= ref1.length ? slice.call(ref1, 1, i = ref1.length - 1) : (i = 1, []), _Configurable = ref1[i++];
return path;
};
Configurable.getConfigurationPathString = function() {
return this.getConfigurationPath().join('.');
};
Configurable.getConfigurationFromPath = function(config, path) {
var el, i, len;
if (path == null) {
path = this.getConfigurationPath();
}
for (i = 0, len = path.length; i < len; i++) {
el = path[i];
config = config != null ? config[el] : void 0;
}
return config;
};
Configurable._register = function() {
this.reset();
return ConfigRegistry.registerConfigurable(this);
};
Configurable.postCreateConcreteClass = function(arg) {
var hotReloaded;
hotReloaded = arg.hotReloaded;
if (hotReloaded) {
ConfigRegistry.reload();
} else {
this._register();
}
return Configurable.__super__.constructor.postCreateConcreteClass.apply(this, arguments);
};
return Configurable;
})(EventedMixin(BaseClass)));
}).call(this);
//# sourceMappingURL=Configurable.js.map