<commit_before>//     (c) 2012 <NAME>, Catch.com
//     goog.mvc may be freely distributed under the MIT license.
//     For all details and documentation:
//     https://github.com/rhysbrettbowen/goog.mvc

goog.provide('mvc.AjaxSync');

goog.require('goog.Uri.QueryData');
goog.require('goog.net.XhrManager');
goog.require('mvc.Sync');



/**
 * @constructor
 * @implements {mvc.Sync}
 * @param {Object|string|Function} url used to construct URLs.
 */
mvc.AjaxSync = function(url) {

  var baseFunction = function(model) {return '';};

  this.baseUrls_ = {
    create: baseFunction,
    read: baseFunction,
    update: baseFunction,
    del: baseFunction
  };

  if (goog.isString(url) || goog.isFunction(url)) {
    url = {
      create: url,
      read: url,
      update: url,
      del: url
    };
  }
  goog.object.extend(this.baseUrls_, goog.object.map(url, this.urlifyString));
  this.xhr_ = new goog.net.XhrManager();
  this.sendCount_ = 0;
};


/**
 * takes a string defining a url where :attribute will return that models
 * attribute. e.g.
 *
 * var obj = new mvc.Model({attrs:{'id': 'fred'}});
 * var urlGen = urlifyString("/object=:id/blah");
 * urlGen(obj); // returns "/object=fred/blah"
 *
 *
 * @param {string} val to be changed to a function.
 * @return {function(mvc.Model):string} function that gives URL.
 */
mvc.AjaxSync.prototype.urlifyString = function(val) {
  if (goog.isString(val)) {
    return function(model) {
      return val.replace(/:(\w+)/g,
          function(id) {
            return model.get(id.substring(1));
          });
    };
  }
  return /** @type {function(mvc.Model):string} */(val);
};


/**
 * @inheritDoc
 */
mvc.AjaxSync.prototype.create = function(model, opt_callback) {

  this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.create(model),
      'POST', goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
      undefined, undefined,
      goog.bind(this.onCreateComplete_, this, model,
          (opt_callback || goog.nullFunction)));
};


/**
 * @inheritDoc
 */
mvc.AjaxSync.prototype.read = function(model, opt_callback) {
  this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.read(model),
      'GET', undefined, undefined, undefined,
      goog.bind(this.onReadComplete_, this, model,
          (opt_callback || goog.nullFunction)));
};

<commit_msg>fix urlify function<commit_after>//     (c) 2012 <NAME>, Catch.com
//     goog.mvc may be freely distributed under the MIT license.
//     For all details and documentation:
//     https://github.com/rhysbrettbowen/goog.mvc

goog.provide('mvc.AjaxSync');

goog.require('goog.Uri.QueryData');
goog.require('goog.net.XhrManager');
goog.require('mvc.Sync');



/**
 * @constructor
 * @implements {mvc.Sync}
 * @param {Object|string|Function} url used to construct URLs.
 */
mvc.AjaxSync = function(url) {

  var baseFunction = function(model) {return '';};

  this.baseUrls_ = {
    create: baseFunction,
    read: baseFunction,
    update: baseFunction,
    del: baseFunction
  };

  if (goog.isString(url) || goog.isFunction(url)) {
    url = {
      create: url,
      read: url,
      update: url,
      del: url
    };
  }
  goog.object.extend(this.baseUrls_, goog.object.map(url, this.urlifyString));
  this.xhr_ = new goog.net.XhrManager();
  this.sendCount_ = 0;
};


/**
 * takes a string defining a url where :attribute will return that models
 * attribute. e.g.
 *
 * var obj = new mvc.Model({attrs:{'id': 'fred'}});
 * var urlGen = urlifyString("/object=:id/blah");
 * urlGen(obj); // returns "/object=fred/blah"
 *
 *
 * @param {Function|string} val to be changed to a function.
 * @return {function(mvc.Model):string} function that gives URL.
 */
mvc.AjaxSync.prototype.urlifyString = function(val) {
  return function(model) {
    var v = val;
    if (goog.isFunction(v))
      v = v(model);
    return v.replace(/:(\w+)/g,
        function(id) {
          return escape(model.get(id.substring(1)));
        });
  };
};


/**
 * @inheritDoc
 */
mvc.AjaxSync.prototype.create = function(model, opt_callback) {

  this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.create(model),
      'POST', goog.Uri.QueryData.createFromMap(model.toJson()).toString(),
      undefined, undefined,
      goog.bind(this.onCreateComplete_, this, model,
          (opt_callback || goog.nullFunction)));
};


/**
 * @inheritDoc
 */
mvc.AjaxSync.prototype.read = function(model, opt_callback) {
  this.xhr_.send('' + (this.sendCount_++), this.baseUrls_.read(model),
      'GET', undefined, undefined, undefined,
      goog.bind(this.onReadComplete_, this, model,
          (opt_callback || goog.nullFunction)));
};


{'fraction_non_alphanumeric': 0.1682122061677809, 'fraction_numerical': 0.002587880094888937, 'mean_word_length': 3.2628676470588234, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1591262', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1557, 'n_words': 315}