From 0872a955b73a26f35abccaeab79cbb610a99da53 Mon Sep 17 00:00:00 2001 From: Mike Dvorkin Date: Tue, 11 Sep 2012 15:41:33 -0700 Subject: [PATCH] Added :raw => false option to avoid deep object formatting by default --- README.md | 1 + lib/awesome_print/formatter.rb | 7 +++++-- lib/awesome_print/inspector.rb | 1 + spec/objects_spec.rb | 16 +++++++++++----- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7f4d858..46987a8 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Default options: :html => false, # Use ANSI color codes rather than HTML. :multiline => true, # Display in multiple lines. :plain => false, # Use colors. + :raw => false, # Do not recursively format object instance variables. :sort_keys => false, # Do not sort hash keys. :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. :color => { diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 7f92c95..911d2dd 100755 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -61,8 +61,11 @@ module AwesomePrint # Catch all method to format an arbitrary object. #------------------------------------------------------------------------------ def awesome_self(object, type) - return awesome_object(object) if object.instance_variables.any? - colorize(object.inspect.to_s, type) + if @options[:raw] && object.instance_variables.any? + awesome_object(object) + else + colorize(object.inspect.to_s, type) + end end # Format an array. diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index eeb0b5e..ad2d620 100755 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -59,6 +59,7 @@ module AwesomePrint :html => false, # Use ANSI color codes rather than HTML. :multiline => true, # Display in multiple lines. :plain => false, # Use colors. + :raw => false, # Do not recursively format object instance variables. :sort_keys => false, # Do not sort hash keys. :limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. :color => { diff --git a/spec/objects_spec.rb b/spec/objects_spec.rb index c4e62b5..1802e9a 100644 --- a/spec/objects_spec.rb +++ b/spec/objects_spec.rb @@ -1,6 +1,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') -describe "Single method" do +describe "Objects" do before do stub_dotfile! end @@ -9,7 +9,7 @@ describe "Single method" do Object.instance_eval{ remove_const :Hello } if defined?(Hello) end - describe "object" do + describe "Formatting an object" do it "attributes" do class Hello attr_reader :abra @@ -21,7 +21,8 @@ describe "Single method" do end end - out = Hello.new.ai(:plain => true) + hello = Hello.new + out = hello.ai(:plain => true, :raw => true) str = <<-EOS.strip # EOS out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str + hello.ai(:plain => true, :raw => false).should == hello.inspect end it "instance variables" do @@ -39,7 +41,8 @@ EOS end end - out = Hello.new.ai(:plain => true) + hello = Hello.new + out = hello.ai(:plain => true, :raw => true) str = <<-EOS.strip # EOS out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str + hello.ai(:plain => true, :raw => false).should == hello.inspect end it "attributes and instance variables" do @@ -62,7 +66,8 @@ EOS end end - out = Hello.new.ai(:plain => true) + hello = Hello.new + out = hello.ai(:plain => true, :raw => true) str = <<-EOS.strip # EOS out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str + hello.ai(:plain => true, :raw => false).should == hello.inspect end end end