Skip to content

Instantly share code, notes, and snippets.

@nw
nw / Class.Mutators.Singleton.js
Created May 9, 2009 02:08
Class Initializers (Singleton)
Class.Initializers = {};
Class.initializer = function(obj){ return this._initializer_ = obj; }
Class.Mutators.initialize = function(initialize){
if(!this._initializer_) return initialize; // quick exit
return function(){
this._init_ = initialize;
for (var modifier in Class.Initializers) {
if (!this[modifier]) continue;
this[modifier] = Class.Initializers[modifier].call(this, this[modifier]);
Number.implement({
parsePermissions : function(){
var types = {r : 4, w : 2, x : 1 }, levels = {u : 1, g : 8, o : 64 }, perms = {};
for(l in levels){
for(t in types) perms[l+t] = !!(this & (levels[l] * types[t]));
}
return perms;
}
});
Element.implement({
truncate : function(len,str,mid){
var str = str || '...';
var mid = !!mid;
var w = this.getSize().x;
if( w < len) return this;
var txt = this.get('text');
var char_size = Math.ceil(w / txt.length);
Element.implement({
truncate : function(len,str,mid){
var str = str || '...';
var mid = !!mid;
var w = this.getSize().x;
if( w < len) return this;
var txt = this.get('text');
var char_size = Math.ceil(w / txt.length);
/* Window.Growl, version 2.0: http://icebeat.bitacoras.com
* Daniel Mota aka IceBeat <daniel.mota@gmail.com>
* Updated to 1.2b2 by Paul Streise <paulstreise@gmail.com>
* Updated to 1.2.2 by nwhite.net
--------------------------------------------------------------------------*/
var Gr0wl = {};
Gr0wl.Base = new Class({
Implements : [Chain],
/*
MooTools 1.2 Custom Backwards-Compatibility Library
By David Isaacson
Portions from Mootools 1.2 by the MooTools production team (http://mootools.net/developers/)
Copyright (c) 2006-2007 Valerio Proietti (http://mad4milk.net/)
Copyright (c) 2008 Siafoo.net
Cleaned up, shortened and logging added by Nathan White (http://www.nwhite.net)
*/
var sys = require('sys'),
times = 500000
function bm(label, fn) {
var start = +new Date
fn()
sys.puts(' ' + label + ' : ' + (+new Date - start) + ' ms')
};
Model.define('User',{
collection : 'test_user', // (optional) if not present uses the model name instead.
// defines your data structure
types: {
_id : Object, // if not defined, Mongoose automatically defines for you.
username: String,
first : String,
last : String,
result = {'array':[1,2,3],
'string':'hello',
'hash':{'a':1, 'b':2},
'date':new Date(), // Stores only milisecond resolution
'int':42,
'float':33.3333,
'regexp':/foobar/i,
'boolean':true,
'null':null};
var assert = require('assert')
, subclass = function(type, proto){
// make sure we are extending a global constructor
// accepted: [Boolean, Number, String, Array, Object, Function, RegExp, Date]
if(!global[type.name] || global[type.name].name != type.name) throw new Error();
var constructor = proto.constructor,
keys = Object.keys(proto),
Obj = process.binding('evals').Script.runInNewContext('x = '+type.name), // eval ftw