From 37a12722cb9df3edf60c1e3fdb9e12474dd26849 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 4 Apr 2014 00:41:29 +0200 Subject: [PATCH] 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). --- lib/oga/xml/doctype.rb | 13 +++++++++++++ lib/oga/xml/document.rb | 14 ++++++++++++++ lib/oga/xml/element.rb | 16 ++++++++++++++++ lib/oga/xml/node.rb | 10 ++++++++++ lib/oga/xml/text.rb | 4 ++++ lib/oga/xml/xml_declaration.rb | 13 +++++++++++++ 6 files changed, 70 insertions(+) diff --git a/lib/oga/xml/doctype.rb b/lib/oga/xml/doctype.rb index 4fe8359..477f7fe 100644 --- a/lib/oga/xml/doctype.rb +++ b/lib/oga/xml/doctype.rb @@ -24,6 +24,19 @@ module Oga return segments + '>' 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 # XML end # Oga diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index f93dfcf..4be2911 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -30,6 +30,20 @@ module Oga return xml 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 # XML end # Oga diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 4e057b8..34ec8f1 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -30,6 +30,22 @@ module Oga return "<#{ns}#{name}#{attrs}>#{body}" 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 # XML end # Oga diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index 7afa4e5..a592d58 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -35,6 +35,16 @@ module Oga after_initialize if respond_to?(:after_initialize) 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 # XML end # Oga diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index dc0d211..fcfc9e8 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -8,6 +8,10 @@ module Oga def to_xml return text.to_s end + + def extra_inspect_data(indent) + return "text: #{text.inspect}" + end end # Text end # XML end # Oga diff --git a/lib/oga/xml/xml_declaration.rb b/lib/oga/xml/xml_declaration.rb index 8642a88..740b65b 100644 --- a/lib/oga/xml/xml_declaration.rb +++ b/lib/oga/xml/xml_declaration.rb @@ -33,6 +33,19 @@ module Oga return "" 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 # XML end # Oga