<commit_before>'use strict';
/**
 * Adds automatic reference updating to a schema.
 *
 * @param {mongoose.Schema} schema The target schema.
 * @param {Function} normalize A function to normalize cases with.
 */

exports.add = function add(schema, normalize) {
  // reference pushing on creation
  schema.pre('save', function(next) {
    if(!this.isNew || !this.context_id) return next();

    var path = getPath(this);
    exports.refUpdate('save', path, this, next);
  });

  // reference pulling on removal
  schema.pre('remove', function(next) {
    if(!this.context_id) return next();

    var path = getPath(this);
    exports.refUpdate('remove', path, this, next);
  });

  // make sure the context is cached
  schema.post('init', exports.cacheContext);
  schema.post('save', exports.cacheContext);

  // reference pushing/pulling on modifications
  schema.pre('save', function(next) {
    if((!this.isModified('context_id') && !this.isModified('context_type'))
      || this.isNew || !this.context_id) return next();

    var _this = this;
    var new_context = {
      context_type: this.context_type,
      context_id:   this.context_id<commit_msg>Only ignore changes to undefined contexts.<commit_after>'use strict';
/**
 * Adds automatic reference updating to a schema.
 *
 * @param {mongoose.Schema} schema The target schema.
 * @param {Function} normalize A function to normalize cases with.
 */

exports.add = function add(schema, normalize) {
  // reference pushing on creation
  schema.pre('save', function(next) {
    if(!this.isNew || !this.context_id) return next();

    var path = getPath(this);
    exports.refUpdate('save', path, this, next);
  });

  // reference pulling on removal
  schema.pre('remove', function(next) {
    if(!this.context_id) return next();

    var path = getPath(this);
    exports.refUpdate('remove', path, this, next);
  });

  // make sure the context is cached
  schema.post('init', exports.cacheContext);
  schema.post('save', exports.cacheContext);

  // reference pushing/pulling on modifications
  schema.pre('save', function(next) {
    // whether the document is new
    var is_new = this.isNew;

    // whether the document has applicable contexts
    var original = this._original;
    var has_contexts = typeof this.context_id !== 'undefined' ||
                       (original && typeof original.context_id !== 'undefined');

    // whether the context has changed
    var context_is_modified = this.isModified('context_id') ||
                              this.isModified('context_type');

    if(is_new || !has_contexts || !context_is_modified) return next();

    var _this = this;
    var new_context = {
      context_type: this.context_type,
      context_id:   this.context_id
{'fraction_non_alphanumeric': 0.13250819075354933, 'fraction_numerical': 0.0, 'mean_word_length': 3.432258064516129, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4688847', 'n_tokens_mistral': 804, 'n_tokens_neox': 781, 'n_words': 222}