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

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_flat_hashObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/scrubyt/output/result_node.rb', line 56

def to_flat_hash()
  hash_result = self.to_hash('@@@@@@')
  merged_hash = hash_result.delete_at 0
  hash_result.each do |hash|
    merged_hash.keys.each do |key|
      merged_hash[key] += "@@@@@@#{hash[key]}"
    end
  end
  result_sets = merged_hash.values.map!{|x| x.split('@@@@@@')}.transpose
  final_result = []

  result_sets.each do |rs|
    temp_result = {}
    merged_hash.keys.each do |k|
      temp_result[k] = rs[merged_hash.keys.index(k)]
    end
    final_result << temp_result
  end
  final_result
end

#to_flat_xml(delimiter = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/scrubyt/output/result_node.rb', line 77

def to_flat_xml(delimiter=nil)
  lines = []
  hash_result = delimiter ? self.to_hash(delimiter) : self.to_hash
  merged_hash = hash_result.delete_at 0

  hash_result.each do |hash|
    merged_hash.keys.each do |key|
      merged_hash[key] += "#{delimiter}#{hash[key]}"
    end
  end

  if delimiter
    result_sets = merged_hash.values.map!{|x| x.split(delimiter)}.transpose
    final_result = []

    result_sets.each do |rs|
      temp_result = {}
      merged_hash.keys.each do |k|
        temp_result[k] = rs[merged_hash.keys.index(k)]
      end
      final_result << temp_result
    end
    hash_result = final_result
  end

  hash_result.each do |hash|
    lines << "<item>"
    hash.each do |key, value|
      xml_tag = key.to_s
      value = '' if value == '#empty#'
      lines << "  <#{xml_tag}>#{REXML::Text.normalize(value)}</#{xml_tag}>"
    end
    lines << "</item>"
  end
  return lines.join("\n")

end

#to_hash(delimiter = ',') ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/scrubyt/output/result_node.rb', line 45

def to_hash(delimiter=',')
  result = []
  flat_hash_inner = lambda {|e, hash|
    hash[e.name.to_sym] = hash[e.name.to_sym] ? hash[e.name.to_sym] + delimiter + e.to_s : e.to_s  if ((e.write_text && !e.to_s.empty?) || e.options[:default])
    e.each {|c| flat_hash_inner.call(c, hash)  }
    hash
  }
  self.each {|e| result << flat_hash_inner.call(e, {}) }
  result
end

#to_libxmlObject



33
34
35
36
37
38
# File 'lib/scrubyt/output/result_node.rb', line 33

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
28
29
30
31
# File 'lib/scrubyt/output/result_node.rb', line 22

def to_s
  text = (@result.is_a? String) ? @result : @result.inner_html.gsub(/<.*?>/, '')
  text = SharedUtils.unescape_entities(text)
  text.strip!
  if (@options[:default] && ((text == '') || (text == @options[:default])))
    @options[:default]
  else
    text
  end
end

#to_xmlObject

note: see ruby_extensions.rb for String#write



41
42
43
# File 'lib/scrubyt/output/result_node.rb', line 41

def to_xml
  to_xml_lines.join("\n")
end

#to_xml_linesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/scrubyt/output/result_node.rb', line 115

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
      if @options[:default]
        lines << "<#{name}>#{@options[:default]}</#{name}>"
      else
        lines << "<#{name}/>"
      end
    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