%w{rubygems wirble what_methods}.each { |lib| require lib }
%w{init colorize}.each { |message| Wirble.send(message) }

# clipboard code: http://project.ioni.st/post/1334#snippet_1334
class MacClipboard
class << self
def read
IO.popen('pbpaste') {|clipboard| clipboard.read}
end
def write(stuff)
IO.popen('pbcopy', 'w+') {|clipboard| clipboard.write(stuff)}
end
end
end

# automate creating pasties
require 'net/http'
def pastie
url =
pastie_url = Net::HTTP.post_form(URI.parse("http://pastie.caboo.se/pastes/create"),
{"paste_parser" => "ruby",
"paste[authorization]" => "burger",
"paste[body]" => MacClipboard.read}).body.match(/href="([^\"]+)"/)[1]
MacClipboard.write(pastie_url)
system("open #{pastie_url}")
pastie_url
end
alias :pst :pastie

# make commas light purple
Wirble::Colorize.colors = Wirble::Colorize::DEFAULT_COLORS.merge(:comma => :light_purple)

# auto indent, _ special var
IRB.conf[:AUTO_INDENT]=true
IRB.conf[:EVAL_HISTORY] = 1000

# verbosity: http://groups.google.com/group/ruby-talk-google/browse_thread/thread/9c1febbe05513dc0
module IRB
def self.result_format
conf[:PROMPT][conf[:PROMPT_MODE]][:RETURN]
end
def self.result_format=(str)
result_format.replace(str)
end
def self.show_results
self.result_format = "=> %s\n"
end
def self.hide_results
self.result_format = ''
end
end

def verbose
IRB.show_results
end
alias :v :verbose

def quiet
IRB.hide_results
end
alias :q :quiet

alias :x :exit

# verbosity: http://weblog.jamisbuck.org/2007/1/8/watching-activerecord-do-it-s-thing
# Marcel said they toyed with making this the console default on core
def log
ActiveRecord::Base.clear_active_connections!
ActiveRecord::Base.logger = Logger.new(STDOUT)
end

# http://www.notsostupid.com/blog/2006/11/09/pm-print-methods/
PM_RESET = "\e[0m"
PM_BOLD = "\e[1m"
PM_GRAY = "\e[1;30m"
PM_LGRAY = "\e[0;37m"

def pm(obj, *options) # Print methods
methods = obj.methods - (options.include?(:more) ? [] : Object.methods)
filter = options.select {|opt| opt.kind_of? Regexp}.first
methods = methods.select {|name| name =~ filter} if filter

data = methods.sort.collect do |name|
method = obj.method(name)
args = "(" + case method.arity <=> 0
when 1
("a"..(?a + method.arity - 1).chr).to_a.join(", ")
when -1
("a"..(?a - method.arity - 1).chr).to_a.join(", ")
else
""
end + ")"
klass = $1 if method.inspect =~ /Method: (.*)#/
klass = $1 if klass =~ /\((.*)\)/
[name, args, klass]
end
max_name_length = data.collect {|item| item[0].size}.max
max_args_length = data.collect {|item| item[1].size}.max
data.each do |item|
print " #{PM_BOLD}#{item[0].rjust(max_name_length)}#{PM_RESET}"
print "#{PM_BOLD}#{item[1].ljust(max_args_length)}#{PM_RESET}"
print " #{PM_BOLD}#{item[2]}#{PM_RESET} \n"
end
data.size
end


# history
# http://blog.bleything.net/pages
# http://gilesbowkett.blogspot.com/2007/06/irbrc-modifications.html

def history(how_many = 50)
history_size = Readline::HISTORY.size
# no lines, get out of here
puts "No history" and return if history_size == 0
start_index = 0
# not enough lines, only show what we have
if history_size <= how_many
how_many = history_size - 1
end_index = how_many
else
end_index = history_size - 1 # -1 to adjust for array offset
start_index = end_index - how_many
end
start_index.upto(end_index) {|i| print_line i}
nil
end
alias :h :history

# -2 because -1 is ourself
def history_do(lines = (Readline::HISTORY.size - 2))
irb_eval lines
end
alias :h! :history_do

def history_write(filename, lines)
file = File.open(filename, 'w')
get_lines(lines).each do |l|
file << "#{l}\n"
end
file.close
end
alias :hw :history_write

private
def get_line(line_number)
Readline::HISTORY[line_number]
end

def get_lines(lines = [])
return [get_line(lines)] if lines.is_a? Fixnum
out = []
lines = lines.to_a if lines.is_a? Range
lines.each do |l|
out << Readline::HISTORY[l]
end
out
end

def print_line(line_number, show_line_numbers = true)
print line_number.to_s + ": " if show_line_numbers
puts get_line(line_number)
end

def irb_eval(lines)
to_eval = get_lines(lines)
to_eval.each {|l| Readline::HISTORY << l}
eval to_eval.join("\n")
end

# http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
class Object
define_method :not do
Not.new(self)
end
class Not
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
def initialize(subject)
@subject = subject
end
def method_missing(sym, *args, &blk)
!@subject.send(sym,*args,&blk)
end
end
end

# http://gilesbowkett.blogspot.com/2006/12/smalltalk-cleverness-translated-into.html
# http://gilesbowkett.com/blog_code_samples/122906_seaside_rails/controller.txt
def grep_classes(search_term)
classes = []
ObjectSpace.each_object {|object| classes << object.name if object.is_a? Class and object.not.name.blank?}
classes.find_all {|klass| klass.downcase.include? search_term.downcase}
end

# http://www.clarkware.com/cgi/blosxom/2007/09/03#ConsoleFindShortcut
# Mike Clark's find() shortcut for Rails console

# Creates shortcut methods for finding models.
def define_model_find_shortcuts
model_files = Dir.glob("app/models/**/*.rb")
table_names = model_files.map { |f| File.basename(f).split('.')[0..-2].join }
table_names.each do |table_name|
Object.instance_eval do
define_method(table_name) do |*args|
table_name.camelize.constantize.send(:find, *args)
end
end
end
end
# note: Mike wrote this for ARec, but it works on ARes too since it doesn't hit the DB

# Called when the irb session is ready, after
# the Rails goodies used above have been loaded.
IRB.conf[:IRB_RC] = Proc.new do
define_model_find_shortcuts
end