mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Merge pull request #226 from waldyr/feature/print-struct-like-objects
Feature/print struct like objects
This commit is contained in:
commit
9dcd4a3f65
3 changed files with 39 additions and 40 deletions
|
@ -2,6 +2,8 @@
|
|||
- Improves spec performance and simplicity (Mauro George)
|
||||
- Handle objects that have a custom #to_hash method (Elliot Shank)
|
||||
- Fixes development dependencies for environments without rake (Edgar Cabrera #222 & Timothée Peignier #216)
|
||||
- Creates #awesome_object_data to encapsulate the logic of printing objects'
|
||||
internals so Structs and Objects can be printed as one (Waldyr de Souza)
|
||||
|
||||
1.6.1
|
||||
- Fixes specs on all rails dependencies (Mauro George)
|
||||
|
|
|
@ -58,6 +58,12 @@ module AwesomePrint
|
|||
|
||||
private
|
||||
|
||||
# Check whether a variable_name is a method or ivar
|
||||
#------------------------------------------------------------------------------
|
||||
def valid_instance_var?(variable_name)
|
||||
variable_name.to_s.start_with?('@')
|
||||
end
|
||||
|
||||
# Catch all method to format an arbitrary object.
|
||||
#------------------------------------------------------------------------------
|
||||
def awesome_self(object, type)
|
||||
|
@ -127,7 +133,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 +164,13 @@ module AwesomePrint
|
|||
end
|
||||
end
|
||||
indented do
|
||||
key << colorize(" = ", :hash) + @inspector.awesome(o.instance_variable_get(var))
|
||||
var_contents = if valid_instance_var?(var)
|
||||
o.instance_variable_get(var)
|
||||
else
|
||||
o.send(var) # Enables handling of Struct attributes
|
||||
end
|
||||
|
||||
key << colorize(" = ", :hash) + @inspector.awesome(var_contents)
|
||||
end
|
||||
end
|
||||
if @options[:multiline]
|
||||
|
@ -173,14 +189,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.
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue