Rough setup for a custom #inspect format.

This format is a lot more readable than the default Ruby #inspect format
(mostly due to not including previous/next/parent nodes).
This commit is contained in:
Yorick Peterse 2014-04-04 00:41:29 +02:00
parent a2c525dd7c
commit 37a12722cb
6 changed files with 70 additions and 0 deletions

View File

@ -24,6 +24,19 @@ module Oga
return segments + '>' return segments + '>'
end end
def inspect(indent = 0)
class_name = self.class.to_s.split('::').last
return <<-EOF.strip
#{class_name}(
name: #{name.inspect}
type: #{type.inspect}
public_id: #{public_id.inspect}
system_id: #{system_id.inspect}
)
EOF
end
end # Doctype end # Doctype
end # XML end # XML
end # Oga end # Oga

View File

@ -30,6 +30,20 @@ module Oga
return xml return xml
end end
def inspect
class_name = self.class.to_s.split('::').last
child_lines = children.map { |child| child.inspect(4) }.join("\n")
return <<-EOF.strip
#{class_name}(
doctype: #{doctype.inspect}
xml_declaration: #{xml_declaration.inspect}
children: [
#{child_lines}
])
EOF
end
end # Document end # Document
end # XML end # XML
end # Oga end # Oga

View File

@ -30,6 +30,22 @@ module Oga
return "<#{ns}#{name}#{attrs}>#{body}</#{name}>" return "<#{ns}#{name}#{attrs}>#{body}</#{name}>"
end end
def extra_inspect_data(indent)
spacing = ' ' * indent
child_lines = children.map { |child| child.inspect(indent + 4) }
.join("\n")
return <<-EOF.chomp
#{spacing} name: #{name.inspect}
#{spacing} namespace: #{namespace.inspect}
#{spacing} attributes: #{attributes.inspect}
#{spacing} children: [
#{child_lines}
#{spacing}]
EOF
end
end # Element end # Element
end # XML end # XML
end # Oga end # Oga

View File

@ -35,6 +35,16 @@ module Oga
after_initialize if respond_to?(:after_initialize) after_initialize if respond_to?(:after_initialize)
end end
def inspect(indent = 0)
class_name = self.class.to_s.split('::').last
spacing = ' ' * indent
return "#{spacing}#{class_name}(#{extra_inspect_data(indent)})"
end
def extra_inspect_data
end
end # Element end # Element
end # XML end # XML
end # Oga end # Oga

View File

@ -8,6 +8,10 @@ module Oga
def to_xml def to_xml
return text.to_s return text.to_s
end end
def extra_inspect_data(indent)
return "text: #{text.inspect}"
end
end # Text end # Text
end # XML end # XML
end # Oga end # Oga

View File

@ -33,6 +33,19 @@ module Oga
return "<?xml #{pairs.join(' ')} ?>" return "<?xml #{pairs.join(' ')} ?>"
end end
def inspect
class_name = self.class.to_s.split('::').last
return <<-EOF.strip
#{class_name}(
version: #{version.inspect}
encoding: #{encoding.inspect}
standalone: #{standalone.inspect}
)
EOF
end
end # XmlDeclaration end # XmlDeclaration
end # XML end # XML
end # Oga end # Oga