Skip to content

Instantly share code, notes, and snippets.

View adrianpike's full-sized avatar

Adrian Pike adrianpike

View GitHub Profile
# delicious helper
def dlify_hash(hash)
'<dl>'+"\n"+hash.collect{|k,v|
"\t<dd>#{k}</dd><dt>#{v}</dt>"
}.join("\n")+"\n"+'</dl>'
end
# Or, if you're a sick bastard like me and don't care if you've got layout logic monkeypatched into Ruby Core:
class FooController < ApplicationController
after_filter 'run_some_magic'
def run_some_magic
StatsMagic.whoa('holy crap')
end
end
class StatsMagic
@adrianpike
adrianpike / gist:193156
Created September 25, 2009 00:09
Remoteling
Remoteling:
- redis server, everybody gets a private redis slice
- starling FIFO queue server, everybody can push & pop to it
- remoteling queue
- I can fire off processes that execute remotely from my app
- I can set up N resident processes running on the remoteling server
- Easy producer/consumer model
- Billing? Information gathering when new users show up? (see example A below)
num = 4.0
pi = 0
plus = true
den = 1
while den < 100000000
if plus
pi = pi + num/den
plus = false
else
# rip lots from activerecord as well as datamapper
# ruby's duck typed. a k/v store should be duck typed. therefore why the hell do I care to set the ID to be type of integer if it's just gonna get stuffed in memory somewheres?
class User < Thunderbucket::Base
has_many :comments
belongs_to :user_group
attributes :name, :password, :email
# - or -
attributes true # i can set anything that's not a method to anything, not sure if i wanna actually implement this, could be too dangerous
:loaded_attributes:
:object: 18610940
:attributes:
- name
- last_login_at
- last_request_at
- updated_at
- crypted_password
- single_access_token
- perishable_token
@adrianpike
adrianpike / mysqldump cron script
Created November 12, 2009 19:53
Mysqldump backup script
#!/bin/bash
NOW=`date +%d-%b`
USER="root"
PASSWORD="lolwat"
OUTPUTDIR="/backup/databases/$NOW"
MYSQLDUMP="mysqldump"
MYSQL="mysql"
# CONTROLLER
if $INVITE_ONLY and (params[:invite_key] != @user.invite_key) then
flash.now[:error] = t 'account.invalid_invite'
render :action => :new
else
if @user.save
flash[:notice] = t 'account.created'
redirect_to account_path
else
#hat tip @bear454
require 'rubygems'
require 'uri'
require 'resolv'
def ip_to_color(ip,alpha = false)
segments = ip.split('.').collect{|s| s.to_i.to_s(16).rjust(2,'0') }
segments.pop unless alpha
segments.join('')
### Before the Rails request
---
- SERVER_NAME
- PATH_INFO
- HTTP_ACCEPT_ENCODING
- HTTP_USER_AGENT
- rack.url_scheme
- rack.run_once
- rack.input
- SCRIPT_NAME