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:
parent
798372d099
commit
b1388ff84a
|
@ -115,10 +115,9 @@ module Oga
|
||||||
# @return [String]
|
# @return [String]
|
||||||
#
|
#
|
||||||
def inspect
|
def inspect
|
||||||
segments = []
|
segments = []
|
||||||
child_lines = children.map { |child| child.inspect(4) }.join("\n")
|
|
||||||
|
|
||||||
[:doctype, :xml_declaration].each do |attr|
|
[:doctype, :xml_declaration, :children].each do |attr|
|
||||||
value = send(attr)
|
value = send(attr)
|
||||||
|
|
||||||
if value
|
if value
|
||||||
|
@ -126,9 +125,11 @@ module Oga
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
segments << "children: [\n#{child_lines}\n]"
|
return <<-EOF.strip
|
||||||
|
Document(
|
||||||
return "Document(\n #{segments.join("\n ")})"
|
#{segments.join("\n ")}
|
||||||
|
)
|
||||||
|
EOF
|
||||||
end
|
end
|
||||||
end # Document
|
end # Document
|
||||||
end # XML
|
end # XML
|
||||||
|
|
|
@ -114,19 +114,12 @@ module Oga
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Returns extra data to use when calling {#inspect} on an element.
|
|
||||||
#
|
|
||||||
# @param [Fixnum] indent
|
|
||||||
# @return [String]
|
# @return [String]
|
||||||
#
|
#
|
||||||
def extra_inspect_data(indent)
|
def inspect
|
||||||
spacing = ' ' * indent
|
|
||||||
child_lines = children.map { |child| child.inspect(indent + 4) }
|
|
||||||
.join("\n")
|
|
||||||
|
|
||||||
segments = []
|
segments = []
|
||||||
|
|
||||||
[:name, :namespace, :attributes].each do |attr|
|
[:name, :namespace, :attributes, :children].each do |attr|
|
||||||
value = send(attr)
|
value = send(attr)
|
||||||
|
|
||||||
if !value or (value.respond_to?(:empty?) and value.empty?)
|
if !value or (value.respond_to?(:empty?) and value.empty?)
|
||||||
|
@ -136,13 +129,7 @@ module Oga
|
||||||
segments << "#{attr}: #{value.inspect}"
|
segments << "#{attr}: #{value.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
return <<-EOF.chomp
|
return "Element(#{segments.join(' ')})"
|
||||||
|
|
||||||
#{spacing} #{segments.join("\n#{spacing} ")}
|
|
||||||
#{spacing} children: [
|
|
||||||
#{child_lines}
|
|
||||||
#{spacing}]
|
|
||||||
EOF
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -140,26 +140,6 @@ module Oga
|
||||||
return node_set.delete(self) if node_set
|
return node_set.delete(self) if node_set
|
||||||
end
|
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]
|
# @return [Symbol]
|
||||||
#
|
#
|
||||||
|
|
|
@ -252,6 +252,15 @@ module Oga
|
||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
def inspect
|
||||||
|
values = @nodes.map(&:inspect).join(', ')
|
||||||
|
|
||||||
|
return "NodeSet(#{values})"
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -29,11 +29,10 @@ module Oga
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# @param [Fixnum] indent
|
|
||||||
# @return [String]
|
# @return [String]
|
||||||
#
|
#
|
||||||
def extra_inspect_data(indent)
|
def inspect
|
||||||
return "text: #{text.inspect}"
|
return "#{self.class.to_s.split('::').last}(#{text.inspect})"
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -29,8 +29,8 @@ describe Oga::XML::Cdata do
|
||||||
@instance = described_class.new(:text => 'foo')
|
@instance = described_class.new(:text => 'foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'pretty-print the node' do
|
example 'return the inspect value' do
|
||||||
@instance.inspect.should == 'Cdata(text: "foo")'
|
@instance.inspect.should == 'Cdata("foo")'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,8 @@ describe Oga::XML::Comment do
|
||||||
@instance = described_class.new(:text => 'foo')
|
@instance = described_class.new(:text => 'foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'pretty-print the node' do
|
example 'return the inspect value' do
|
||||||
@instance.inspect.should == 'Comment(text: "foo")'
|
@instance.inspect.should == 'Comment("foo")'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -143,23 +143,21 @@ describe Oga::XML::Document do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'pretty-print the node' do
|
example 'return the inspect value' do
|
||||||
@instance.inspect.should == <<-EOF.strip
|
@instance.inspect.should == <<-EOF.strip
|
||||||
Document(
|
Document(
|
||||||
doctype: Doctype(name: "html")
|
doctype: Doctype(name: "html")
|
||||||
xml_declaration: XmlDeclaration(version: "1.0" encoding: "UTF-8")
|
xml_declaration: XmlDeclaration(version: "1.0" encoding: "UTF-8")
|
||||||
children: [
|
children: NodeSet(Comment("foo"))
|
||||||
Comment(text: "foo")
|
)
|
||||||
])
|
|
||||||
EOF
|
EOF
|
||||||
end
|
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
|
described_class.new.inspect.should == <<-EOF.strip
|
||||||
Document(
|
Document(
|
||||||
children: [
|
children: NodeSet()
|
||||||
|
)
|
||||||
])
|
|
||||||
EOF
|
EOF
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -142,30 +142,18 @@ describe Oga::XML::Element do
|
||||||
example 'inspect a node with a name' do
|
example 'inspect a node with a name' do
|
||||||
node = described_class.new(:name => 'a')
|
node = described_class.new(:name => 'a')
|
||||||
|
|
||||||
node.inspect.should == <<-EOF.strip
|
node.inspect.should == 'Element(name: "a")'
|
||||||
Element(
|
|
||||||
name: "a"
|
|
||||||
children: [
|
|
||||||
|
|
||||||
])
|
|
||||||
EOF
|
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'inspect a node with attributes and children' do
|
example 'inspect a node with attributes and children' do
|
||||||
node = described_class.new(
|
node = described_class.new(
|
||||||
:name => 'p',
|
:name => 'p',
|
||||||
:children => [Oga::XML::Comment.new(:text => 'foo')],
|
:children => [Oga::XML::Comment.new(:text => 'foo')],
|
||||||
:attributes => {'class' => 'foo'}
|
:attributes => [Oga::XML::Attribute.new(:name => 'x', :value => 'y')]
|
||||||
)
|
)
|
||||||
|
|
||||||
node.inspect.should == <<-EOF.strip
|
node.inspect.should == 'Element(name: "p" attributes: ' \
|
||||||
Element(
|
'[Attribute(name: "x" value: "y")] children: NodeSet(Comment("foo")))'
|
||||||
name: "p"
|
|
||||||
attributes: {"class"=>"foo"}
|
|
||||||
children: [
|
|
||||||
Comment(text: "foo")
|
|
||||||
])
|
|
||||||
EOF
|
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'inspect a node with a namespace' do
|
example 'inspect a node with a namespace' do
|
||||||
|
@ -174,14 +162,7 @@ Element(
|
||||||
:namespace => Oga::XML::Namespace.new(:name => 'x')
|
:namespace => Oga::XML::Namespace.new(:name => 'x')
|
||||||
)
|
)
|
||||||
|
|
||||||
node.inspect.should == <<-EOF.strip
|
node.inspect.should == 'Element(name: "p" namespace: Namespace(name: "x"))'
|
||||||
Element(
|
|
||||||
name: "p"
|
|
||||||
namespace: Namespace(name: "x")
|
|
||||||
children: [
|
|
||||||
|
|
||||||
])
|
|
||||||
EOF
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -29,8 +29,8 @@ describe Oga::XML::Text do
|
||||||
@instance = described_class.new(:text => 'foo')
|
@instance = described_class.new(:text => 'foo')
|
||||||
end
|
end
|
||||||
|
|
||||||
example 'pretty-print the node' do
|
example 'return the inspect value' do
|
||||||
@instance.inspect.should == 'Text(text: "foo")'
|
@instance.inspect.should == 'Text("foo")'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue