Skip to content

Instantly share code, notes, and snippets.

@deanrather
Created July 3, 2012 03:46
Show Gist options
  • Save deanrather/3037500 to your computer and use it in GitHub Desktop.
Save deanrather/3037500 to your computer and use it in GitHub Desktop.
JSKK Base Class
/**
* Provides get and set functions, which store properties into the _properties object.
*/
$JSKK.Class.create
(
{
$namespace: 'helper',
$name: 'BaseClass'
}
)
(
// Static Block
{
},
// Instance Block
{
_properties:{},
init: function()
{
},
get: function(key)
{
if (Object.isDefined(this._properties[key]))
{
return this._properties[key];
}
else
{
throw new Error('Invalid property: "'+key+'".');
}
},
set: function(key, val)
{
this._properties[key] = val;
return this;
},
/**
* Like "get" except for use with boolean values
* returns false if the boolean value is false
* @return Boolean
*/
is: function(key)
{
var result = false;
try
{
return (this.get(key) === true);
}
catch(error)
{
return false;
}
},
/**
* Returns an object of properties, given a list of property names
* @param array of property names
* @return key:val object of named properties
*/
getProperties: function(/** list of property names */)
{
var args = $JSKK.toArray(arguments);
var length = args.length;
var rtn = {};
for(var i=0; i<length; i++)
{
var val = args[i];
if(!Object.isString(val)) throw new Error('All paramaters must be strings');
rtn[val] = this.get(val);
}
return rtn;
},
/**
* Deletes a property
* @param string key the name of the property to delete
*/
remove: function(key)
{
delete this._properties[key];
},
/**
* Increments a numeic property by a value.
* Optionally, you can pass in a 'max' which it will not increase beyond
* @param string key the name of the property to increase
* @param numeric value the amount to increase it by
* @param numeric max (optional) it will not be increased beyond this value
* @return numeric the new value
*/
increase: function(key, value, max)
{
if(!Object.isNumeric(this.get(key))) throw new error ('cannot increase non-numeric', key);
if(!Object.isNumeric(value)) throw new error ('cannot increase by non-numeric', value);
this.set(key, this.get(key) + value);
if(max)
{
if(!Object.isNumeric(max)) throw new error ('cannot limit increase to by non-numeric', max);
if(this.get(key) > max) this.set(key, max);
}
return this.get(key);
},
/**
* Decrements a numeic property by a value.
* Optionally, you can pass in a 'min' which it will not decrease below
* @param string key the name of the property to decrease
* @param numeric value the amount to decrease it by
* @param numeric max (optional) it will not bedecreased below this value
* @return numeric the new value
*/
decrease: function(key, value, min)
{
if(!Object.isNumeric(this.get(key))) throw new error ('cannot decrease non-numeric', key);
if(!Object.isNumeric(value)) throw new error ('cannot decrease by non-numeric', value);
this.set(key, this.get(key) - value);
if(min)
{
if(!Object.isNumeric(min)) throw new error ('cannot limit decrease to by non-numeric', min);
if(this.get(key) > min) this.set(key, min);
}
return this.get(key);
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment