Skip to content

Instantly share code, notes, and snippets.

@foliosus
Created May 24, 2011 22:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foliosus/989897 to your computer and use it in GitHub Desktop.
Save foliosus/989897 to your computer and use it in GitHub Desktop.
Interesting .irbrc hacks, including getting logging to your console, making easy calls to the app from the console, easy loading of accounts
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[1;30m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"
def extend_console(name, care = true, required = true)
if care
require name if required
yield if block_given?
$console_extensions << "#{ANSI[:GREEN]}#{name}#{ANSI[:RESET]}"
else
$console_extensions << "#{ANSI[:GRAY]}#{name}#{ANSI[:RESET]}"
end
rescue LoadError
$console_extensions << "#{ANSI[:RED]}#{name}#{ANSI[:RESET]}"
end
$console_extensions = []
extend_console 'pp'
extend_console 'utility_belt'
if ENV['RAILS_ENV']
load File.dirname(__FILE__) + '/.railsrc'
else
IRB.conf[:PROMPT_MODE] = :SIMPLE
end
puts "#{ANSI[:LGRAY]}~> Console extensions:#{ANSI[:RESET]} #{$console_extensions.join(' ')}#{ANSI[:RESET]}"
# Set up the prompt to be slightly more informative
rails_env = case ENV['RAILS_ENV'].downcase
when 'test' then 'test'
when /local_(.*)/ then 'local_' + $1[0,3]
else ENV['RAILS_ENV'].downcase[0,3]
end
current_app = Dir.pwd.split('/').last
IRB.conf[:PROMPT][:RAILS] ||= {
:PROMPT_I => "#{ANSI[:GREEN]}#{current_app} #{rails_env} >> #{ANSI[:RESET]}",
:PROMPT_N => "#{ANSI[:GREEN]}#{current_app} #{rails_env} >> #{ANSI[:RESET]}",
:PROMPT_S => nil,
:PROMPT_C => "#{ANSI[:YELLOW]}?> #{ANSI[:RESET]}",
:RETURN => "=> %s\n",
:AUTO_INDENT => true }
IRB.conf[:PROMPT_MODE] = :RAILS
extend_console 'ruby-prof' do
def measure
RubyProf.start
yield
mem = '%.2f MB' % (RubyProf.measure_memory / (1024.0 * 1024.0))
RubyProf.stop
mem
end
end
def load_account(account_id)
@account = Account.find(account_id)
@account.set_shard_connection
Thread.current[:metric_account_id] = @account.id
return @account
end
# By default, log to the console, override by passing LOG= something other than 'console' at the command line
ENV['LOG'] ||= 'console' if !Object.const_defined?('RAILS_DEFAULT_LOGGER')
# When you're using Rails 2 console, show queries in the console
extend_console 'rails2', ENV['LOG'] == 'console', false do
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
# When you're using Rails 3 console, show queries in the console
extend_console 'rails3', ENV['LOG'] == 'console' && defined?(ActiveSupport::Notifications), false do
$odd_or_even_queries = false
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
$odd_or_even_queries = !$odd_or_even_queries
color = $odd_or_even_queries ? ANSI[:CYAN] : ANSI[:MAGENTA]
event = ActiveSupport::Notifications::Event.new(*args)
time = "%.1fms" % event.duration
name = event.payload[:name]
sql = event.payload[:sql].gsub("\n", " ").squeeze(" ")
puts " #{ANSI[:UNDERLINE]}#{color}#{name} (#{time})#{ANSI[:RESET]} #{sql}"
end
end
class Object
def app_request(options = {})
method = options.delete(:method) || :get
options.reverse_merge! :only_path => true
ENV['REQUEST_URI'] = app.url_for(options)
ENV['REQUEST_METHOD'] = method.to_s
Dispatcher.dispatch
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment