1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Add feature to print Structs like Objects

This commit is contained in:
Waldyr 2016-04-14 22:47:53 -03:00 committed by Waldyr de Souza
parent 68431d0475
commit e4a26d28a5
2 changed files with 25 additions and 40 deletions

View file

@ -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.

View file

@ -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