Skip to content

Instantly share code, notes, and snippets.

@jnunemaker
jnunemaker / gist:217362
Created October 24, 2009 04:07 — forked from lukesutton/gist:107966
example of warden with sinatra
Warden::Manager.serialize_into_session{|user| user.id }
Warden::Manager.serialize_from_session{|id| User.get(id) }
Warden::Manager.before_failure do |env,opts|
# Sinatra is very sensitive to the request method
# since authentication could fail on any type of method, we need
# to set it for the failure app so it is routed to the correct block
env['REQUEST_METHOD'] = "POST"
end
@dkubb
dkubb / test.rb
Created March 17, 2010 23:52
UoW code spike
# NOTE: this is a code spike, and the following code will probably
# not make it into dm-core in it's current form. I wanted to see if it
# were possible to use a topographical sort to ensure parents were
# saved before children, before/after filters were fired in the correct
# order, and foreign keys set before children are saved, but after parents
# are saved.
# The hooks should fire in the following order:
# https://gist.github.com/6666d2818b14296a28ab
@dkubb
dkubb / yard_dbc.rb
Created August 25, 2010 04:53
YARD based DbC system (code spike)
#!/usr/bin/ruby -Ku
# encoding: utf-8
# The idea with this code spike is to parse some code that has YARD
# documentation, and then wrap the methods at runtime to ensure that
# they match the behavior specified in the docs.
#
# This code will test the input, output and exceptions for every
# documented method. It will also test the provided block's input
@adamsanderson
adamsanderson / test_mail_purge.rb
Created December 18, 2010 04:35
An example of using MiniTest::Mock
require 'minitest/mock'
require 'minitest/unit'
require 'date'
MiniTest::Unit.autorun
class TestMailPurge < MiniTest::Unit::TestCase
class MailPurge
def initialize(imap)
@imap = imap
@TooTallNate
TooTallNate / starttls.js
Created March 1, 2011 01:40
Upgrade a regular `net.Stream` connection to a secure `tls` connection.
// Target API:
//
// var s = require('net').createStream(25, 'smtp.example.com');
// s.on('connect', function() {
// require('starttls')(s, options, function() {
// if (!s.authorized) {
// s.destroy();
// return;
// }
//
@dkubb
dkubb / ev.rb
Created March 16, 2011 22:13
Poor-man's Embedded Value
class User
include DataMapper::Resource
property :id, Serial
property :username, String, :required => true, :unique => true
property :address_street_address, String
property :address_location, String
property :address_subdivision, String
property :address_country, String
@jdmaturen
jdmaturen / ExponentiallyDecayingSample.py
Created April 6, 2011 01:45
expontentially decaying sample algorithm w/ redis
import logging
from math import exp
from random import random
from time import sleep
from time import time
from uuid import uuid1
from redis.exceptions import WatchError
@xaviershay
xaviershay / loader.rb
Created May 17, 2011 07:43
DataMapper hacks to generate raw SQL
# Yields the given models wrapped in Loader::Table objects that can be
# used to write SQL. The block should return an array with the SQL as
# the first element, and variable substitutions as the rest.
#
# Example
# using_sql(Article) {|a|
# ["SELECT #{a.*} FROM #{a} WHERE #{a.id} = ?", 1]
# }
def using_sql(*models)
opts = models.extract_options!
@xaviershay
xaviershay / query_trace_logger.rb
Created May 27, 2011 04:18
Ghetto query tracing for DataMapper. Put it in an initializer.
class QueryTraceLogger
def initialize(logger)
@logger = logger
end
def method_missing(method_name, *args)
if method_name == :debug
if args.first =~ /SQL/
Rails.logger.debug ""
Rails.backtrace_cleaner.clean(caller).each do |line|
@emmanuel
emmanuel / file.sql
Created June 2, 2011 07:54
Closure Table operations SQL fragments
-- Retrieve descendants
-- ====================
-- retrieve descendants of #4
SELECT c.*
FROM Comments AS c
JOIN TreePaths AS t ON c.comment_id = t.descendant
WHERE t.ancestor = 4;
-- Retrieve ancestors