<commit_before>/*!
 * BackFire is the officially supported Backbone binding for Firebase. The
 * bindings let you use special model and collection types that allow for
 * synchronizing data with Firebase.
 *
 * BackFire 0.0.0
 * https://github.com/firebase/backfire/
 * License: MIT
 */

(function(_, Backbone) {
  "use strict";

  Backbone.Firebase = {};

  /**
   * A utility for retrieving the key name of a Firebase ref or
   * DataSnapshot. This is backwards-compatible with `name()`
   * from Firebase 1.x.x and `key()` from Firebase 2.0.0+. Once
   * support for Firebase 1.x.x is dropped in BackFire, this
   * helper can be removed.
   */
  Backbone.Firebase._getKey = function(refOrSnapshot) {
    return (typeof refOrSnapshot.key === 'function') ? refOrSnapshot.key() : refOrSnapshot.name();
  };

  /**
   * A utility for resolving whether an item will have the autoSync
   * property. Models can have this property on the prototype.
   */
  Backbone.Firebase._determineAutoSync = function(self, options) {
    var proto = Object.getPrototypeOf(self);
    return _.extend(
      {
        autoSync: proto.hasOwnProperty('autoSync') ? proto.autoSync : true
      },
      this,
      options
    ).autoSync;
  };

  /**
   * Overriding of Backbone.sync.
   * All Backbone crud calls (destroy, add, create, save...) will pipe into
   * this method. This way Backbone can handle the prepping of the models
   * and the trigering of the appropiate methods. While sync can be overwritten
   * to handle updates to Firebase.
   */
  Backbone.Firebase.sync = function(method, model, options) {
    var modelJSON = model.toJSON();

    if (method === 'read') {

      Backbone.Firebase._readOnce(model.firebase, function onComplete(snap) {
        var resp = snap.val();
        options.success(resp);
      });

    } else if (method === 'create') {

      Backbone.Firebase._setWithCheck(model.firebase, modelJSON, options);

    } else if (method === 'update') {

      Backbone.Firebase._updateWithCheck(model.firebase, modelJSON, options);

    } else if(method === 'delete') {

      Backbone.Firebase._setWithCheck(model.firebase, null, options);

    }

  };

  /**
   * A utility for a one-time read from Firebase.
   */
  Backbone.Firebase._readOnce = function(ref, cb) {
    ref.once('value', cb);
  };

  /**
   * A utility for a destructive save to Firebase.
   */
  Backbone.Firebase._setToFirebase = function(ref, item, onComplete) {
    ref.set(item, onComplete);
  };

  /**
   * A utility for a non-destructive save to Firebase.
   */
  Backbone.Firebase._updateToFirebase = function(ref, item, onComplete) {
    ref.update(item, onComplete);
  };


  /**
   * A utility for success and error events that are called after updates
   * from Firebase.
   */
  Backbone.Firebase._onCompleteCheck = function(err, item, options) {
    if(!options) { return; }

    if(err && options.error) {
      options.error(item, err, options);
    } else if(options.success) {
      options.success(item, null, options);
    }
  };

  /**
   * A utility for a destructive save to Firebase that handles success and
   * error events from the server.
   */
  Backbone.Firebase._setWithCheck = function(ref, item, options) {
    Backbone.Firebase._setToFirebase(ref, item, function(err) {
      Backbone.Firebase._onCompleteCheck(err, item, options);
    });
  };

  /**
   * A utility for a non-destructive save to Firebase that handles success and
   * error events from the server.
   */
  Backbone.Firebase._updateWithCheck = function(ref, item, options) {
    Backbone.Firebase._updateToFirebase(ref, item, function(err) {
      Backbone.Firebase._onCompleteCheck(err, item, options);
    });
  };

  /**
   * A utility for throwing errors.
   */
  Backbone.Firebase._throwError = function(message) {
    throw new Error(message);
  };


  /**
   * A utility for a determining whether a string or a Firebase
   * reference should be returned.
   *    string - return new Firebase('')
   *    object - assume object is ref and return
   */
  Backbone.Firebase._determineRef = function(objOrString) {
    switch (typeof(objOrString)) {
    case 'string':
      return new Firebase(objOrString);
    case 'object':
      return objOrString;
    default:
      Backbone.Firebase._throwError('Invalid type passed to url property');
    }
  };

  /**
   * A utility for assigning an id from a snapshot.
   *    object    - Assign id from snapshot key
   *    primitive - Throw error, primitives cannot be synced
   *    null      - Create blank object and assign id
   */
  Backbone.Firebase._checkId = function(snap) {
    var model = snap.val();

    // if the model is a primitive throw an error
    if (Backbone.Firebase._isPrimitive(model)) {
      Backbone.Firebase._throwError('InvalidIdException: Models must have an Id. Note: You may ' +
      'be trying to sync a primitive value (int, string, bool).');
    }

    // if the model is null set it to an empty object and assign its id
    // this way listeners can still be attached to populate the object in the future
    if(model === null) {
      model = {};
    }

    // set the id to the snapshot's key
    model.id = snap.name();

    return model;
  };

  /**
   * A utility for checking if a value is a primitive
   */
  Backbone.Firebase._isPrimitive = function(value) {
    // is the value not an object and not null (basically, is it a primitive?)
    return !_.isObject(value) && model !== null;
  };

  /**
   * Model responsible for autoSynced objects
   * This model is never directly used. The Backbone.Firebase.Model will
   * inherit from this if it is an autoSynced model
   */
  var SyncModel = (function() {

    function SyncModel() {
      // Set up sync events

      // apply remote changes locally
      this.firebase.on('value', function(snap) {

        this._setLocal(snap);
        this.trigger('sync', this, null, null);
      }, this);

      // apply local changes remotely
      this._listenLocalChange(function(model) {
        this.firebase.update(model);
      });

    }

    SyncModel.protoype = {
      save: function() {
        console.warn('Save called on a Firebase model with autoSync enabled, ignoring.');
      },<commit_msg>Corrected _isPrimitive error and removed _getKey<commit_after>/*!
 * BackFire is the officially supported Backbone binding for Firebase. The
 * bindings let you use special model and collection types that allow for
 * synchronizing data with Firebase.
 *
 * BackFire 0.0.0
 * https://github.com/firebase/backfire/
 * License: MIT
 */

(function(_, Backbone) {
  "use strict";

  Backbone.Firebase = {};

  /**
   * A utility for resolving whether an item will have the autoSync
   * property. Models can have this property on the prototype.
   */
  Backbone.Firebase._determineAutoSync = function(self, options) {
    var proto = Object.getPrototypeOf(self);
    return _.extend(
      {
        autoSync: proto.hasOwnProperty('autoSync') ? proto.autoSync : true
      },
      this,
      options
    ).autoSync;
  };

  /**
   * Overriding of Backbone.sync.
   * All Backbone crud calls (destroy, add, create, save...) will pipe into
   * this method. This way Backbone can handle the prepping of the models
   * and the trigering of the appropiate methods. While sync can be overwritten
   * to handle updates to Firebase.
   */
  Backbone.Firebase.sync = function(method, model, options) {
    var modelJSON = model.toJSON();

    if (method === 'read') {

      Backbone.Firebase._readOnce(model.firebase, function onComplete(snap) {
        var resp = snap.val();
        options.success(resp);
      });

    } else if (method === 'create') {

      Backbone.Firebase._setWithCheck(model.firebase, modelJSON, options);

    } else if (method === 'update') {

      Backbone.Firebase._updateWithCheck(model.firebase, modelJSON, options);

    } else if(method === 'delete') {

      Backbone.Firebase._setWithCheck(model.firebase, null, options);

    }

  };

  /**
   * A utility for a one-time read from Firebase.
   */
  Backbone.Firebase._readOnce = function(ref, cb) {
    ref.once('value', cb);
  };

  /**
   * A utility for a destructive save to Firebase.
   */
  Backbone.Firebase._setToFirebase = function(ref, item, onComplete) {
    ref.set(item, onComplete);
  };

  /**
   * A utility for a non-destructive save to Firebase.
   */
  Backbone.Firebase._updateToFirebase = function(ref, item, onComplete) {
    ref.update(item, onComplete);
  };


  /**
   * A utility for success and error events that are called after updates
   * from Firebase.
   */
  Backbone.Firebase._onCompleteCheck = function(err, item, options) {
    if(!options) { return; }

    if(err && options.error) {
      options.error(item, err, options);
    } else if(options.success) {
      options.success(item, null, options);
    }
  };

  /**
   * A utility for a destructive save to Firebase that handles success and
   * error events from the server.
   */
  Backbone.Firebase._setWithCheck = function(ref, item, options) {
    Backbone.Firebase._setToFirebase(ref, item, function(err) {
      Backbone.Firebase._onCompleteCheck(err, item, options);
    });
  };

  /**
   * A utility for a non-destructive save to Firebase that handles success and
   * error events from the server.
   */
  Backbone.Firebase._updateWithCheck = function(ref, item, options) {
    Backbone.Firebase._updateToFirebase(ref, item, function(err) {
      Backbone.Firebase._onCompleteCheck(err, item, options);
    });
  };

  /**
   * A utility for throwing errors.
   */
  Backbone.Firebase._throwError = function(message) {
    throw new Error(message);
  };


  /**
   * A utility for a determining whether a string or a Firebase
   * reference should be returned.
   *    string - return new Firebase('')
   *    object - assume object is ref and return
   */
  Backbone.Firebase._determineRef = function(objOrString) {
    switch (typeof(objOrString)) {
    case 'string':
      return new Firebase(objOrString);
    case 'object':
      return objOrString;
    default:
      Backbone.Firebase._throwError('Invalid type passed to url property');
    }
  };

  /**
   * A utility for assigning an id from a snapshot.
   *    object    - Assign id from snapshot key
   *    primitive - Throw error, primitives cannot be synced
   *    null      - Create blank object and assign id
   */
  Backbone.Firebase._checkId = function(snap) {
    var model = snap.val();

    // if the model is a primitive throw an error
    if (Backbone.Firebase._isPrimitive(model)) {
      Backbone.Firebase._throwError('InvalidIdException: Models must have an Id. Note: You may ' +
      'be trying to sync a primitive value (int, string, bool).');
    }

    // if the model is null set it to an empty object and assign its id
    // this way listeners can still be attached to populate the object in the future
    if(model === null) {
      model = {};
    }

    // set the id to the snapshot's key
    model.id = snap.name();

    return model;
  };

  /**
   * A utility for checking if a value is a primitive
   */
  Backbone.Firebase._isPrimitive = function(value) {
    // is the value not an object and not null (basically, is it a primitive?)
    return !_.isObject(value) && value !== null;
  };

  /**
   * Model responsible for autoSynced objects
   * This model is never directly used. The Backbone.Firebase.Model will
   * inherit from this if it is an autoSynced model
   */
  var SyncModel = (function() {

    function SyncModel() {
      // Set up sync events

      // apply remote changes locally
      this.firebase.on('value', function(snap) {

        this._setLocal(snap);
        this.trigger('sync', this, null, null);
      }, this);

      // apply local changes remotely
      this._listenLocalChange(function(model) {
        this.firebase.update(model);
      });

    }

    SyncModel.protoype = {
      save: function() {
        console.warn('Save called on a Firebase model with autoSync enabled, ignoring.');
      },
{'fraction_non_alphanumeric': 0.11194091776616048, 'fraction_numerical': 0.0009127873205543108, 'mean_word_length': 2.7768724537762455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2526107', 'n_tokens_mistral': 3618, 'n_tokens_neox': 3515, 'n_words': 1121}