Skip to content

Instantly share code, notes, and snippets.

View tobi's full-sized avatar

Tobias Lütke tobi

View GitHub Profile
@tobi
tobi / em.rb
Created October 10, 2009 21:34
require 'rubygems'
require 'eventmachine'
# I'm writing a micro web framework that works exclusively in an evented environment.
# All IO is wrapped in async calls. The problem is that this leads to very deeply nested actions
# on average. E.g. request comes in -> Memcache query creates a block -> DB query creates a block
# -> next DB query creates a block and so on. This becomes unwieldy. What i'd really like is
# to move the evented stuff into the library and make the implementation for the framework user
# as familar as possible.
#
module Mogrify
class TransformationError < StandardError
end
def self.from_blob(blob)
ImageStream.new(blob)
end
class ImageStream
class JsonHax
def initialize(app)
@app = app
end
def call(env)
if env['CONTENT_TYPE'] == 'application/json'
env['CONTENT_TYPE'] = 'application/xml'
env['REQUEST_URI'].gsub!(/\.json/, '.xml')
# usage: ruby timecard.rb path branch name-of-graph-file-not-including-extension
# e.g.: ruby timecard.rb . master timecard #=> produces timecard.png
require 'rubygems'
# This requires the 'cyberfox-gchart' gem (0.5.4), as the standard
# gchart gem is woefully broken for this kind of graph. Broken to the
# point that it's an inherent design choice that doesn't work well for
# this kind of chart. I'm sure that the cyberfox-gchart gem won't
STDOUT.sync = true
require 'queue'
start_time = Time.now.to_i
msg = 0
queue = Queue.new("testing")
queue.subscribe do |obj|
msg += 1
require 'fiber'
module Rack
class Fabric
def initialize(app)
@app = app
end
def call(env)
fib = Fiber.new do
def self.accelerated_belongs_to(record)
klass = record.to_s.camelize.constantize
if !klass.respond_to?(:current)
raise ArgumentError, "#{record} does not implement the current class method that is needed for accelerated_belongs_to."
end
self.belongs_to record
alias_method :"#{record}_without_acceleration", record
includes = Product.includes_from_params(params)
conditions = Product.conditions_from_params(params)
@product_count = shop.products.count(:all, :include => includes, :conditions => conditions)
@products = shop.products.find(:all,
:limit => page_limit,
:offset => page_offset,
:conditions => conditions,
<html>
<body>
<script>
var TestCase = function(testcase) {
var success = 0;
var failure = 0;
@tobi
tobi / time_and_json.rb
Created November 3, 2010 13:38
Unexpected behavior of json before core mixins are loaded
[tobi@i7:~]% irb
ree-1.8.7-2010.02 > require 'rubygems'
=> true
ree-1.8.7-2010.02 > require 'json'
=> true
ree-1.8.7-2010.02 > JSON.parse([ Time.now ].to_json)[0].class
=> String
ree-1.8.7-2010.02 > require 'json/add/core'
=> true
ree-1.8.7-2010.02 > JSON.parse([ Time.now ].to_json)[0].class