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
# A shoulda macro for verifying a class has the writer attribute readers,
# writers and accessors. NOTE: I could be silly for wanting this. But it was
# there. So here it is.
#
# (c) Copyright 2009 Adam Keys. MIT license.
module AccessorMacros
def should_have_reader(name)
should "have an attribute reader for #{name.to_s}" do
module BlameTheCompiler
def method_added(name)
if rand(3) != 1
undef_method(name)
end
end
end
require 'rubygems'
require 'validatable'
module ValidatableForm
module ClassMethods
def fields
@fields ||= []
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.
#
class Array
def map_e(elz, &block)
results = []
if length > 0
each do |o|
block.call(o)
end
else
elz.call
end
@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 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 / 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
@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 / 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.