From b1388ff84a6a556851ac9280d3c6ba4a031fb700 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Thu, 7 Aug 2014 21:09:10 +0200 Subject: [PATCH] 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. --- lib/oga/xml/document.rb | 13 +++++++------ lib/oga/xml/element.rb | 19 +++---------------- lib/oga/xml/node.rb | 20 -------------------- lib/oga/xml/node_set.rb | 9 +++++++++ lib/oga/xml/text.rb | 5 ++--- spec/oga/xml/cdata_spec.rb | 4 ++-- spec/oga/xml/comment_spec.rb | 4 ++-- spec/oga/xml/document_spec.rb | 14 ++++++-------- spec/oga/xml/element_spec.rb | 29 +++++------------------------ spec/oga/xml/text_spec.rb | 4 ++-- 10 files changed, 38 insertions(+), 83 deletions(-) diff --git a/lib/oga/xml/document.rb b/lib/oga/xml/document.rb index ea1b939..ae82630 100644 --- a/lib/oga/xml/document.rb +++ b/lib/oga/xml/document.rb @@ -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 diff --git a/lib/oga/xml/element.rb b/lib/oga/xml/element.rb index 78c80f3..bb44677 100644 --- a/lib/oga/xml/element.rb +++ b/lib/oga/xml/element.rb @@ -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 ## diff --git a/lib/oga/xml/node.rb b/lib/oga/xml/node.rb index dbc7deb..0cabf6d 100644 --- a/lib/oga/xml/node.rb +++ b/lib/oga/xml/node.rb @@ -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] # diff --git a/lib/oga/xml/node_set.rb b/lib/oga/xml/node_set.rb index 1603022..b9a3b54 100644 --- a/lib/oga/xml/node_set.rb +++ b/lib/oga/xml/node_set.rb @@ -252,6 +252,15 @@ module Oga return text end + ## + # @return [String] + # + def inspect + values = @nodes.map(&:inspect).join(', ') + + return "NodeSet(#{values})" + end + private ## diff --git a/lib/oga/xml/text.rb b/lib/oga/xml/text.rb index b90a185..94a3607 100644 --- a/lib/oga/xml/text.rb +++ b/lib/oga/xml/text.rb @@ -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 ## diff --git a/spec/oga/xml/cdata_spec.rb b/spec/oga/xml/cdata_spec.rb index faa5a49..916b2b4 100644 --- a/spec/oga/xml/cdata_spec.rb +++ b/spec/oga/xml/cdata_spec.rb @@ -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 diff --git a/spec/oga/xml/comment_spec.rb b/spec/oga/xml/comment_spec.rb index 5c015cf..9a07513 100644 --- a/spec/oga/xml/comment_spec.rb +++ b/spec/oga/xml/comment_spec.rb @@ -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 diff --git a/spec/oga/xml/document_spec.rb b/spec/oga/xml/document_spec.rb index 6d54929..5c68e5c 100644 --- a/spec/oga/xml/document_spec.rb +++ b/spec/oga/xml/document_spec.rb @@ -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 diff --git a/spec/oga/xml/element_spec.rb b/spec/oga/xml/element_spec.rb index b117152..7e7ddf6 100644 --- a/spec/oga/xml/element_spec.rb +++ b/spec/oga/xml/element_spec.rb @@ -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 diff --git a/spec/oga/xml/text_spec.rb b/spec/oga/xml/text_spec.rb index 41dee84..d8e1671 100644 --- a/spec/oga/xml/text_spec.rb +++ b/spec/oga/xml/text_spec.rb @@ -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