<commit_before>import Document from 'orbit/document';
import Transformable from 'orbit/transformable';
import Requestable from 'orbit/requestable';
import { assert } from 'orbit/lib/assert';
import { required } from 'orbit/lib/stubs';
import { Class, expose } from 'orbit/lib/objects';
import Cache from './cache';

/**
 `Source` is an abstract base class to be extended by other sources.

 @class Source
 @namespace OC
 @param {OC.Schema} schema
 @param options
 @constructor
*/
var Source = Class.extend({
  init: function(schema, options) {
    assert("Source's `schema` must be specified", schema);

    this.schema = schema;

    options = options || {};

    // Create an internal cache and expose some elements of its interface
    this._cache = new Cache(schema);
    expose(this, this._cache, 'length', 'reset', 'retrieve');
    // TODO - clean up listener
    this._cache.on('didTransform', this._cacheDidTransform, this);

    Transformable.extend(this);
    Requestable.extend(this, ['find', 'add', 'update', 'patch', 'remove',
                              'findLink', 'addLink', 'removeLink',
                              'findLinked']);
  },

  /////////////////////////////////////////////////////////////////////////////
  // Transformable interface implementation
  /////////////////////////////////////////////////////////////////////////////

  /**
   Internal method that applies a single transform to this source.

   `_transform` must be implemented by a `Transformable` source.
   It is called by the public method `transform` in order to actually apply
   transforms.

   `_transform` should return a promise if the operation is asynchronous.

   @method _transform
   @param operation JSON PATCH operation as detailed in RFC 6902
   @private
   */
  _transform: required,

  /////////////////////////////////////////////////////////////////////////////
  // Requestable interface implementation
  /////////////////////////////////////////////////////////////////////////////

  _find: required,

  _findLink: required,

  _findLinked: function(type, id, link, relId) {
    var _this = this;
    var linkDef = _this.schema.models[type].links[link];
    var relType = linkDef.model;

    id = this.getId(type, id);

    if (relId === undefined) {
      relId = this.retrieveLink(type, id, link);
    }

    if (relId) {
      return this.find(relType, relId);

    } else {
      return this.findLink(type, id, link).then(function(relId) {
        return _this.find(relType, relId);
      });
    }
  },

  _add: function(type, data) {
    data = data || {};

    var record = this.normalize(type, data);

    var id = this.getId(type, record),
        path = [type, id],
        _this = this;

    return this.transform({op: 'add', path: path, value: record}).then(function() {
      return _this.retrieve(path);
    });
  },

  _update: function(type, data) {
    var record = this.normalize(type, data);

    var id = this.getId(type, record),
        path = [type, id],
        _this = this;

    return this.transform({op: 'replace', path: path, value: record});
  },

  _patch: function(type, id, property, value) {
    if (id !== null && typeof id === 'object') {
      var record = this.normalize(type, id);
      id = this.getId(type, record);
    }

    return this.transform({
      op: 'replace',
      path: [type, id].concat(Document.prototype.deserializePath(property)),
      value: value
    });
  },

  _remove: function(type, id) {
    if (id !== null && typeof id === 'object') {
      var record = this.normalize(type, id);
      id = this.getId(type, record);
    }

    return this.transform({op: 'remove', path: [type, id]});
  },

  _addLink: function(type, id, key, value) {
    var linkOp = function(linkDef, type, id, key, value) {<commit_msg>More consistent formatting / declarations<commit_after>import Document from 'orbit/document';
import Transformable from 'orbit/transformable';
import Requestable from 'orbit/requestable';
import { assert } from 'orbit/lib/assert';
import { required } from 'orbit/lib/stubs';
import { Class, expose } from 'orbit/lib/objects';
import Cache from './cache';

/**
 `Source` is an abstract base class to be extended by other sources.

 @class Source
 @namespace OC
 @param {OC.Schema} schema
 @param options
 @constructor
*/
var Source = Class.extend({
  init: function(schema, options) {
    assert("Source's `schema` must be specified", schema);

    this.schema = schema;

    options = options || {};

    // Create an internal cache and expose some elements of its interface
    this._cache = new Cache(schema);
    expose(this, this._cache, 'length', 'reset', 'retrieve');
    // TODO - clean up listener
    this._cache.on('didTransform', this._cacheDidTransform, this);

    Transformable.extend(this);
    Requestable.extend(this, ['find', 'add', 'update', 'patch', 'remove',
                              'findLink', 'addLink', 'removeLink',
                              'findLinked']);
  },

  /////////////////////////////////////////////////////////////////////////////
  // Transformable interface implementation
  /////////////////////////////////////////////////////////////////////////////

  /**
   Internal method that applies a single transform to this source.

   `_transform` must be implemented by a `Transformable` source.
   It is called by the public method `transform` in order to actually apply
   transforms.

   `_transform` should return a promise if the operation is asynchronous.

   @method _transform
   @param operation JSON PATCH operation as detailed in RFC 6902
   @private
   */
  _transform: required,

  /////////////////////////////////////////////////////////////////////////////
  // Requestable interface implementation
  /////////////////////////////////////////////////////////////////////////////

  _find: required,

  _findLink: required,

  _findLinked: function(type, id, link, relId) {
    var _this = this;
    var linkDef = _this.schema.models[type].links[link];
    var relType = linkDef.model;

    id = this.getId(type, id);

    if (relId === undefined) {
      relId = this.retrieveLink(type, id, link);
    }

    if (relId) {
      return this.find(relType, relId);

    } else {
      return this.findLink(type, id, link).then(function(relId) {
        return _this.find(relType, relId);
      });
    }
  },

  _add: function(type, data) {
    data = data || {};

    var record = this.normalize(type, data);

    var id = this.getId(type, record),
        path = [type, id],
        _this = this;

    return this.transform({op: 'add', path: path, value: record}).then(function() {
      return _this.retrieve(path);
    });
  },

  _update: function(type, data) {
    var record = this.normalize(type, data);
    var id = this.getId(type, record);
    var path = [type, id];

    return this.transform({op: 'replace', path: path, value: record});
  },

  _patch: function(type, id, property, value) {
    if (id !== null && typeof id === 'object') {
      var record = this.normalize(type, id);
      id = this.getId(type, record);
    }

    var path = [type, id].concat(Document.prototype.deserializePath(property));

    return this.transform({op: 'replace', path: path, value: value});
  },

  _remove: function(type, id) {
    if (id !== null && typeof id === 'object') {
      var record = this.normalize(type, id);
      id = this.getId(type, record);
    }

    var path = [type, id];

    return this.transform({op: 'remove', path: path});
  },

  _addLink: function(type, id, key, value) {
    var linkOp = function(linkDef, type, id, key, value) {
{'fraction_non_alphanumeric': 0.20598660890114218, 'fraction_numerical': 0.0010502822633582775, 'mean_word_length': 2.204880100967606, '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': '5418530', 'n_tokens_mistral': 2161, 'n_tokens_neox': 2077, 'n_words': 645}