Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge data snapshots #27

Merged
merged 4 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';

var RuleDataSnapshot = require('./rule-data-snapshot');

exports.pathSplitter = function(path) {

if (path[0] === '/') {
path = path.substr(1);
}

return path.split('/');
};

exports.makeNewDataSnap = function(path, newData) {

path = path.replace(/^\/+/, '');
newData = RuleDataSnapshot.convert(newData)

var result;

if (path.length === 0) {
result = newData;
} else {

var workObject = {};
result = workObject;

exports.pathSplitter(path).forEach(function(pathPart, i, pathParts) {

if (pathParts.length - i === 1) {
workObject[pathPart] = newData;
} else {
workObject[pathPart] = {};
}
workObject = workObject[pathPart];

});

}

return new RuleDataSnapshot(result);
};
9 changes: 9 additions & 0 deletions lib/rule-data-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ RuleDataSnapshot.convert = function(data) {
};


RuleDataSnapshot.prototype.merge = function(other) {
if (this._path || other._path) {
throw new Error('can only merge top-level RuleDataSnapshots');
}

var data = extend(true, {}, this._data, other._data);
return new RuleDataSnapshot(data);
}

RuleDataSnapshot.prototype._getVal = function() {

var pathParts;
Expand Down
49 changes: 5 additions & 44 deletions lib/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

var extend = require('extend'),
Rule = require('./parser/rule'),
RuleDataSnapshot = require('./rule-data-snapshot');
RuleDataSnapshot = require('./rule-data-snapshot'),
pathSplitter = require('./helpers').pathSplitter,
makeNewDataSnap = require('./helpers').makeNewDataSnap;

var validRuleKinds = {
'.read': true,
Expand All @@ -20,47 +22,6 @@ function ruleError(rulePath, message) {

}

function pathSplitter(path)
{

if (path[0] === '/') {
path = path.substr(1);
}

return path.split('/');

}

function makeNewDataSnap(path, newData) {

path = path.replace(/^\/+/, '');
var result;

if (path.length === 0) {
result = newData;
} else {

var workObject = {};
result = workObject;

pathSplitter(path).forEach(function(pathPart, i, pathParts) {

if (pathParts.length - i === 1) {
workObject[pathPart] = newData;
} else {
workObject[pathPart] = {};
}
workObject = workObject[pathPart];

});

}

return new RuleDataSnapshot(result);

}


function Ruleset(rulesDefinition) {

if (typeof rulesDefinition !== 'object' ||
Expand Down Expand Up @@ -153,7 +114,6 @@ function Ruleset(rulesDefinition) {

}


Ruleset.prototype.get = function(path, kind) {

var rules = [];
Expand Down Expand Up @@ -278,7 +238,8 @@ Ruleset.prototype.tryWrite = function(path, root, newData, auth, skipWrite, skip
// write encompasses both the cascading write rules and the
// non-cascading validate rules

var newDataRoot = makeNewDataSnap(path, RuleDataSnapshot.convert(newData));
var newDataRoot = makeNewDataSnap(path, newData);
newDataRoot = root.merge(newDataRoot)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put these on one line, like so:

var newDataRoot = root.merge(makeNewDataSnap(path, newData));


var result = {
path: path,
Expand Down
5 changes: 4 additions & 1 deletion lib/test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
var root, rules,
debug = true,
Ruleset = require('./ruleset'),
RuleDataSnapshot = require('./rule-data-snapshot');
RuleDataSnapshot = require('./rule-data-snapshot'),
makeNewDataSnap = require('./helpers').makeNewDataSnap;

var userDefinitions = exports.userDefinitions = {

Expand Down Expand Up @@ -165,3 +166,5 @@ exports.setFirebaseRules = function(ruleDefinition) {
exports.getFirebaseRules = function() {
return rules;
};

exports.makeNewDataSnap = makeNewDataSnap;