<commit_before>/*
 * HTML5 localStorage sync adapter 
 */
var _ = require('alloy/underscore')._;

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

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

function InitAdapter(config) {
	if (!OS_MOBILEWEB) {
		throw 'No support for localStorage persistence in non MobileWeb environments.';
	}
}

function Sync(model, method, opts) {
	var name =  model.config.adapter.collection_name;
	var data =  model.config.data;

	function storeModel(data) {
		localStorage.setItem(name, JSON.stringify(data));
	}

	switch (method) {

		case 'create':
			if (!model.id) model.id = model.attributes.id = guid();
			data[model.id] = model;
	    	storeModel(data);
	    	break;   	

		case 'read':
			var store = localStorage.getItem(name);
			var store_data = (store && JSON.parse(store)) || {};
            
            var len = 0;
            for (var key in store_data) {
            	var m = new model.config.Model(store_data[key]);
            	model.models.push(m);
            	len++;
            }

            model.length = len;
            model.trigger('fetch');
			break;

		case 'update':
			data[model.id] = model;
			storeModel(data); 
			break;
		
		case 'delete':
			delete data[model.id];
			storeModel(data); 
			break;
	}
}

module.exports.sync = Sync;

module.exports.beforeModelCreate = function(config) {
	config = config || {};
	
	config.data = {}; // for localStorage or case where entire collection is needed to maintain store

	InitAdapter(config);

	return config;
};

module.exports.afterModelCreate = function(Model) {
	Model = Model || {};
	
	Model.prototype.config.Model = Model; // needed for fetch operations to initialize the collection from persistent store

	return Model;
};<commit_msg>refactor and idAttribute fix for localStorage adapter<commit_after>/*
 * HTML5 localStorage sync adapter 
 */
var _ = require('alloy/underscore')._;

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

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

function InitAdapter(config) {
	if (!OS_MOBILEWEB) {
		throw 'localStorage persistence supported only with MobileWeb.';
	}
}

function Sync(model, method, opts) {
	var name = model.config.adapter.collection_name,
		data = model.config.data,
		resp = null;

	function storeModel(data) {
		localStorage.setItem(name, JSON.stringify(data));
	}

	switch (method) {

		case 'create':
			if (!model.id) {
				model.id = guid();
		        model.set(model.idAttribute, model.id);
			}
			data[model.id] = model;
	    	storeModel(data);
	    	resp = model.toJSON();
	    	break;   	

		case 'read':
			var store = localStorage.getItem(name);
			var store_data = (store && JSON.parse(store)) || {};
            
            var len = 0;
            for (var key in store_data) {
            	var m = new model.config.Model(store_data[key]);
            	model.models.push(m);
            	len++;
            }

            model.length = len;
            len === 1 ? resp = model.models[0] : resp = model.models;
			break;

		case 'update':
			data[model.id] = model;
			storeModel(data); 
			resp = model.toJSON();
			break;
		
		case 'delete':
			delete data[model.id];
			storeModel(data); 
			resp = model.toJSON();
			break;
	}

	// 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) {
	config = config || {};
	
	config.data = {}; // for localStorage or case where entire collection is needed to maintain store

	InitAdapter(config);

	return config;
};

module.exports.afterModelCreate = function(Model) {
	Model = Model || {};
	
	Model.prototype.config.Model = Model; // needed for fetch operations to initialize the collection from persistent store

	return Model;
};
{'fraction_non_alphanumeric': 0.15511791879406758, 'fraction_numerical': 0.011184050571359105, 'mean_word_length': 2.9255725190839694, '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': '2347034', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1316, 'n_words': 305}