Class: Scrubyt::ResultNode
- Defined in:
- lib/scrubyt/output/result_node.rb
Direct Known Subclasses
Constant Summary collapse
- OUTPUT_OPTIONS =
[:write_text]
Instance Attribute Summary collapse
-
#generated_by_leaf ⇒ Object
Returns the value of attribute generated_by_leaf.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#result ⇒ Object
Returns the value of attribute result.
Instance Method Summary collapse
- #has_content? ⇒ Boolean
-
#initialize(name, result = nil, options = {}) ⇒ ResultNode
constructor
A new instance of ResultNode.
- #to_hash ⇒ Object
- #to_libxml ⇒ Object
- #to_s ⇒ Object
-
#to_xml ⇒ Object
note: see ruby_extensions.rb for String#write.
- #to_xml_lines ⇒ Object
- #write_text ⇒ Object
Methods inherited from Array
Constructor Details
#initialize(name, result = nil, options = {}) ⇒ ResultNode
Returns a new instance of ResultNode.
7 8 9 10 11 |
# File 'lib/scrubyt/output/result_node.rb', line 7 def initialize(name, result=nil, ={}) @name = name @result = result @options = end |
Instance Attribute Details
#generated_by_leaf ⇒ Object
Returns the value of attribute generated_by_leaf.
5 6 7 |
# File 'lib/scrubyt/output/result_node.rb', line 5 def generated_by_leaf @generated_by_leaf end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/scrubyt/output/result_node.rb', line 5 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/scrubyt/output/result_node.rb', line 5 def @options end |
#result ⇒ Object
Returns the value of attribute result.
5 6 7 |
# File 'lib/scrubyt/output/result_node.rb', line 5 def result @result end |
Instance Method Details
#has_content? ⇒ Boolean
17 18 19 20 |
# File 'lib/scrubyt/output/result_node.rb', line 17 def has_content? return true if result.is_a? String write_text || (inject(false) { |one_child_has_content, child| one_child_has_content || child.has_content? }) end |
#to_hash ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/scrubyt/output/result_node.rb', line 41 def to_hash result = [] flat_hash_inner = lambda {|e, hash| hash[e.name.to_sym] = hash[e.name.to_sym] ? hash[e.name.to_sym] + "," + e.to_s : e.to_s if e.write_text && !e.to_s.empty? e.each {|c| flat_hash_inner.call(c, hash) } hash } self.each {|e| result << flat_hash_inner.call(e, {}) } result end |
#to_libxml ⇒ Object
29 30 31 32 33 34 |
# File 'lib/scrubyt/output/result_node.rb', line 29 def to_libxml libxml_node = XML::Node.new(name) self.each { |child| libxml_node << child.to_libxml if child.has_content? } libxml_node << to_s if write_text libxml_node end |
#to_s ⇒ Object
22 23 24 25 26 27 |
# File 'lib/scrubyt/output/result_node.rb', line 22 def to_s text = (@result.is_a? String) ? @result : @result.inner_text text = SharedUtils.unescape_entities(text) text.strip! text end |
#to_xml ⇒ Object
note: see ruby_extensions.rb for String#write
37 38 39 |
# File 'lib/scrubyt/output/result_node.rb', line 37 def to_xml to_xml_lines.join("\n") end |
#to_xml_lines ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/scrubyt/output/result_node.rb', line 52 def to_xml_lines lines = [] children = self.select{ |child| child.has_content? } if children.empty? if result.is_a? String lines << "<#{name}>#{result}</#{name}>" elsif write_text && !to_s.empty? lines << "<#{name}>#{ERB::Util.html_escape(to_s)}</#{name}>" else lines << "<#{name}/>" end else lines << "<#{name}>" lines << " #{ERB::Util.html_escape(to_s)}" if write_text && !to_s.empty? children.each do |child| lines.push(*child.to_xml_lines.map{ |line| " #{line}" }) end lines << "</#{name}>" end end |
#write_text ⇒ Object
13 14 15 |
# File 'lib/scrubyt/output/result_node.rb', line 13 def write_text @options[:write_text].nil? ? @generated_by_leaf : @options[:write_text] end |