Skip to content

Instantly share code, notes, and snippets.

View aliang's full-sized avatar

Alvin Liang aliang

View GitHub Profile
@aliang
aliang / application.rb
Created June 12, 2011 08:12
monkeypatch Rails::Application.load_console to load app-specific .irbrc
# taken from http://railsbros.de/2010/11/29/a-custom-irbrc-for-rails-3.html
# config/application.rb that is
module MyApp
class Application < Rails::Application
# config stuff comes here
def load_console(sandbox=false)
super
if File.exists?(project_specific_irbrc = File.join(Rails.root, ".irbrc"))
puts "Loading project specific .irbrc ..."
@aliang
aliang / stream_itunes_over_ssh.sh
Created June 12, 2011 08:39
stream iTunes over SSH (requires dns-sd)
# Shamelessly taken from http://blog.iharder.net/2009/09/28/itunes-stream-itunes-over-ssh/
# Your remote library will appear in your local iTunes as a shared library
# You need an ssh server running on the remote library's computer
#!/bin/sh
dns-sd -P "Home iTunes" _daap._tcp local 3689 localhost.local. \
127.0.0.1 "Arbitrary text record" &
PID=$!
ssh -C -N -L 3689:localhost:3689 yourhost.dyndns.org
kill $PID
@aliang
aliang / mymodel.rb
Created June 13, 2011 06:25
render from model in Rails 3
# yes, sometimes you need to do this. you get pilloried in a forum if you
# ask about it, though!
# this code taken from
# http://wholemeal.co.nz/blog/2011/04/05/rendering-from-a-model-in-rails-3/
class MyModel < ActiveRecord::Base
# Use the my_models/_my_model.txt.erb view to render this object as a string
def to_s
ActionView::Base.new(Rails.configuration.paths.app.views.first).render(
@aliang
aliang / devise_http_rememberable.rb
Created June 13, 2011 07:55 — forked from jamiew/devise_http_rememberable.rb
Monkeypatch Devise to support saving sessions via HTTP auth (e.g. via bookmarklets or browser addons)
# config/initializers/devise_http_rememberable.rb
# Monkeypatch Devise so HTTP authentication sets remember_user_token cookie
# ABR: Always Be Remembering
module Devise
module Strategies
class Authenticatable < Base
def remember_me?
# Devise's implementation of this is:
# valid_params? && Devise::TRUE_VALUES.include?(params_auth_hash[:remember_me])
@aliang
aliang / Mac SSH Autocomplete
Created June 14, 2011 07:14
Add auto complete to your ssh, put into your .bash_profile
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
@aliang
aliang / triangles.html
Created June 20, 2011 09:21
triangles without images
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<title>Triangle</title>
<meta charset="utf-8" />
<style type="text/css">
.a{width:0;height:0;border-top:100px solid #0099ff;border-left:100px solid #ff9900;border-right:100px solid #F6402D;border-bottom:100px solid #8CC63E; float:left;}
.b{
width:0;height:0;border-top:100px solid #0099ff;border-left:100px solid transparent;border-right:100px solid transparent;border-bottom:0; margin-left:60px; float:left;}
.c{width:0;height:0;border-top:100px solid transparent;border-left:0;border-right:100px solid transparent;border-bottom:100px solid #0099ff; margin-left:60px; float:left;}
@aliang
aliang / parse_query.rb
Created June 20, 2011 22:00
search query string parser, google-ish
# via http://jablan.radioni.ca/post/4982485974/parsing-search-query-in-ruby
def parse_query s
s.scan(/((\S+)\:\s?)?([+-])?(("(.+?)")|(\S+))/).map{|match|
Hash[
[nil, :prefix, :plusminus, nil, nil, :phrase, :word].zip(match).select(&:all?)
]
}
end
@aliang
aliang / gist:1393029
Created November 25, 2011 08:02
monkey patch to make pymongo not throw ridiculous AutoReconnect errors
# Stolen from http://paste.pocoo.org/show/224441/
from pymongo.cursor import Cursor
from pymongo.connection import Connection
from pymongo.errors import AutoReconnect
from time import sleep
def reconnect(f):
def f_retry(*args, **kwargs):
@aliang
aliang / gist:1507475
Created December 21, 2011 20:04
recursive globbing
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
for f in find_files(".", "*.txt"):
print f
@aliang
aliang / build_less.py
Created January 22, 2012 09:08 — forked from remcoder/build_less.py
Compile LESS to CSS automatically whenever a file is modified
#!/usr/bin/env python
# determines filename and extension, then delegates compiling the LESS files to lessc via a shell command
import sys, os.path
from subprocess import call
src = sys.argv[1]
base,ext = os.path.splitext(src)