Ripped out inspect fuckery.

The old code used for generating Object#inspect values has been ripped out (for
the most part). The result is a non indented but far more compact #inspect
output. The code for this is also easier and doesn't break the signature of
Object#inspect.
This commit is contained in:
Yorick Peterse 2014-08-07 21:09:10 +02:00
parent 798372d099
commit b1388ff84a
10 changed files with 38 additions and 83 deletions

View File

@ -115,10 +115,9 @@ module Oga
# @return [String]
#
def inspect
segments = []
child_lines = children.map { |child| child.inspect(4) }.join("\n")
segments = []
[:doctype, :xml_declaration].each do |attr|
[:doctype, :xml_declaration, :children].each do |attr|
value = send(attr)
if value
@ -126,9 +125,11 @@ module Oga
end
end
segments << "children: [\n#{child_lines}\n]"
return "Document(\n #{segments.join("\n ")})"
return <<-EOF.strip
Document(
#{segments.join("\n ")}
)
EOF
end
end # Document
end # XML

View File

@ -114,19 +114,12 @@ module Oga
end
##
# Returns extra data to use when calling {#inspect} on an element.
#
# @param [Fixnum] indent
# @return [String]
#
def extra_inspect_data(indent)
spacing = ' ' * indent
child_lines = children.map { |child| child.inspect(indent + 4) }
.join("\n")
def inspect
segments = []
[:name, :namespace, :attributes].each do |attr|
[:name, :namespace, :attributes, :children].each do |attr|
value = send(attr)
if !value or (value.respond_to?(:empty?) and value.empty?)
@ -136,13 +129,7 @@ module Oga
segments << "#{attr}: #{value.inspect}"
end
return <<-EOF.chomp
#{spacing} #{segments.join("\n#{spacing} ")}
#{spacing} children: [
#{child_lines}
#{spacing}]
EOF
return "Element(#{segments.join(' ')})"
end
##

View File

@ -140,26 +140,6 @@ module Oga
return node_set.delete(self) if node_set
end
##
# Generates the inspect value for the current node. Sub classes can
# overwrite the {#extra_inspect_data} method to customize the output
# format.
#
# @param [Fixnum] indent
# @return [String]
#
def inspect(indent = 0)
class_name = self.class.to_s.split('::').last
spacing = ' ' * indent
return "#{spacing}#{class_name}(#{extra_inspect_data(indent)})"
end
##
# @return [String]
#
def extra_inspect_data; end
##
# @return [Symbol]
#

View File

@ -252,6 +252,15 @@ module Oga
return text
end
##
# @return [String]
#
def inspect
values = @nodes.map(&:inspect).join(', ')
return "NodeSet(#{values})"
end
private
##

View File

@ -29,11 +29,10 @@ module Oga
end
##
# @param [Fixnum] indent
# @return [String]
#
def extra_inspect_data(indent)
return "text: #{text.inspect}"
def inspect
return "#{self.class.to_s.split('::').last}(#{text.inspect})"
end
##

View File

@ -29,8 +29,8 @@ describe Oga::XML::Cdata do
@instance = described_class.new(:text => 'foo')
end
example 'pretty-print the node' do
@instance.inspect.should == 'Cdata(text: "foo")'
example 'return the inspect value' do
@instance.inspect.should == 'Cdata("foo")'
end
end

View File

@ -29,8 +29,8 @@ describe Oga::XML::Comment do
@instance = described_class.new(:text => 'foo')
end
example 'pretty-print the node' do
@instance.inspect.should == 'Comment(text: "foo")'
example 'return the inspect value' do
@instance.inspect.should == 'Comment("foo")'
end
end

View File

@ -143,23 +143,21 @@ describe Oga::XML::Document do
)
end
example 'pretty-print the node' do
example 'return the inspect value' do
@instance.inspect.should == <<-EOF.strip
Document(
doctype: Doctype(name: "html")
xml_declaration: XmlDeclaration(version: "1.0" encoding: "UTF-8")
children: [
Comment(text: "foo")
])
children: NodeSet(Comment("foo"))
)
EOF
end
example 'pretty-print a document without a doctype and XML declaration' do
example 'return the inspect value of an empty document' do
described_class.new.inspect.should == <<-EOF.strip
Document(
children: [
])
children: NodeSet()
)
EOF
end
end

View File

@ -142,30 +142,18 @@ describe Oga::XML::Element do
example 'inspect a node with a name' do
node = described_class.new(:name => 'a')
node.inspect.should == <<-EOF.strip
Element(
name: "a"
children: [
])
EOF
node.inspect.should == 'Element(name: "a")'
end
example 'inspect a node with attributes and children' do
node = described_class.new(
:name => 'p',
:children => [Oga::XML::Comment.new(:text => 'foo')],
:attributes => {'class' => 'foo'}
:attributes => [Oga::XML::Attribute.new(:name => 'x', :value => 'y')]
)
node.inspect.should == <<-EOF.strip
Element(
name: "p"
attributes: {"class"=>"foo"}
children: [
Comment(text: "foo")
])
EOF
node.inspect.should == 'Element(name: "p" attributes: ' \
'[Attribute(name: "x" value: "y")] children: NodeSet(Comment("foo")))'
end
example 'inspect a node with a namespace' do
@ -174,14 +162,7 @@ Element(
:namespace => Oga::XML::Namespace.new(:name => 'x')
)
node.inspect.should == <<-EOF.strip
Element(
name: "p"
namespace: Namespace(name: "x")
children: [
])
EOF
node.inspect.should == 'Element(name: "p" namespace: Namespace(name: "x"))'
end
end

View File

@ -29,8 +29,8 @@ describe Oga::XML::Text do
@instance = described_class.new(:text => 'foo')
end
example 'pretty-print the node' do
@instance.inspect.should == 'Text(text: "foo")'
example 'return the inspect value' do
@instance.inspect.should == 'Text("foo")'
end
end