Skip to content

Instantly share code, notes, and snippets.

@medikoo
Created October 8, 2014 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save medikoo/24266a50b48b1a4b145d to your computer and use it in GitHub Desktop.
Save medikoo/24266a50b48b1a4b145d to your computer and use it in GitHub Desktop.
Populator
var generateObject, generateSection, generateProperty, serialize;
serialize = function (id, value, stamp) {
// Add to updates
};
generateObject = function (meta/*, options*/) {
var id = genId(), options = Object(arguments[1])
, stamp = options.stamp ? options.stamp : now();
serialize(id, validValue(meta.id), stamp);
validArray(meta.sections).forEach(function (section, index) {
stamp = generateSection(id, section, index ? null : { stamp: stamp + 1 });
});
return { id: id, stamp: stamp }
};
generateSection = function (objId, meta/*, options*/) {
var options = Object(arguments[2])
, stamp = options.stamp ? options.stamp : now();
forEach(validObject(value), function (meta, sKey) {
stamp = generateProperty(objId, sKey, meta, { stamp: stamp + 1 });
});
return stamp;
};
generateProperty = function (objId, sKey, meta/*, options*/) {
var options = Object(arguments[3]), objMeta;
, stamp = options.stamp ? options.stamp : now()
, value, count;
if (meta.multiple) {
if (isArray(meta.value)) {
meta.value.forEach(function (value) {
serialize(objId + '/' + sKey + '*' + serializeKey(value), '11', stamp++);
});
return stamp;
}
count = toPosInt(meta.min) || 1;
value = [];
if (isFunction(value.get)) {
while (count--) {
serialize(objId + '/' + sKey + '*' + serializeKey(value.get), '11', stamp++);
}
return stamp;
}
validObject(value.value);
while (count--) {
objMeta = generateObject(value.value, { stamp: stamp++ });
stamp = objMeta.stamp + 1;
serialize(objId + '/' + sKey + '*7' + objMeta.id, '11', stamp);
}
return stamp;
}
if (meta.value != null) {
serialize(objId + '/' + sKey, serializeValue(meta.value), stamp);
return stamp;
}
if (isFunction(value.get)) {
serialize(objId + '/' + sKey, serializeValue(meta.get()), stamp);
return stamp;
}
validObject(value.value);
objMeta = generateObject(meta.value, { stamp: stamp });
stamp = objMeta.stamp + 1;
serialize(objId + '/' + sKey, '7' + objMeta.id, stamp);
return stamp;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment