Skip to content

Instantly share code, notes, and snippets.

View therealadam's full-sized avatar
🍔
Building Pingboard. Accepting JavaScript. Still loving Rails. Telling jokes.

Adam Keys therealadam

🍔
Building Pingboard. Accepting JavaScript. Still loving Rails. Telling jokes.
View GitHub Profile
@therealadam
therealadam / gist:1627058
Created January 17, 2012 15:27
Sugar for swallowing exceptions
# Suppose we're going to have a bunch of code like the following
# boilerplate (ignore the unfortunate nil propagation)
@city = begin
GizmoStore::Thingy.get(123)
rescue GizmoStore::NotFound
nil
end
# I can't decide if this is useful boilerplate elimination or
# saccharine ivory tower abstraction.
@therealadam
therealadam / test.sh
Created December 1, 2011 23:48
wcld in Ruby
#!/bin/sh
message=$0 || "this just happened #boom"
echo "this just happened #boom" | nc -c localhost 4321
echo '....'
sleep 1
psql wcld -c 'select * from events order by time DESC;'
@therealadam
therealadam / doozer_lock.rb
Created August 28, 2011 21:03
Locking with Doozer
require 'rubygems'
require 'fraggle/block'
# # Locks with doozerd
#
# Doozerd is a little coordination service. I think of it like Zookeeper
# Lite (TM). In today's episode, we'll naively manage exclusive advisory locks.
#
# Disclaimer: If part of this implementation is total lolscale,
# please to let me know what papers/sources I should read to avoid lolscaling.
@therealadam
therealadam / append_only_sets.rb
Created March 16, 2011 03:28
A naive implementation of log-structured sets in Ruby.
# # Append-only sets
#
# Dustin Sallings described [how to implement sets in memcached using only
# atomic commands](http://dustin.github.com/2011/02/17/memcached-set.html).
# This implements the necessary encoding/decoding for that
# scheme. You could then use this with a database that doesn't support sets.
#
# N.B. This is a naive implementation (see the pending specs), but it's fun.
# An append-only set is, after all, a set
@therealadam
therealadam / gist:792648
Created January 24, 2011 01:12
Cassandredis, a possibly ill-advised hack
# cassandredis.rb - It makes your Cassandra look like a Redis.
require "cassandra/0.7"
class Cassandredis
# Connect to a keyspace and column family.
def initialize(keyspace, cf)
@client = Cassandra.new(keyspace)
@cf = cf
class QueryTracer < ActiveSupport::LogSubscriber
ACCEPT = %r{^(app|config|lib)}.freeze
FRAMES = 5
THRESHOLD = 300 # In ms
def sql(event)
return unless event.duration > THRESHOLD
callers = Rails.
backtrace_cleaner.
@therealadam
therealadam / Gemfile
Created June 18, 2010 18:45
An example of how to authorize your app with Gowalla's OAuth API
source :rubygems
gem 'sinatra', '1.0'
gem 'oauth2'
gem 'json'
group :development do
gem 'shotgun'
end
class Array
def map_e(elz, &block)
results = []
if length > 0
each do |o|
block.call(o)
end
else
elz.call
end
# attribute_mapper is a plugin I worked on a little at FiveRuns
# and later extracted because I think it's rad. It's a better way to do
# enumerated values in your models.
#
# I'm giving it a little love this week. One of the things I'd like to do is
# make it a little cleaner vis a vis contemporary Ruby coding practice. One
# of the changes I'd like to make is to switch to explicitly including
# attribute_mapper in models that use it, rather than having it inject itself
# into every model in your app.
#
require 'rubygems'
require 'validatable'
module ValidatableForm
module ClassMethods
def fields
@fields ||= []
end