Skip to content

Instantly share code, notes, and snippets.

View scttnlsn's full-sized avatar

Scott Nelson scttnlsn

View GitHub Profile
@scttnlsn
scttnlsn / README.md
Created September 1, 2011 00:25
Minimalist append-only key/value store written in Ruby.

A minimalist append-only key/value store written in Ruby.

Ruby API

c = Collection.new('example.db')

c.set('hello', 'world')
c.get('hello') # => "world"
@scttnlsn
scttnlsn / editor.coffee
Created September 9, 2011 01:37
A toy text editor written in CoffeeScript
# =========================================
# A toy text editor written in CoffeeScript
# =========================================
#
# Requires ncurses bindings for node.js:
# npm install ncurses
#
# Usage:
# coffee editor.coffee FILENAME
#
@scttnlsn
scttnlsn / app.rb
Created October 18, 2011 13:59
Sinatra/Mongoid RESTful resource
require 'mongoid'
require 'sinatra'
require_relative 'resource'
class Example
include Mongoid::Document
field :name
field :created, :type => DateTime
@scttnlsn
scttnlsn / promise.js
Created January 13, 2012 14:43
Simple promise implementation for Node.js and the browser
(function() {
if (typeof module !== 'undefined' && module.exports) {
module.exports = Promise;
} else {
this.Promise = Promise;
}
function Promise(context) {
this.context = context || this;
@scttnlsn
scttnlsn / base_view.js
Created May 8, 2012 17:00
Backbone.js base view
var BaseView = Backbone.View.extend({
close: function() {
this.closeSubviews();
this.unbindFromAll();
this.off();
this.remove();
if (this.onClose) this.onClose();
},
@scttnlsn
scttnlsn / eachobj.js
Created May 14, 2012 21:38
Handlebars object iteration
Handlebars.registerHelper('eachobj', function(object, options) {
var buffer = '';
var empty = true;
for (var key in object) {
buffer += options.fn({ key: key, value: object[key] });
empty = false;
}
if (empty) {
@scttnlsn
scttnlsn / server.js
Created May 18, 2012 17:46
Serving thumbnails on-the-fly with Nettle and Express
var express = require('express');
var im = require('imagemagick');
var nettle = require('nettle');
var app = express.createServer();
var store = nettle.store({ url: 'mongodb://localhost:27017/mydb' });
// Create Nettle processor to resize image
var createProcessor = function(width) {
store.processor(width, function(buffer, callback) {
@scttnlsn
scttnlsn / coffeescript-ifer.sublime-snippet
Created June 11, 2012 16:56
ST2 Snippet: if (err) return callback(err)
<snippet>
<content><![CDATA[return ${1:callback} ${2:err} if ${2:err}?]]></content>
<tabTrigger>ifer</tabTrigger>
<scope>source.coffee</scope>
<description>ifer</description>
</snippet>
@scttnlsn
scttnlsn / README.md
Created July 30, 2012 22:16
Pub/sub with MongoDB and Node.js

Pub/sub with MongoDB and Node.js

Setup:

$ mongo
> use pubsub
> db.createCollection('messages', { capped: true, size: 100000 })
> db.messages.insert({})
@scttnlsn
scttnlsn / README.md
Created August 21, 2012 16:13
Git as a key/value store

git-store

$ git init mystore
$ cd mystore

$ git store set foo "this is foo"
$ git store get foo
this is foo