From 312fcea5f18871536773ed040e1b4739ca27d398 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 15 May 2015 18:08:21 +0200 Subject: [PATCH] Auto links. --- packages/xo-lib/xo.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/xo-lib/xo.js b/packages/xo-lib/xo.js index 8e5cc1556..fc8d6b4f3 100644 --- a/packages/xo-lib/xo.js +++ b/packages/xo-lib/xo.js @@ -31,8 +31,31 @@ function makeStandaloneDeferred () { function noop () {} +// ------------------------------------------------------------------- + +var defineProperty = Object.defineProperty + +var LINK_RE = /^(.*)\$link\$$/ + +function createAutoLinks (collection, object) { + forEach(object, function resolveObject (value, key, object) { + var matches = key.match(LINK_RE) + if (!matches) { + return + } + + defineProperty(object, matches[1], { + get: function () { + return collection[value] + } + }) + }) +} + function setMultiple (collection, items) { forEach(items, function (item) { + createAutoLinks(item) + collection.set(item) }) } @@ -90,7 +113,7 @@ function Xo (opts) { } objects.createIndex('ref', new Index('ref')) objects.createIndex('type', new Index('type')) - objects.createIndex('UUID', new Index('UUID')) + objects.createIndex('uuid', new Index('uuid')) this.status = 'disconnected' this.user = null