Skip to content

Instantly share code, notes, and snippets.

View creationix's full-sized avatar
💚
<3

Tim Caswell creationix

💚
<3
View GitHub Profile
process.mixin(require('sys'));
var Persistance = require('./persistance');
var db;
// Can use a mongodb server as the backend
db = new Persistance.Backend('mongodb://localhost/blog');
// Can use a postgres server as the backend
db = new Persistance.Backend('postgresql://user:pass@localhost:5432/blog');
// Can use a sqlite database file
@creationix
creationix / dispatcher.rb
Created December 9, 2009 18:13
simple dispatcher using method_missing in Ruby
class Dispatcher
# Remove the cruft to try to keep the options clear
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_variable_?e?|object_id|inspect|dup/ }
# Store the callback and initialize the route
def initialize(&block)
@route = []
@block = block
end
@creationix
creationix / sqlite_driver.js
Created December 9, 2009 21:30
sqlite3 wrapper for node.js
var sys = require('sys');
function Sqlite(path) {
var conn = process.createChildProcess("sqlite3", ['-batch', '-header', '-nullvalue', "\r", '-separator', "\t", path]),
queue = [], dequeued = 0,
callback, want_close = false;
function parse(string) {
var raw, headers;
// Parse the output into cells. newline for row, tab for field, and
@creationix
creationix / jquery.ui.menu.js
Created December 28, 2009 20:54
OS style dropdown menu as a jQuery-UI plugin
(function($) {
$.widget("ui.menu", {
active: false,
last_activated: null,
last_menu_level: null,
last_level: null,
timer_id: null,
_init: function() {
this._menufy();
},
// combo library
function Combo(callback) {
this.callback = callback;
this.items = 0;
this.results = [];
}
Combo.prototype = {
add: function () {
var self = this;
this.items++;
~$ coffee -i
coffee> process.mixin(require("sys"))
coffee> p
[Function]
coffee> p (x) -> x * x
[Function]
coffee> puts (x) -> x * x
function (x) {
return x * x;
}
process.mixin(require('sys'));
valid_modes = ["r", "w", "a"];
var File = {
open: function (filename, mode) { return function (callback) {
debug("in open closure");
mode = mode || "w";
if (valid_modes.indexOf(mode) < 0) {
throw new Error("Invalid file mode " + JSON.stringify(mode));
From fa8b7dc1513e03ca014d3ffd234c6d8e0c9e8d5f Mon Sep 17 00:00:00 2001
From: Tim Caswell <[email protected]>
Date: Wed, 3 Feb 2010 12:28:05 -0600
Subject: [PATCH] Use the new Object.getOwnPropertyNames to show hidden properties on inspect. Added as a second optional argument to inspect.
For example:
node> puts(inspect([1,2,3]))
[
1,
// Imagine some library called Do that is used as follows:
Do.parallel(
users.search({"age >": 21}).addCallback,
file.read("pubs.txt").addCallback
)(function (people, text) {
// This function will be called after the database
// query and the file read are both done.
});
From 151330d050405d088b357713d7c204847902be37 Mon Sep 17 00:00:00 2001
From: Tim Caswell <[email protected]>
Date: Wed, 3 Feb 2010 20:29:13 -0600
Subject: [PATCH] Bind the addCallback function on File.read and File.write so we can use it in any context.
---
lib/file.js | 19 ++++++++++++++++---
1 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/lib/file.js b/lib/file.js