<commit_before>var _ = require('underscore');
var cdb = require('cartodb.js');
var camshaftReference = require('../../../../../data/camshaft-reference');

/**
 * Base model for all analysis form models.
 */
module.exports = cdb.core.Model.extend({

  initialize: function (attrs, opts) {
    if (!opts.layerDefinitionModel) throw new Error('layerDefinitionModel is required');
    if (!opts.analysisSourceOptionsModel) throw new Error('analysisSourceOptionsModel is required');

    this._layerDefinitionModel = opts.layerDefinitionModel;
    this._analysisSourceOptionsModel = opts.analysisSourceOptionsModel;
  },

  isNew: function () {
    return true; // so one can call .destroy() on it to clean up bindings etc. and have it removed from its collection
  },

  getTemplate: function () {
    return undefined; // override to return custom template
  },

  getTemplateData: function () {
    return undefined; // override to return custom template data
  },

  validate: function () {
    var type = this.get('type');
    var sourceNames = camshaftReference.getSourceNamesForAnalysisType(type);
    var paramNames = camshaftReference.getParamNamesForAnalysisType(type);

    var errors = _.reduce(paramNames, function (memo, name) {
      var val = this.attributes[name];

      if (_.contains(sourceNames, name) && val === 'source-placeholder') {
        memo[name] = _t('data.analysis-definition-node-model.validation.invalid-source');
      } else if (val === undefined) {
        memo[name] = _t('data.analysis-definition-node-model.validation.required');
      }

      return memo;
    }, {}, this);

    if (!_.isEmpty(errors)) {
      return errors;
    }
  },

  save: function (analysisDefinitionNodesCollection) {
    var nodeDefModel = analysisDefinitionNodesCollection.get(this.id);
    if (nodeDefModel) {
      this._updateNodeDefinition(nodeDefModel);
    } else {
      this._createNodeDefinition();
    }
  },

  /**
   * @protected
   */
  _updateNodeDefinition: function (nodeDefModel) {
    nodeDefModel.set(this.attributes);
  },

  /**
   * @protected<commit_msg>Sources should be set for validation to pass<commit_after>var _ = require('underscore');
var cdb = require('cartodb.js');
var camshaftReference = require('../../../../../data/camshaft-reference');

/**
 * Base model for all analysis form models.
 */
module.exports = cdb.core.Model.extend({

  initialize: function (attrs, opts) {
    if (!opts.layerDefinitionModel) throw new Error('layerDefinitionModel is required');
    if (!opts.analysisSourceOptionsModel) throw new Error('analysisSourceOptionsModel is required');

    this._layerDefinitionModel = opts.layerDefinitionModel;
    this._analysisSourceOptionsModel = opts.analysisSourceOptionsModel;
  },

  isNew: function () {
    return true; // so one can call .destroy() on it to clean up bindings etc. and have it removed from its collection
  },

  getTemplate: function () {
    return undefined; // override to return custom template
  },

  getTemplateData: function () {
    return undefined; // override to return custom template data
  },

  validate: function () {
    var type = this.get('type');
    var sourceNames = camshaftReference.getSourceNamesForAnalysisType(type);
    var paramNames = camshaftReference.getParamNamesForAnalysisType(type);

    var errors = _.reduce(paramNames, function (memo, name) {
      var val = this.attributes[name];

      if (_.contains(sourceNames, name) && !val) {
        memo[name] = _t('data.analysis-definition-node-model.validation.invalid-source');
      } else if (val === undefined) {
        memo[name] = _t('data.analysis-definition-node-model.validation.required');
      }

      return memo;
    }, {}, this);

    if (!_.isEmpty(errors)) {
      return errors;
    }
  },

  save: function (analysisDefinitionNodesCollection) {
    var nodeDefModel = analysisDefinitionNodesCollection.get(this.id);
    if (nodeDefModel) {
      this._updateNodeDefinition(nodeDefModel);
    } else {
      this._createNodeDefinition();
    }
  },

  /**
   * @protected
   */
  _updateNodeDefinition: function (nodeDefModel) {
    nodeDefModel.set(this.attributes);
  },

  /**
   * @protected
{'fraction_non_alphanumeric': 0.12819288613034138, 'fraction_numerical': 0.0, 'mean_word_length': 3.5005370569280343, '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': '5942105', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1149, 'n_words': 281}