Skip to content

Instantly share code, notes, and snippets.

View trek's full-sized avatar
🏳️‍🌈

Trek Glowacki trek

🏳️‍🌈
View GitHub Profile
@trek
trek / routes.py
Created September 25, 2008 20:17
from routes import *
from routes.util import url_for
m = Mapper()
m.resource('blog', 'blogs')
m.create_regs(['blogs'])
url_for('blog', id=2) # outputs '/blogs/2'
m.match('/blogs/2') # no match
def show
@note = Note.find(:where => 'id = 1')
rescue NotFound
redirect_to 'not_found', :status => 404
rescue NoDatabase
logger.error('the db died again, shit')
redirect_to 'apology', :status => 500
end
class Economy
def slowing?
self.value.today < self.value.yesterday
end
def slow_by(amount)
self.value -= amount
end
end
if foreigner = terrorist
imprison(foreigner)
else
release(foreigner)
end
def show
@note = Note.find(:where => 'id = 1')
rescue NotFound
redirect_to 'not_found', :status => 404
rescue NoDatabase
logger.error('the db died again, shit')
redirect_to 'apology', :status => 500
end
class Foo(object):
def __init__(self,arg):
"""docstring for __init__"""
self.arg = arg
def dec(fn):
def wrapper(*args):
# args[0] is self. Lame
args[0].arg = 'not what you thought, huh?'
return fn(*args)
class Thingie
attr_accessor :element
def initialize(element)
self.element = Element.new(element)
# with a block
self.element.respond_to :click do
self.element.hide
... other stuff ...
end
class PhotoBox
attr_accessor :element, :images
def initialize(element)
# associate an element with this object
self.element = Element.new(element)
# give it a class
self.element.add_class('active')
# find subelemts and store them
Customer < ActiveRecord::Base
before_destroy :log_deletion, :email_admin
private
def log_deletion
logger.info("customer id:#{self.id} was just deleted")
end
def email_admin
...
class Conference
has_many :attendees, :class_name => 'Person', :through => :attendance
def Conference.next_upcoming
self.find!(:first, :conditions => ['starting_at > ? and visibile = ?' Time.now, true])
rescue RecordNotFound
self.most_recent
end
def most_recent