<commit_before>'use strict';

////////////////////////////////////////////////////////////////////////////////
/// Foxx Repository
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2013 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var Model = require("org/arangodb/foxx/model").Model,
  _ = require("underscore"),
  arangodb = require("org/arangodb"),
  ArangoError = arangodb.ArangoError,
  ArangoCollection = arangodb.ArangoCollection,
  errors = arangodb.errors,
  extend = require('extendible'),
  EventEmitter = require('events').EventEmitter,
  util = require('util');

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_initializer
/// `new FoxxRepository(collection, opts)`
///
/// Create a new instance of Repository.
///
/// A Foxx Repository is always initialized with a collection object. You can get
/// your collection object by asking your Foxx.Controller for it: the
/// *collection* method takes the name of the collection (and will prepend
/// the prefix of your application). It also takes two optional arguments:
///
/// 1. Model: The prototype of a model. If you do not provide it, it will default
/// to Foxx.Model
/// 2. Prefix: You can provide the prefix of the application if you need it in
/// your Repository (for some AQL queries probably)
///
/// @EXAMPLES
///
/// ```javascript
/// instance = new Repository(appContext.collection("my_collection"));
/// // or:
/// instance = new Repository(appContext.collection("my_collection"), {
///   model: MyModelPrototype,
///   prefix: app.collectionPrefix,
/// });
/// ```
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

function Repository(collection, opts) {
  if (! collection instanceof ArangoCollection) {
    throw new ArangoError({
      errorNum: errors.ERROR_BAD_PARAMETER.code,
      errorMessage: "Collection parameter has to be of type ArangoCollection"
    });
  }
  this.options = opts || {};

// -----------------------------------------------------------------------------
// --SECTION--                                                        Attributes
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_collection
/// The wrapped ArangoDB collection object.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.collection = collection;

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_model
/// The model of this repository. Formerly called "modelPrototype".
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.model = this.options.model || Model;

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_modelSchema
/// The schema of this repository's model.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  Object.defineProperty(this, 'modelSchema', {
    configurable: false,
    enumerable: true,
    get: function () {
      return this.model.prototype.schema;
    }
  });

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_prefix
/// The prefix of the application. This is useful if you want to construct AQL
/// queries for example.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.prefix = this.options.prefix;

  // Undocumented, unfinished graph feature
  this.graph = this.options.graph;

  if (this.indexes) {
    _.each(this.indexes, function (index) {
      this.collection.ensureIndex(index);
    }, this);
  }

  EventEmitter.call(this);
}

util.inherits(Repository, EventEmitter);

// -----------------------------------------------------------------------------<commit_msg>Implemented auto-binding model lifecycle listeners. Fixes #1257.<commit_after>'use strict';

////////////////////////////////////////////////////////////////////////////////
/// Foxx Repository
///
/// @file
///
/// DISCLAIMER
///
/// Copyright 2013 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
///     http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author <NAME>
/// @author Copyright 2013, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var Model = require("org/arangodb/foxx/model").Model,
  _ = require("underscore"),
  arangodb = require("org/arangodb"),
  ArangoError = arangodb.ArangoError,
  ArangoCollection = arangodb.ArangoCollection,
  errors = arangodb.errors,
  extend = require('extendible'),
  EventEmitter = require('events').EventEmitter,
  util = require('util');

var EVENTS = [
  'beforeSave', 'beforeCreate', 'beforeUpdate', 'beforeRemove',
  'afterSave', 'afterCreate', 'afterUpdate', 'afterRemove'
];

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_initializer
/// `new FoxxRepository(collection, opts)`
///
/// Create a new instance of Repository.
///
/// A Foxx Repository is always initialized with a collection object. You can get
/// your collection object by asking your Foxx.Controller for it: the
/// *collection* method takes the name of the collection (and will prepend
/// the prefix of your application). It also takes two optional arguments:
///
/// 1. Model: The prototype of a model. If you do not provide it, it will default
/// to Foxx.Model
/// 2. Prefix: You can provide the prefix of the application if you need it in
/// your Repository (for some AQL queries probably)
///
/// If the Model has any static methods named after the lifecycle events, they
/// will automatically be registered as listeners to the events emitted by this
/// repository.
///
/// **Examples**
///
/// ```js
/// instance = new Repository(appContext.collection("my_collection"));
/// // or:
/// instance = new Repository(appContext.collection("my_collection"), {
///   model: MyModelPrototype
/// });
/// ```
///
/// Example with listeners:
///
/// ```js
/// var ValidatedModel = Model.extend({
///   schema: {...}
/// }, {
///   beforeSave: function (modelInstance) {
///     if (!modelInstance.valid) {
///       throw new Error('Refusing to save: model is not valid!');
///     }
///   }
/// });
/// instance = new Repository(collection, {model: ValidatedModel});
/// ```
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

function Repository(collection, opts) {
  if (! collection instanceof ArangoCollection) {
    throw new ArangoError({
      errorNum: errors.ERROR_BAD_PARAMETER.code,
      errorMessage: "Collection parameter has to be of type ArangoCollection"
    });
  }
  this.options = opts || {};

// -----------------------------------------------------------------------------
// --SECTION--                                                        Attributes
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_collection
/// The wrapped ArangoDB collection object.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.collection = collection;

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_model
/// The model of this repository. Formerly called "modelPrototype".
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.model = this.options.model || Model;

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_modelSchema
/// The schema of this repository's model.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  Object.defineProperty(this, 'modelSchema', {
    configurable: false,
    enumerable: true,
    get: function () {
      return this.model.prototype.schema;
    }
  });

////////////////////////////////////////////////////////////////////////////////
/// @startDocuBlock JSF_foxx_repository_prefix
/// The prefix of the application. This is useful if you want to construct AQL
/// queries for example.
/// @endDocuBlock
////////////////////////////////////////////////////////////////////////////////

  this.prefix = this.options.prefix;

  // Undocumented, unfinished graph feature
  this.graph = this.options.graph;

  if (this.indexes) {
    _.each(this.indexes, function (index) {
      this.collection.ensureIndex(index);
    }, this);
  }

  EventEmitter.call(this);

  _.each(this.model, function (listener, eventName) {
    if (EVENTS.indexOf(eventName) === -1 || typeof listener !== 'function') {
      return;
    }
    this.on(eventName, listener.bind(this.model));
  }, this);
}

util.inherits(Repository, EventEmitter);

// -----------------------------------------------------------------------------
{'fraction_non_alphanumeric': 0.32998309224121736, 'fraction_numerical': 0.0030997557768175843, 'mean_word_length': 1.4023014440433212, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6980372', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2367, 'n_words': 761}