From 24b6192f57d2e3b7483057c8c4e7024d0ff6d3cc Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Fri, 13 May 2011 14:42:04 -0700 Subject: [PATCH] Avoid loading awesome_print more than once (ex. ~/.irbrc and then Rails console) --- lib/ap.rb | 30 ++++++++++++++++++------------ lib/awesome_print.rb | 29 +++++++++++++++-------------- spec/awesome_print_spec.rb | 6 ++++++ spec/spec_helper.rb | 2 +- 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/lib/ap.rb b/lib/ap.rb index 149fe30..ebeb7d4 100755 --- a/lib/ap.rb +++ b/lib/ap.rb @@ -3,16 +3,22 @@ # Awesome Print is freely distributable under the terms of MIT license. # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ -%w(array string method object class kernel).each do |file| - require File.dirname(__FILE__) + "/ap/core_ext/#{file}" +# +# AwesomePrint might be loaded implicitly through ~/.irbrc so do nothing +# for subsequent requires. +# +unless defined?(AwesomePrint) + %w(array string method object class kernel).each do |file| + require File.dirname(__FILE__) + "/ap/core_ext/#{file}" + end + + require File.dirname(__FILE__) + "/ap/awesome_print" + require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger) + require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView) + + # Load the following under normal circumstatnces as well as in Rails + # console when required from ~/.irbrc. + require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) + require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) + require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper) end - -require File.dirname(__FILE__) + "/ap/awesome_print" -require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger) -require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView) - -# Load the following under normal circumstatnces as well as in Rails -# console when required from ~/.irbrc. -require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) -require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) -require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper) diff --git a/lib/awesome_print.rb b/lib/awesome_print.rb index 268239a..01eeba5 100755 --- a/lib/awesome_print.rb +++ b/lib/awesome_print.rb @@ -7,19 +7,20 @@ # This is the copy of original 'ap.rb' file that matches the gem name. It makes # it possible to omit the :require part in bundler's Gemfile: # -# gem 'awesome_print', '>= 0.2.1', :require => 'ap' -# gem 'awesome_print', '>= 3.0.0' +# gem 'awesome_print', '>= 0.4.0' # -%w(array string method object class kernel).each do |file| - require File.dirname(__FILE__) + "/ap/core_ext/#{file}" +unless defined?(AwesomePrint) + %w(array string method object class kernel).each do |file| + require File.dirname(__FILE__) + "/ap/core_ext/#{file}" + end + + require File.dirname(__FILE__) + "/ap/awesome_print" + require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger) + require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView) + + # Load the following under normal circumstatnces as well as in Rails + # console when required from ~/.irbrc. + require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) + require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) + require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper) end - -require File.dirname(__FILE__) + "/ap/awesome_print" -require File.dirname(__FILE__) + "/ap/core_ext/logger" if defined?(Logger) -require File.dirname(__FILE__) + "/ap/mixin/action_view" if defined?(ActionView) - -# Load the following under normal circumstatnces as well as in Rails -# console when required from ~/.irbrc. -require File.dirname(__FILE__) + "/ap/mixin/active_record" if defined?(ActiveRecord) || (defined?(IRB) && ENV['RAILS_ENV']) -require File.dirname(__FILE__) + "/ap/mixin/active_support" if defined?(ActiveSupport) || (defined?(IRB) && ENV['RAILS_ENV']) -require File.dirname(__FILE__) + "/ap/mixin/mongo_mapper" if defined?(MongoMapper) \ No newline at end of file diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index 8e3c731..1bbf949 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -467,6 +467,12 @@ EOS self.stub!(:puts) (ap object).should == object end + + # Require different file name this time (lib/ap.rb vs. lib/awesome_print). + it "several require 'awesome_print' should do no harm" do + require File.expand_path(File.dirname(__FILE__) + '/../lib/ap') + lambda { rand.ai }.should_not raise_error + end end describe "HTML output" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 81a61bf..2e90293 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,7 +10,7 @@ # $LOAD_PATH.unshift(File.dirname(__FILE__)) $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) -require 'ap' +require 'awesome_print' def stub_dotfile! dotfile = File.join(ENV["HOME"], ".aprc")