diff --git a/lib/awesome_print/formatter.rb b/lib/awesome_print/formatter.rb index 0e07ecb..d610efe 100644 --- a/lib/awesome_print/formatter.rb +++ b/lib/awesome_print/formatter.rb @@ -127,7 +127,11 @@ module AwesomePrint # Format an object. #------------------------------------------------------------------------------ def awesome_object(o) - vars = o.instance_variables.map do |var| + awesome_object_data(o, o.instance_variables) + end + + def awesome_object_data(o, variables) + vars = variables.map do |var| property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet. accessor = if o.respond_to?(:"#{property}=") o.respond_to?(property) ? :accessor : :writer @@ -154,7 +158,7 @@ module AwesomePrint end end indented do - key << colorize(" = ", :hash) + @inspector.awesome(o.instance_variable_get(var)) + key << colorize(" = ", :hash) + @inspector.awesome(o.send(var)) end end if @options[:multiline] @@ -173,14 +177,7 @@ module AwesomePrint # Format a Struct. #------------------------------------------------------------------------------ def awesome_struct(s) - # - # The code is slightly uglier because of Ruby 1.8.6 quirks: - # awesome_hash(Hash[s.members.zip(s.values)]) <-- ArgumentError: odd number of arguments for Hash) - # awesome_hash(Hash[*s.members.zip(s.values).flatten]) <-- s.members returns strings, not symbols. - # - hash = {} - s.each_pair { |key, value| hash[key] = value } - awesome_hash(hash) + awesome_object_data(s, s.members) end # Format Class object. diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index f4106e3..f806bf3 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -576,56 +576,44 @@ EOS it "plain multiline" do s1 = <<-EOS.strip -{ - :address => "1313 Mockingbird Lane", - :name => "Herman Munster" -} + address = \"1313 Mockingbird Lane\", + name = \"Herman Munster\" EOS s2 = <<-EOS.strip -{ - :name => "Herman Munster", - :address => "1313 Mockingbird Lane" -} + name = \"Herman Munster\", + address = \"1313 Mockingbird Lane\" EOS - expect(@struct.ai(:plain => true)).to satisfy { |match| match == s1 || match == s2 } + expect(@struct.ai(:plain => true)).to satisfy { |out| out.match(s1) || out.match(s2) } end it "plain multiline indented" do s1 = <<-EOS.strip -{ - :address => "1313 Mockingbird Lane", - :name => "Herman Munster" -} + address = "1313 Mockingbird Lane", + name = "Herman Munster" EOS s2 = <<-EOS.strip -{ - :name => "Herman Munster", - :address => "1313 Mockingbird Lane" -} + name = "Herman Munster", + address = "1313 Mockingbird Lane" EOS - expect(@struct.ai(:plain => true, :indent => 1)).to satisfy { |match| match == s1 || match == s2 } + expect(@struct.ai(:plain => true, :indent => 1)).to satisfy { |out| out.match(s1) || out.match(s2) } end it "plain single line" do - s1 = "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }" - s2 = "{ :name => \"Herman Munster\", :address => \"1313 Mockingbird Lane\" }" - expect(@struct.ai(:plain => true, :multiline => false)).to satisfy { |match| match == s1 || match == s2 } + s1 = "address = \"1313 Mockingbird Lane\", name = \"Herman Munster\"" + s2 = "name = \"Herman Munster\", address = \"1313 Mockingbird Lane\"" + expect(@struct.ai(:plain => true, :multiline => false)).to satisfy { |out| out.match(s1) || out.match(s2) } end it "colored multiline (default)" do s1 = <<-EOS.strip -{ - :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m, - :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m -} + address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m, + name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m EOS s2 = <<-EOS.strip -{ - :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m, - :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m -} + name\e[0;37m = \e[0m\e[0;33m\"Herman Munster\"\e[0m, + address\e[0;37m = \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m EOS - expect(@struct.ai).to satisfy { |match| match == s1 || match == s2 } + expect(@struct.ai).to satisfy { |out| out.include?(s1) || out.include?(s2) } end end