diff options
author | Kris Maglione <maglione.k@gmail.com> | 2011-03-13 18:21:13 -0400 |
---|---|---|
committer | Kris Maglione <maglione.k@gmail.com> | 2011-03-13 18:21:13 -0400 |
commit | bbb5f4e5419c799e938125c02fd373774c3cae1f (patch) | |
tree | acaaa19d0844eaa8da3e6c6f2968933ce7c4f40e /common/modules/options.jsm | |
parent | fdc37532d7a143df528c5051180960320f036796 (diff) | |
download | pentadactyl-bbb5f4e5419c799e938125c02fd373774c3cae1f.tar.gz |
Add separate classes for option types.
Diffstat (limited to 'common/modules/options.jsm')
-rw-r--r-- | common/modules/options.jsm | 67 |
1 files changed, 44 insertions, 23 deletions
diff --git a/common/modules/options.jsm b/common/modules/options.jsm index baa570a7..3809937e 100644 --- a/common/modules/options.jsm +++ b/common/modules/options.jsm @@ -44,30 +44,13 @@ let ValueError = Class("ValueError", ErrorBase); * @private */ var Option = Class("Option", { - init: function init(names, description, type, defaultValue, extraInfo) { + init: function init(modules, names, description, defaultValue, extraInfo) { + this.modules = modules; this.name = names[0]; this.names = names; this.realNames = names; - this.type = type; this.description = description; - if (this.type in Option.getKey) - this.getKey = Option.getKey[this.type]; - - if (this.type in Option.parse) - this.parse = Option.parse[this.type]; - - if (this.type in Option.stringify) - this.stringify = Option.stringify[this.type]; - - if (this.type in Option.domains) - this.domains = Option.domains[this.type]; - - if (this.type in Option.testValues) - this.testValues = Option.testValues[this.type]; - - this._op = Option.ops[this.type]; - // Need to trigger setter if (extraInfo && "values" in extraInfo && !extraInfo.__lookupGetter__("values")) { this.values = extraInfo.values; @@ -81,7 +64,7 @@ var Option = Class("Option", { defaultValue = this.modules.config.defaults[this.name]; if (defaultValue !== undefined) { - if (this.type == "string") + if (this == "string") defaultValue = Commands.quote(defaultValue); if (isObject(defaultValue)) @@ -711,9 +694,48 @@ var Option = Class("Option", { return Array.concat(values).every(function (re) set.has(acceptable, re.result)); return Array.concat(values).every(set.has(acceptable)); - } + }, + + types: {} }); +["Boolean", + "Charlist", + "Number", + "RegexpList", + "RegexpMap", + "SiteList", + "SiteMap", + "String", + "StringList", + "StringMap"].forEach(function (name) { + let type = name.toLowerCase(); + let class_ = Class(name + "Option", Option, { + type: type, + + _op: Option.ops[type] + }) + + if (type in Option.getKey) + class_.prototype.getKey = Option.getKey[type]; + + if (type in Option.parse) + class_.prototype.parse = Option.parse[type]; + + if (type in Option.stringify) + class_.prototype.stringify = Option.stringify[type]; + + if (type in Option.domains) + class_.prototype.domains = Option.domains[type]; + + if (type in Option.testValues) + class_.prototype.testValues = Option.testValues[type]; + + Option.types[type] = class_; + this[class_.className] = class_; + EXPORTED_SYMBOLS.push(class_.className); +}, this); + /** * @instance options */ @@ -724,7 +746,6 @@ var Options = Module("options", { this.needInit = []; this._options = []; this._optionMap = {}; - this.Option = Class("Option", Option, { modules: modules }); storage.newMap("options", { store: false }); storage.addObserver("options", function optionObserver(key, event, option) { @@ -813,7 +834,7 @@ var Options = Module("options", { let closure = function () self._optionMap[name]; - memoize(this._optionMap, name, function () self.Option(names, description, type, defaultValue, extraInfo)); + memoize(this._optionMap, name, function () Option.types[type](modules, names, description, defaultValue, extraInfo)); for (let alias in values(names.slice(1))) memoize(this._optionMap, alias, closure); |