<commit_before>/**
 * CouchDB base class/resource definition.
 * This file should be inherited by all CouchDB models that want to leverage 
 * CouchDB's power in rocket.
 */

var crypto = require('crypto');
var _ = require('underscore');
var lingo = require('lingo');
var cradle = require('cradle');
var watch  = require('watch');
var async = require('async');
 
var oo = require('../utils/oo');
var BaseResource = require('./base_resource');

/******************************************************************************
 * CONSTANTS
 */

var ROCKET_NAMESPACE = 'rocket';

/******************************************************************************
 * Default connection parameters
 */
CouchDBResource.connection = {
    host: 'localhost'
  , port: 5984
  , options: { 
      raw: true
    , cache: false
    , auth: null
    }
  };

/******************************************************************************
 * Default _design documents                                     *** SYNCD ***
 */
CouchDBResource.ddocs = [
    { 
      _id: '_design/rocket'
    , lists: {}
    , shows: {}
    , updates: {}
    , validate_doc_update: function(newDoc, oldDoc, userCtx) {
        var schema = require('rocket/schema');
        
        if(newDoc._deleted) {
          return;
        }
                
        for(var member in schema) {
        
          if(typeof schema[member] !== 'undefined' 
          && schema[member] !== null) {
          
            var validator;
            var optional;
            
            if(typeof schema[member] === 'string') {
            
              validator = require('rocket/validators/' + schema[member]) || null;
              
            }else if(typeof schema[member] === 'object') {
            
              optional = schema[member].optional;
              validator = (schema[member].validate ? require('rocket/validators/' + schema[member].validate) : null);
              
            }
            
            if(typeof newDoc[member] !== 'undefined'
            && newDoc[member] !== null) {
              if(validator) {
                validator(newDoc[member]);
              }else{
                continue;
              }
            }else if(optional){
              continue;
            }else{
              throw({ invalid: member + ' can\'t be missing or null' });
            }
          }
        }
      }
    , views: {
       all: {
          map: function(doc) { emit(doc._id, doc); }
        }
      }
    }
  ];

/******************************************************************************
 * Default _security document                                    *** SYNCD ***
 */
CouchDBResource._security = {
    admins: {
        names: ["admin"]
      , roles: []
      }
  , readers: {
        names: []
      , roles: []
      }  
  };

/******************************************************************************
 * Resource Prototype Functions (used to create models)
 */
CouchDBResource.prototype = {
    save: function save_CouchDBResourceInstance() {}
  , update: function update_CouchDBResourceInstance() {}
  , destroy: function destroy_CouchDBResourceInstance() {}
  , reload: function reload_CouchDBResourceInstance() {}
  , exists: function exists_CouchDBResourceInstance() {}
  };
 
/******************************************************************************
 * Resource Factory/Constructor Functions
 */
var factoryFunctions = {
    initialize: function initialize_CouchDBResource(callback) {
      var that = this;
    
      //Initialize connection object
      that.__connection = new cradle.Connection(
          that.connection.host
        , that.connection.port
        , that.connection.options
        );
      
      //Infer DB name from file name
      if(typeof that.name == 'undefined') {
        throw 'xxx ERROR Model.type is undefined';
      }
      
      that.__db_name = lingo.en.pluralize(that.name).toLowerCase();
      
      //setup db object
      that.__db = this.__connection.database(that.__db_name);
      
      //Create db if it doesn't exists. Harmless otherwise.
      that.__db.create();      
      
      async.parallel(
          [function(callback) {
              async.forEach(that.ddocs, syncDoc, callback);
            }
       /* , function(callback) {
              that.get('_security', syncSecurity);
            }*/
          ]
        , callback);
        
      /*function syncSecurity(err, doc) {
          if(err) {
            if(err.error === 'not_found') {
              that.__db.save('_security'
                            , that._security
                            , callback);
            }else{
              callback(err);
            }
          }else{
            //oo.__extends(doc, that._security, {overwrite: true});
             console.log('sec: ' + that._security.toString());
            that.__db.save('_security', that._security, callback);
          }
        };*/
        
      
      function fctToString(obj) {
        for(var key in obj) {
          if(typeof obj[key] === 'function') {
            obj[key] = obj[key].toString();
          }else if(typeof obj[key] === 'object' && obj[key] !== null) {
            arguments.callee(obj[key]);
          }
        }
      }       
        
      function syncDoc(docObj, callback) {
      
        //create the `rocket` namespace in the design doc
        docObj[ROCKET_NAMESPACE] = {};
      
        //add the validators to the design doc
        docObj[ROCKET_NAMESPACE].validators = that.validators;
        <commit_msg>Closed issue #14 : Allow CouchDBResource to sync _security<commit_after>/**
 * CouchDB base class/resource definition.
 * This file should be inherited by all CouchDB models that want to leverage 
 * CouchDB's power in rocket.
 */

var crypto = require('crypto');
var _ = require('underscore');
var lingo = require('lingo');
var cradle = require('cradle');
var watch  = require('watch');
var async = require('async');
 
var oo = require('../utils/oo');
var BaseResource = require('./base_resource');

/******************************************************************************
 * CONSTANTS
 */

var ROCKET_NAMESPACE = 'rocket';

/******************************************************************************
 * Default connection parameters
 */
CouchDBResource.connection = {
    host: 'localhost'
  , port: 5984
  , options: { 
      raw: true
    , cache: false
    , auth: null
    }
  };

/******************************************************************************
 * Default _design documents                                     *** SYNCD ***
 */
CouchDBResource.ddocs = [
    { 
      _id: '_design/rocket'
    , lists: {}
    , shows: {}
    , updates: {}
    , validate_doc_update: function(newDoc, oldDoc, userCtx) {
        var schema = require('rocket/schema');
        
        if(newDoc._deleted) {
          return;
        }
                
        for(var member in schema) {
        
          if(typeof schema[member] !== 'undefined' 
          && schema[member] !== null) {
          
            var validator;
            var optional;
            
            if(typeof schema[member] === 'string') {
            
              validator = require('rocket/validators/' + schema[member]) || null;
              
            }else if(typeof schema[member] === 'object') {
            
              optional = schema[member].optional;
              validator = (schema[member].validate ? require('rocket/validators/' + schema[member].validate) : null);
              
            }
            
            if(typeof newDoc[member] !== 'undefined'
            && newDoc[member] !== null) {
              if(validator) {
                validator(newDoc[member]);
              }else{
                continue;
              }
            }else if(optional){
              continue;
            }else{
              throw({ invalid: member + ' can\'t be missing or null' });
            }
          }
        }
      }
    , views: {
       all: {
          map: function(doc) { emit(doc._id, doc); }
        }
      }
    }
  ];

/******************************************************************************
 * Default _security document                                    *** SYNCD ***
 */
CouchDBResource._security = {
    admins: {
        names: ["admin"]
      , roles: []
      }
  , readers: {
        names: []
      , roles: []
      }  
  };

/******************************************************************************
 * Resource Prototype Functions (used to create models)
 */
CouchDBResource.prototype = {
    save: function save_CouchDBResourceInstance() {}
  , update: function update_CouchDBResourceInstance() {}
  , destroy: function destroy_CouchDBResourceInstance() {}
  , reload: function reload_CouchDBResourceInstance() {}
  , exists: function exists_CouchDBResourceInstance() {}
  };
 
/******************************************************************************
 * Resource Factory/Constructor Functions
 */
var factoryFunctions = {
    initialize: function initialize_CouchDBResource(callback) {
      var that = this;
    
      //Initialize connection object
      that.__connection = new cradle.Connection(
          that.connection.host
        , that.connection.port
        , that.connection.options
        );
      
      //Infer DB name from file name
      if(typeof that.name == 'undefined') {
        throw 'xxx ERROR Model.type is undefined';
      }
      
      that.__db_name = lingo.en.pluralize(that.name).toLowerCase();
      
      //setup db object
      that.__db = this.__connection.database(that.__db_name);
      
      //Create db if it doesn't exists. Harmless otherwise.
      that.__db.create();      
      
      async.parallel(
          [function(callback) {
              async.forEach(that.ddocs, syncDoc, callback);
            }
          , function(callback) {
              that.__db._save('_security', false, that._security, callback);
            }
          ]
        , callback);
        
      
      function fctToString(obj) {
        for(var key in obj) {
          if(typeof obj[key] === 'function') {
            obj[key] = obj[key].toString();
          }else if(typeof obj[key] === 'object' && obj[key] !== null) {
            arguments.callee(obj[key]);
          }
        }
      }       
        
      function syncDoc(docObj, callback) {
      
        //create the `rocket` namespace in the design doc
        docObj[ROCKET_NAMESPACE] = {};
      
        //add the validators to the design doc
        docObj[ROCKET_NAMESPACE].validators = that.validators;
        
{'fraction_non_alphanumeric': 0.2008262135010797, 'fraction_numerical': 0.0009388789784996713, 'mean_word_length': 1.9239637661268185, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5609662', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2698, 'n_words': 580}