Class: Scrubyt::ResultNode

Inherits:
Array
  • Object
show all
Defined in:
lib/scrubyt/output/result_node.rb

Direct Known Subclasses

ScrubytResult

Constant Summary collapse

OUTPUT_OPTIONS =
[:write_text]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#to_sexp, #to_sexp_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, options={})
  @name = name
  @result = result
  @options = options
end

Instance Attribute Details

#generated_by_leafObject

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

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/scrubyt/output/result_node.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/scrubyt/output/result_node.rb', line 5

def options
  @options
end

#resultObject

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

Returns:

  • (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_hashObject



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_libxmlObject



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_sObject



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_xmlObject

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_linesObject



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_textObject



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