Skip to content

Instantly share code, notes, and snippets.

@jordanyaker
Created April 18, 2012 23:20
Show Gist options
  • Save jordanyaker/2417310 to your computer and use it in GitHub Desktop.
Save jordanyaker/2417310 to your computer and use it in GitHub Desktop.
Implementation Of Error Singleton
/* errors.coffee
----------------------------------------------------------------------*/
@Errors = new ->
_locales = new Array; _locale = null
@init = (locale) ->
count = 0
for entry, i in _locales
matches = entry.key.exec locale
if matches && matches.length > count
count = matches.length; _locale = entry.value
throw new Error 'The specified locale is currently not defined.' unless _locale
@get = (code) ->
return _locale[code] if _locale[code]
return _locale['Default']
@add = (locale) ->
_locales.push locale
@
/* errors.js
----------------------------------------------------------------------*/
(function() {
this.Errors = new function() {
var _locale, _locales;
_locales = new Array;
_locale = null;
this.init = function(locale) {
var count, entry, i, matches, _i, _len;
count = 0;
for (i = _i = 0, _len = _locales.length; _i < _len; i = ++_i) {
entry = _locales[i];
matches = entry.key.exec(locale);
if (matches && matches.length > count) {
count = matches.length;
_locale = entry.value;
}
}
if (!_locale) {
throw new Error('The specified locale is currently not defined.');
}
};
this.get = function(code) {
if (_locale[code]) {
return _locale[code];
}
return _locale['Default'];
};
this.add = function(locale) {
return _locales.push(locale);
};
return this;
};
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment