Created
August 10, 2011 19:49
-
-
Save henriquemoody/1137964 to your computer and use it in GitHub Desktop.
Implementação de Registry em javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implementação de Registry em javascript. | |
* | |
* @author Henrique Moody | |
*/ | |
var Registry = new function Registry () { | |
// Dados no registry | |
var _data = new Array(); | |
/** | |
* Verifica há valor definido para "name". | |
* | |
* @param {String} name | |
* @return {Boolean} | |
*/ | |
this.has = function (name) | |
{ | |
return (name in _data); | |
}; | |
/** | |
* Verifica há valor definido para "name". | |
* | |
* @throws {Error} | |
* @param {String} name | |
* @return {Object} | |
*/ | |
this.get = function (name) | |
{ | |
if (!this.has(name)) { | |
throw new Error('Chave ' + name + ' não foi definida.'); | |
} | |
return _data[name].value; | |
}; | |
/** | |
* Define um valor no Registry. | |
* | |
* @param {String} name Nome da chave registrada | |
* @param {String} value Valor registrado | |
* @param {Boolean} readOnly Se o valor é apenas leitura ou não | |
* @return {Registry} | |
*/ | |
this.set = function (name, value, readOnly) | |
{ | |
if (this.has(name) && this.isReadOnly(name)) { | |
throw Error('Você não pode sobrescrever' + name); | |
} | |
if (true != readOnly) { | |
readOnly = false; | |
} | |
_data[name] = { | |
value: value, | |
readOnly : readOnly | |
} | |
return this; | |
}; | |
/** | |
* @param {String} name | |
* @return {Boolean} | |
*/ | |
this.isReadOnly = function (name) | |
{ | |
return (this.has(name) && _data[name].readOnly == true); | |
} | |
/** | |
* Remove um valor no Registry. | |
* | |
* @param {String} name Nome da chave registrada | |
* @return {Registry} | |
*/ | |
this.unset = function (name) | |
{ | |
if (this.has(name)) { | |
if (this.isReadOnly(name)) { | |
throw Error('Você não pode remover' + name); | |
} | |
delete _data[name]; | |
} | |
return this; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment