From 67cde1be8d488a042ff2d79a5cd1143862155a17 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Wed, 5 Sep 2012 19:41:54 -0700 Subject: [PATCH] Load necessary files when Rails console is running under Pry --- lib/awesome_print.rb | 16 +++++++++------- lib/awesome_print/inspector.rb | 8 ++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 2d16f87..7da8630 100755 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -4,8 +4,8 @@ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ # -# AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing -# for subsequent requires. +# AwesomePrint might be loaded implicitly through ~/.irbrc or ~/.pryrc +# so do nothing for subsequent requires. # unless defined?(AwesomePrint::Inspector) %w(array string method object class kernel).each do |file| @@ -16,13 +16,15 @@ unless defined?(AwesomePrint::Inspector) require File.dirname(__FILE__) + "/awesome_print/formatter" require File.dirname(__FILE__) + "/awesome_print/version" require File.dirname(__FILE__) + "/awesome_print/core_ext/logger" if defined?(Logger) - + # # Load the following under normal circumstances as well as in Rails - # console when required from ~/.irbrc. - require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) - require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) - + # console when required from ~/.irbrc or ~/.pryrc. + # + require File.dirname(__FILE__) + "/awesome_print/ext/active_record" if defined?(ActiveRecord) || AwesomePrint.rails_console? + require File.dirname(__FILE__) + "/awesome_print/ext/active_support" if defined?(ActiveSupport) || AwesomePrint.rails_console? + # # Load remaining extensions. + # require File.dirname(__FILE__) + "/awesome_print/ext/action_view" if defined?(ActionView::Base) require File.dirname(__FILE__) + "/awesome_print/ext/mongo_mapper" if defined?(MongoMapper) require File.dirname(__FILE__) + "/awesome_print/ext/mongoid" if defined?(Mongoid) diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 7ce241a..1c17d7e 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -14,6 +14,14 @@ module AwesomePrint def force_colors!(value = true) @force_colors = value end + + def console? + defined?(IRB) || defined?(Pry) + end + + def rails_console? + console? && (defined?(Rails::Console) || ENV["RAILS_ENV"]) + end end class Inspector