<commit_before>var Alloy = require('alloy'),
	_ = require("alloy/underscore")._,
	TAP = Ti.App.Properties;

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};

function guid() {
   return (S4()+S4()+'-'+S4()+'-'+S4()+'-'+S4()+'-'+S4()+S4()+S4());
};	

function Sync(model, method, opts) {
	var prefix = model.config.adapter.collection_name ? model.config.adapter.collection_name : 'default';
	var regex = new RegExp("^(" + prefix + ")\\-(.+)$");
	var resp = null;

	if (method === 'read') {
		if (opts.parse) {
			// is collection
			var list = [];
			_.each(TAP.listProperties(), function(prop) {
				var match = prop.match(regex);
				if (match !== null) {
					list.push(TAP.getObject(prop));
				}
			});
			model.reset(list);
			resp = list;
		} else {
			// is model
			var obj = TAP.getObject(prefix + '-' + model.get('id'));
			model.set(obj);
			resp = model.toJSON();
		}	
	} 
	else if (method === 'create' || method === 'update') { 
		var newId = model.get('id') || guid();
		model.set({id: newId}, {silent:true});
		TAP.setObject(prefix + '-' + newId, model.toJSON() || {});
		resp = model.toJSON();
	} else if (method === 'delete') {
		TAP.removeProperty(prefix + '-' + model.get('id'));
		model.clear();
		resp = model.toJSON();
	}

	// process success/error handlers, if present
	if (resp) {
        _.isFunction(opts.success) && opts.success(resp);
        method === "read" && model.trigger("fetch");
    } else {
    	_.isFunction(opts.error) && opts.error("Record not found");
    }
}

module.exports.sync = Sync;
module.exports.beforeModelCreate = function(config) {
	// make sure we have a populated model object
	config = config || {};<commit_msg>added idAttribute fix to properties sync adapter<commit_after>var Alloy = require('alloy'),
	_ = require("alloy/underscore")._,
	TAP = Ti.App.Properties;

function S4() {
   return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};

function guid() {
   return (S4()+S4()+'-'+S4()+'-'+S4()+'-'+S4()+'-'+S4()+S4()+S4());
};	

function Sync(model, method, opts) {
	var prefix = model.config.adapter.collection_name ? model.config.adapter.collection_name : 'default';
	var regex = new RegExp("^(" + prefix + ")\\-(.+)$");
	var resp = null;

	if (method === 'read') {
		if (opts.parse) {
			// is collection
			var list = [];
			_.each(TAP.listProperties(), function(prop) {
				var match = prop.match(regex);
				if (match !== null) {
					list.push(TAP.getObject(prop));
				}
			});
			model.reset(list);
			resp = list;
		} else {
			// is model
			var obj = TAP.getObject(prefix + '-' + model.get('id'));
			model.set(obj);
			resp = model.toJSON();
		}	
	} 
	else if (method === 'create' || method === 'update') { 
		if (!model.id) {
			model.id = guid();
	        model.set(model.idAttribute, model.id);
		}
		TAP.setObject(prefix + '-' + model.id, model.toJSON() || {});
		resp = model.toJSON();
	} else if (method === 'delete') {
		TAP.removeProperty(prefix + '-' + model.get('id'));
		model.clear();
		resp = model.toJSON();
	}

	// process success/error handlers, if present
	if (resp) {
        _.isFunction(opts.success) && opts.success(resp);
        method === "read" && model.trigger("fetch");
    } else {
    	_.isFunction(opts.error) && opts.error("Record not found");
    }
}

module.exports.sync = Sync;
module.exports.beforeModelCreate = function(config) {
	// make sure we have a populated model object
	config = config || {};
{'fraction_non_alphanumeric': 0.2146158290005777, 'fraction_numerical': 0.011554015020219527, 'mean_word_length': 3.3559748427672957, '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': '4131899', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1219, 'n_words': 245}