/* Authors: * Petr Vobornik * * Copyright (C) 2013 Red Hat * see file 'COPYING' for use and warranty information * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ define(['dojo/_base/declare', 'dojo/_base/lang' ], function(declare, lang) { var Spec_mod = declare(null, { /** * Modifies spec according to rules defined in diff object. * * Diff should have following structure: { * $add: array of add rules * $del: array of del rules * $set: array of set rules * } * * The order of modification is del, add, set. * * @param {Object} spec * @param {Object} diff */ mod: function(spec, diff) { if (!diff) return spec; this.del(spec, diff.$del); this.add(spec, diff.$add); this.set(spec, diff.$set); return spec; }, /** * Adds objects according to rules to array. * * A rule is a triple of path and a object and position to add: * ['path.to.spec.array', {}, position] * */ add: function(spec, rules) { return this._apply_rules(spec, rules, this._add); }, /** * Deletes objects according to rules from an array. * * A rule is a pair of path and delete conditions: * ['path.to.spec.array', [ { name: 'foo'}, { name: 'baz'} ]] * * Deletes all objects with name 'baz' or 'foo'. */ del: function(spec, rules) { return this._apply_rules(spec, rules, this._del); }, /** * A rule is a pair of path and a object to set. * ['path.to.spec.property', {}] */ set: function(spec, rules) { return this._apply_rules(spec, rules, this._set); }, /** * Removes all rule props */ del_rules: function(diff) { delete diff.$add; delete diff.$del; delete diff.$set; }, _apply_rules: function(spec, rules, method) { if (!lang.isArrayLike(rules)) return spec; for (var i=0; i arr.length) pos = arr.length; arr.splice(pos, 0, value); return spec; }, _del: function(spec, rule) { var path = rule[0]; var conds = rule[1]; var arr = lang.getObject(path, false, spec); if (!arr) return spec; var del = []; var i,j; for (i=0; i