Skip to content

Commit

Permalink
Added rails specific console for pry and irb
Browse files Browse the repository at this point in the history
  • Loading branch information
apraditya committed Apr 1, 2013
1 parent b87cd6a commit 1a17ca0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 7 deletions.
17 changes: 11 additions & 6 deletions irbrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ rescue LoadError => e
warn "Couldn't load awesome_print: #{e}"
end

# print SQL to STDOUT
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end


# Prompt behavior
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
Expand All @@ -34,6 +28,17 @@ IRB.conf[:EVAL_HISTORY] = 1000
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

# load .irbrc_rails in rails environments
railsrc_path = File.expand_path('~/.irbrc_rails')
if ( ENV['RAILS_ENV'] || defined? Rails ) && File.exist?( railsrc_path )
begin
load railsrc_path
rescue Exception
warn "Could not load: #{ railsrc_path } because of #{$!.message}"
end
end


# Easily print methods local to an object's class
class Object
def local_methods
Expand Down
59 changes: 59 additions & 0 deletions irbrc_rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# hirb: some nice stuff for Rails
begin
require 'hirb'
HIRB_LOADED = true
rescue LoadError
HIRB_LOADED = false
end

require 'logger'

def loud_logger
enable_hirb
set_logger_to Logger.new(STDOUT)
end

def quiet_logger
disable_hirb
set_logger_to nil
end

def set_logger_to(logger)
ActiveRecord::Base.logger = logger
ActiveRecord::Base.clear_reloadable_connections!
end

def enable_hirb
if HIRB_LOADED
Hirb::Formatter.dynamic_config['ActiveRecord::Base']
Hirb.enable
else
puts "hirb is not loaded"
end
end

def disable_hirb
if HIRB_LOADED
Hirb.disable
else
puts "hirb is not loaded"
end
end

def efind(email)
User.find_by_email email
end

# set a nice prompt
rails_root = File.basename(Dir.pwd)
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{rails_root}> ", # normal prompt
:PROMPT_S => "#{rails_root}* ", # prompt when continuing a string
:PROMPT_C => "#{rails_root}? ", # prompt when continuing a statement
:RETURN => "=> %s\n" # prefixes output
}
IRB.conf[:PROMPT_MODE] = :RAILS

# turn on the loud logging by default
IRB.conf[:IRB_RC] = Proc.new { loud_logger }
45 changes: 44 additions & 1 deletion pryrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# Pry config to work with RubyMine
Pry.config.editor = proc { |file, line| "mine --line #{line} #{file}" }
# Pry.config.editor = proc { |file, line| "mine --line #{line} #{file}" }
Pry.config.editor = 'subl'

begin
require 'awesome_print'
AwesomePrint.pry!
rescue LoadError => err
puts "no awesome_print :("
end


# load .irbrc_rails in rails environments
railsrc_path = File.expand_path('~/.pryrc_rails')
if ( ENV['RAILS_ENV'] || defined? Rails ) && File.exist?( railsrc_path )
begin
load railsrc_path
rescue Exception
warn "Could not load: #{ railsrc_path } because of #{$!.message}"
end
end

# Easily print methods local to an object's class
class Object
def local_methods
(methods - Object.instance_methods).sort
end

def interesting_methods
case self.class
when Class
self.public_methods.sort - Object.public_methods
when Module
self.public_methods.sort - Module.public_methods
else
self.public_methods.sort - Object.new.public_methods
end
end
end

# copy a string to the clipboard
def pbcopy(string)
`echo "#{string}" | pbcopy`
string
end
26 changes: 26 additions & 0 deletions pryrc_rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# HIRB
begin
require 'hirb'
rescue LoadError
end


if defined? Hirb
# Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
Hirb::View.instance_eval do
def enable_output_method
@output_method = true
@old_print = Pry.config.print
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || @old_print.call(output, value)
end
end

def disable_output_method
Pry.config.print = @old_print
@output_method = nil
end
end

Hirb.enable
end

0 comments on commit 1a17ca0

Please sign in to comment.