[ADD] Registry object to record extensible mappings of objects (e.g. views or widgets)
bzr revid: xmo@openerp.com-20110331102023-iovd7s7ktk6zgb45
This commit is contained in:
@@ -53,6 +53,103 @@ openerp.base.callback = function(obj, method) {
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Base error for lookup failure
|
||||
*
|
||||
* @class
|
||||
*/
|
||||
openerp.base.NotFound = Class.extend(
|
||||
/** @lends openerp.base.NotFound# */ {
|
||||
});
|
||||
openerp.base.KeyNotFound = openerp.base.NotFound.extend(
|
||||
/** @lends openerp.base.KeyNotFound# */ {
|
||||
/**
|
||||
* Thrown when a key could not be found in a mapping
|
||||
*
|
||||
* @constructs
|
||||
* @extends openerp.base.NotFound
|
||||
* @param {String} key the key which could not be found
|
||||
*/
|
||||
init: function (key) {
|
||||
self.key = key;
|
||||
}
|
||||
});
|
||||
openerp.base.ObjectNotFound = openerp.base.NotFound.extend(
|
||||
/** @lends openerp.base.ObjectNotFound# */ {
|
||||
/**
|
||||
* Thrown when an object path does not designate a valid class or object
|
||||
* in the openerp hierarchy.
|
||||
*
|
||||
* @constructs
|
||||
* @extends openerp.base.NotFound
|
||||
* @param {String} path the invalid object path
|
||||
*/
|
||||
init: function (path) {
|
||||
self.path = path;
|
||||
}
|
||||
});
|
||||
openerp.base.Registry = Class.extend(
|
||||
/** @lends openerp.base.Registry# */ {
|
||||
/**
|
||||
* Stores a mapping of arbitrary key (strings) to object paths (as strings
|
||||
* as well).
|
||||
*
|
||||
* Resolves those paths at query time in order to always fetch the correct
|
||||
* object, even if those objects have been overloaded/replaced after the
|
||||
* registry was created.
|
||||
*
|
||||
* An object path is simply a dotted name from the openerp root to the
|
||||
* object pointed to (e.g. ``"openerp.base.Session"`` for an OpenERP
|
||||
* session object).
|
||||
*
|
||||
* @constructs
|
||||
* @param {Object} mapping a mapping of keys to object-paths
|
||||
*/
|
||||
init: function (mapping) {
|
||||
this.map = mapping || {};
|
||||
},
|
||||
/**
|
||||
* Retrieves the object matching the provided key string.
|
||||
*
|
||||
* @param {String} key the key to fetch the object for
|
||||
* @returns {Class} the stored class, to initialize
|
||||
*
|
||||
* @throws {openerp.base.KeyNotFound} if the object was not in the mapping
|
||||
* @throws {openerp.base.ObjectNotFound} if the object path was invalid
|
||||
*/
|
||||
get_object: function (key) {
|
||||
var path_string = this.map[key];
|
||||
if (path_string === undefined) {
|
||||
throw new openerp.base.KeyNotFound(key);
|
||||
}
|
||||
|
||||
var object_match = openerp;
|
||||
var path = path_string.split('.');
|
||||
// ignore first section
|
||||
for(var i=1; i<path.length; ++i) {
|
||||
object_match = object_match[path[i]];
|
||||
|
||||
if (object_match === undefined) {
|
||||
throw new openerp.base.ObjectNotFound(path_string);
|
||||
}
|
||||
}
|
||||
return object_match;
|
||||
},
|
||||
/**
|
||||
* Adds a new key and value to the registry.
|
||||
*
|
||||
* This method can be chained.
|
||||
*
|
||||
* @param {String} key
|
||||
* @param {String} object_path fully qualified dotted object path
|
||||
* @returns {openerp.base.Registry} itself
|
||||
*/
|
||||
add: function (key, object_path) {
|
||||
this.map[key] = object_path;
|
||||
return this;
|
||||
}
|
||||
});
|
||||
|
||||
openerp.base.BasicController = Class.extend(
|
||||
/** @lends openerp.base.BasicController# */{
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
$(document).ready(function () {
|
||||
var openerp;
|
||||
module('Registry', {
|
||||
setup: function () {
|
||||
openerp = window.openerp.init(true);
|
||||
window.openerp.base.chrome(openerp);
|
||||
openerp.base.Foo = {};
|
||||
openerp.base.Bar = {};
|
||||
}
|
||||
});
|
||||
test('key fetch', function () {
|
||||
var reg = new openerp.base.Registry({
|
||||
foo: 'openerp.base.Foo',
|
||||
bar: 'openerp.base.Bar',
|
||||
quux: 'openerp.base.Quux'
|
||||
});
|
||||
|
||||
strictEqual(reg.get_object('foo'), openerp.base.Foo);
|
||||
raises(function () { reg.get_object('qux'); },
|
||||
openerp.base.KeyNotFound,
|
||||
"Unknown keys should raise KeyNotFound");
|
||||
raises(function () { reg.get_object('quux'); },
|
||||
openerp.base.ObjectNotFound,
|
||||
"Incorrect file paths should raise ObjectNotFound");
|
||||
});
|
||||
test('key set', function () {
|
||||
var reg = new openerp.base.Registry();
|
||||
|
||||
reg.add('foo', 'openerp.base.Foo')
|
||||
.add('bar', 'openerp.base.Bar');
|
||||
strictEqual(reg.get_object('bar'), openerp.base.Bar);
|
||||
});
|
||||
});
|
||||
@@ -26,4 +26,5 @@
|
||||
<ol id="qunit-tests"></ol>
|
||||
</body>
|
||||
<script type="text/javascript" src="/base/static/test/dataset.js"></script>
|
||||
<script type="text/javascript" src="/base/static/test/registry.js"></script>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user