Skip to content

Instantly share code, notes, and snippets.

View nathanclark's full-sized avatar

Nathan Clark nathanclark

View GitHub Profile
# download, from_repo, and commit_state methods swiped from
# http://github.com/Sutto/rails-template/blob/07b044072f3fb0b40aea27b713ca61515250f5ec/rails_template.rb
require 'open-uri'
def download(from, to = from.split("/").last)
#run "curl -s -L #{from} > #{to}"
file to, open(from).read
rescue
puts "Can't get #{from} - Internet down?"
/*
* CodePress regular expressions for HTML syntax highlighting
*/
// HTML
Language.syntax = [
{ input : /(&lt;[^!]*?&gt;)/g, output : '<b>$1</b>' }, // all tags
{ input : /(&lt;a .*?&gt;|&lt;\/a&gt;)/g, output : '<a>$1</a>' }, // links
{ input : /(&lt;img .*?&gt;)/g, output : '<big>$1</big>' }, // images
{ input : /(&lt;\/?(button|textarea|form|input|select|option|label).*?&gt;)/g, output : '<u>$1</u>' }, // forms
# autotest your engine plugins together with your rails app
# put this .autotest file in your rails root
# see http://code.whatcould.com/2009/02/09/gentlemen-test-your-engines.html for details
# until/unless my changes are pulled into the main streams,
# there are a couple requirements:
# my autotest fork, http://github.com/whatcould/zentest/tree/master
# and for better fixture handling, my engines fork, http://github.com/whatcould/engines/tree/master
Autotest.add_hook :initialize do |at|
# --------------------------------------------------
require 'rubygems'
def run(cmd)
puts(cmd)
system(cmd)
end
def run_all_tests
# see Rakefile for the definition of the test:all task
#!/bin/bash
yum install httpd-devel openssl-devel zlib-devel gcc gcc-c++ curl-devel expat-devel gettext-devel mysql-server mysql-devel curl make
mkdir /usr/local/src
cd /usr/local/src
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p249.tar.gz
tar xzvf ruby-1.8.7-p249.tar.gz
cd ruby-1.8.7-p249
./configure --enable-shared --enable-pthread
make
require 'rubygems'
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we’re interested in...
doc = Nokogiri::HTML(open('http://www.facebook.com/ChickfilA?v=wall','User-Agent' => 'Mozilla/5.0 (...) Firefox/3.0.6'))
# Do funky things with it using Nokogiri::XML::Node methods...
require 'rubygems'
require 'nokogiri'
require 'open-uri'
# Get a Nokogiri::HTML:Document for the page we’re interested in...
client = "FOOOOOOOOO"
doc = Nokogiri::HTML(open("http://www.facebook.com/#{client}?v=wall",'User-Agent' => 'Mozilla/5.0 (...) Firefox/3.0.6'))
####
!function() {
var doc = document,
htm = doc.documentElement,
lct = null, // last click target
nearest = function(elm, tag) {
while (elm && elm.nodeName != tag) {
elm = elm.parentNode;
}
return elm;
};
@nathanclark
nathanclark / Ruby named_scope example
Created January 24, 2011 02:26
[Snippet] named_scope
class User < ActiveRecord::Base
named_scope :active, :conditions => {:active => true}
named_scope :inactive, :conditions => {:active => false}
named_scope :recent, lambda { { :conditions => ['created_at > ?', 1.week.ago] } }
end
# Standard usage
User.active # same as User.find(:all, :conditions => {:active => true})
User.inactive # same as User.find(:all, :conditions => {:active => false})
User.recent # same as User.find(:all, :conditions => ['created_at > ?', 1.week.ago])
def tip(msg); puts; puts msg; puts "-"*100; end
#
# 30 Ruby 1.9 Tips, Tricks & Features:
# http://www.igvita.com/2011/02/03/new-ruby-19-features-tips-tricks/
#
tip "Upgrading to Ruby 1.9 is simple: rvm install 1.9.2 && rvm --default 1.9.2"
tip "Ruby 1.9 supports named captures in regular expressions!"