/*jslint node: true, nomen: true */
"use strict";

var _ = require('underscore');

function getObjValue(field, data) {
    return _.reduce(field.split("."), function (obj, f) {
        if (obj) {
            return obj[f];
        }
    }, data);
}

function setObjValue(field, data, value) {
    var fieldArr = field.split('.');
    return _.reduce(fieldArr, function (o, f, i) {
        if (i === fieldArr.length - 1) {
            o[f] = value;
        } else {
            if (!o[f]) {
                o[f] = {};
            }
        }
        return o[f];
    }, data);
}

exports.updateDocument = function (doc, SchemaTarget, data) {
    var newValue,
        field;
    for (field in SchemaTarget.schema.paths) {
        if (SchemaTarget.schema.paths.hasOwnProperty(field)) {
            if ((field !== '_id') && (field !== '__v')) {
                newValue = getObjValue(field, data);
            }
        }
    }
    return doc;
};
                if (newValue !== undefined) {
                    setObjValue(field, doc, newValue);
                }