Skip to content

Instantly share code, notes, and snippets.

@betawaffle
Created November 10, 2011 12:20
Show Gist options
  • Save betawaffle/1354727 to your computer and use it in GitHub Desktop.
Save betawaffle/1354727 to your computer and use it in GitHub Desktop.
File.expand_path('~/etc/pry').tap do |base|
%w(core_ext custom_commands).each { |f| require File.join(base, f) }
end
require File.expand_path('~/etc/pry/pry-rspec.rb')
require File.expand_path('~/etc/pry/pry-helpers.rb')
Pry.config.should_load_plugins = false
Pry.config.auto_indent = false
Pry.config.correct_indent = false
Pry.config.editor = proc { |file, line| "subl -w #{file}:#{line}" }
Pry.config.commands.tap do |c|
c.import CustomCommands::Benchmarking
c.import CustomCommands::ObjectSpace
c.import CustomCommands::Enumeration
c.import CustomCommands::Fibers
c.import RSpecCommands
c.command 'load-rails', 'Load the Rails environment.' do |*args|
rails = File.join(Dir.getwd, 'config', 'environment.rb')
unless File.exists? rails
output.puts 'No Rails environment to load.'
next
end
args.compact!
opts = Slop.parse!(args) do |opt|
opt.on :s, :sandbox, 'Rollback database modifications on exit.'
opt.on :'pry-app', 'Break when application class is created.'
end
env = args.shift || 'development'
orig = Kernel.method(:require)
orig.append do |f|
next unless f == 'rails'
output.puts " Rails #{Rails.version} Loaded"
orig.redefine
orig = Rails::Application.method(:inherited)
orig.append do |app|
app.sandbox = opts[:sandbox] if app.respond_to? :sandbox=
output.puts " Rails Application Created"
if opts[:'pry-app']
file, line, _ = caller[2].split(':', 3)
Pry.start Pry.binding_for(app).fake(file, line)
end
end
Rails.logger = Logger.new(output)
end
ENV['RAILS_ENV'] = env
output.puts "Loading Rails Environment... \e[0;37;42m #{ENV['RAILS_ENV'].upcase} \e[0m"
require rails
output.puts 'Loading Rails Console...'
case Rails.version[0..0]
when '2'
require 'console_app'
require 'console_with_helpers'
when '3'
require 'rails/console/app'
require 'rails/console/helpers'
else
warn " \e[0;33mFailed:\e[0m Loading Rails Console"
end
load_hirb
init_hirb
end
c.helpers do
def load_hirb
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
def init_hirb
return unless defined? Hirb
extend Hirb::Console
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || Pry::DEFAULT_PRINT.call(output, value)
end
Hirb.enable
end
end
=begin
c.command 'live-trace', 'Run a trace.' do
file = nil
line = nil
called = false
tracer = lambda do |e, f, l, m, b, k|
return if method == :set_trace_func
if f != file
output.printf "\n\e[1m%s\e[0m\n", f
end
if l == line + 1
output.printf "\e[0;32m%5d\e[0m: ", l
line = l
else
output.printf "%8s", e
end
end
end
=end
end
# Pry.prompt = [
# proc { |obj, nest_level| "#{Fiber.current.name} (#{obj}):#{nest_level} > " },
# proc { |obj, nest_level| "#{Fiber.current.name} (#{obj}):#{nest_level} * " }
# ]
Pry.prompt = [
proc { |obj, nest_level| "(#{obj}):#{nest_level} > " },
proc { |obj, nest_level| "(#{obj}):#{nest_level} * " }
]
Pry.plugins['doc'].activate!
class Task
def initialize(*args, &block)
define_singleton_method(:perform, &block) if block
@fiber = method(:perform).to_fiber
end
def resume(*args)
@fiber.resume(*args)
end
end
if defined? Hirb
extend Hirb::Console
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment