Class: Boxcars::XNode
- Inherits:
-
Object
show all
- Defined in:
- lib/boxcars/x_node.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(node) ⇒ XNode
Returns a new instance of XNode.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/boxcars/x_node.rb', line 9
def initialize(node)
@node = node
@valid_names = []
@children = {}
@attributes = node.attributes.values.to_h { |a| [a.name.to_sym, a.value] }
node.children.each do |child|
next if child.text?
child_node = XNode.new(child)
if @children[child.name].nil?
@valid_names << child.name.to_sym
@children[child.name] = child_node
elsif @children[child.name].is_a?(Array)
@children[child.name] << child_node
else
@children[child.name] = [@children[child.name], child_node]
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
71
72
73
74
75
|
# File 'lib/boxcars/x_node.rb', line 71
def method_missing(name, *args)
return @children[name.to_s] if @children.key?(name.to_s)
super
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
7
8
9
|
# File 'lib/boxcars/x_node.rb', line 7
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
7
8
9
|
# File 'lib/boxcars/x_node.rb', line 7
def children
@children
end
|
#node ⇒ Object
Returns the value of attribute node.
7
8
9
|
# File 'lib/boxcars/x_node.rb', line 7
def node
@node
end
|
Class Method Details
.from_xml(xml) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/boxcars/x_node.rb', line 30
def self.from_xml(xml)
xml = xml[xml.index("<")..-1] unless xml.start_with?("<")
xml = xml[0..xml.rindex(">")] unless xml.end_with?(">")
doc = Nokogiri::XML.parse(xml)
if doc.errors.any?
Boxcars.debug("XML: #{xml}", :yellow)
debugger if ENV.fetch("DEBUG_XML", false)
raise XmlError, "XML is not valid: #{doc.errors.map { |e| "#{e.line}:#{e.column} #{e.message}" }}"
end
XNode.new(doc.root)
end
|
Instance Method Details
#[](key) ⇒ Object
67
68
69
|
# File 'lib/boxcars/x_node.rb', line 67
def [](key)
@children[key.to_s]
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
77
78
79
|
# File 'lib/boxcars/x_node.rb', line 77
def respond_to_missing?(method_name, include_private = false)
@valid_names.include?(method) || super
end
|
#stext ⇒ Object
63
64
65
|
# File 'lib/boxcars/x_node.rb', line 63
def stext
@stext ||= text.gsub(/[[:space:]]+/, " ").strip end
|
#text ⇒ Object
48
49
50
|
# File 'lib/boxcars/x_node.rb', line 48
def text
@node.text
end
|
#xml ⇒ Object
44
45
46
|
# File 'lib/boxcars/x_node.rb', line 44
def xml
@node.to_xml
end
|
#xpath(path) ⇒ Object
52
53
54
|
# File 'lib/boxcars/x_node.rb', line 52
def xpath(path)
@node.xpath(path)
end
|
#xtext(path) ⇒ Object
56
57
58
59
60
61
|
# File 'lib/boxcars/x_node.rb', line 56
def xtext(path)
rv = xpath(path)&.text&.gsub(/[[:space:]]+/, " ")&.strip
return nil if rv.empty?
rv
end
|