Skip to content

Instantly share code, notes, and snippets.

View dcparker's full-sized avatar

Daniel Parker dcparker

View GitHub Profile
def locals(*only, &block)
raise ArgumentError, "must include a block" unless block_given?
bndng = block.send(:binding)
all_locals = eval("local_variables",bndng) - ['_']
locals_hash = Hash[all_locals.map {|l| [l,eval(l,bndng)]}]
Hash[locals_hash.select {|k,v| only.include?(v)}]
end
hi = "hello world"
bye = "goodbye world"
(function($){
// Set up the overlay as a DOM node, we'll swap it in and out of the DOM
var $overlay = $('<div></div>');
var $overlay_content = $('<div></div>');
$overlay_content.css({
'width' : '25em',
'margin' : '100px auto',
'border' : '1px solid #000',
'padding' : '15px',
'text-align': 'center',
action: listcustomers
<QBXML>
<QBXMLMsgsRq onError="stopOnError">
<CustomerQueryRq>
<MaxReturned>params[:limit]</MaxReturned>
</CustomerQueryRq>
</QBXMLMsgsRq>
</QBXML>
action: show
<QBXML>
@dcparker
dcparker / ChromeProfile
Created March 5, 2010 15:12
ChromeProfile switcher. Usage: `ChromeProfile Home` / `ChromeProfile Work`
#!/usr/bin/env ruby
$profiles_path = "#{ENV['HOME']}/Library/Application Support/Google/Chrome"
Dir.chdir($profiles_path)
if ARGV[0] == 'clone'
$clone = true
ARGV.shift
elsif ARGV[0] == 'list'
puts Dir['*'].select {|d| File.directory?(d) && File.exists?("#{d}/.profile_name")}.map {|d| File.read("#{d}/.profile_name").chomp}.join("\n")
@dcparker
dcparker / irb-man.rb
Created March 2, 2010 17:45
`man` command for IRB
module Kernel
def man(obj)
publ = obj.public_methods.sort - Object.public_methods - Kernel.public_methods
puts "Public:\n#{publ.join(', ')}"
priv = obj.private_methods.sort - Object.private_methods - Kernel.private_methods
puts "\nProtected:\n#{prot.join(', ')}" unless prot.empty?
prot = obj.protected_methods.sort - Object.protected_methods - Kernel.protected_methods
puts "\nPrivate:\n#{priv.join(', ')}" unless priv.empty?
@dcparker
dcparker / gist:300797
Created February 10, 2010 20:24
A versionable API routing-controller-action strategy.
# Routing
map.with_options(:namespace => 'api/', :path_prefix => "/api/:version", :version => /v\d+/) do |api|
api.resources :photos
end
# API Controller superclass
class ApiController < ApplicationController
append_view_path 'views/api'
# this will happen before anything else that cares about
@dcparker
dcparker / mkgem
Created December 16, 2009 21:18
mkgem - an interactive prompt to help you assemble a gem from scratch
#!/usr/bin/env ruby
# Command: mkgem
# Usage: mkgem gemname
# mkgem will prompt you for all of the pieces of information
# it needs to create a gem using jeweler.
# PLEASE FEEL FREE TO FORK AND CONTRIBUTE!
# Adjusts Google Calendar ical times for TimeZone ONLY WHEN NECESSARY
tzadjust = Time.gcalschema("#{e["DTSTART;TZID=#{self.time_zone_name}"] || "#{e['DTSTART;VALUE=DATE']}T000000"}Z").nil? ? -4*3600 : 0
st = (Time.gcalschema("#{e["DTSTART;TZID=#{self.time_zone_name}"] || "#{e['DTSTART;VALUE=DATE']}T000000"}Z") || Time.gcalschema(e['DTSTART'])) + tzadjust
et = (Time.gcalschema("#{e["DTEND;TZID=#{self.time_zone_name}"] || "#{e['DTEND;VALUE=DATE']}T000000"}Z") || Time.gcalschema(e['DTEND'])) + tzadjust
curl "http://repo.or.cz/w/git.git?a=blob_plain;f=contrib/completion/git-completion.bash;h=4ea727b14303e397117067993dbda446ed154ea1;hb=HEAD" > ~/.git_bash_completion.sh
chmod +x ~/.git_bash_completion.sh
echo " source ~/.git_bash_completion.sh
#PS1='$PS1'
PS1='\\W \$(__git_ps1 \"(%s) \")\\\$ '" >> ~/.profile
@dcparker
dcparker / gist:187412
Created September 15, 2009 16:09
Prerequisites for Rails. Simple way to test for prerequisites for a specific action, and redirect appropriately to fulfill each prereq if not fulfilled.
###########
## File: app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
around_filter :catch_prerequisite
private
def catch_prerequisite
need = catch :prerequisite_unmet do
yield