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.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/boxcars/x_node.rb', line 7
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
72
73
74
75
76
|
# File 'lib/boxcars/x_node.rb', line 72
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.
5
6
7
|
# File 'lib/boxcars/x_node.rb', line 5
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
5
6
7
|
# File 'lib/boxcars/x_node.rb', line 5
def children
@children
end
|
#node ⇒ Object
Returns the value of attribute node.
5
6
7
|
# File 'lib/boxcars/x_node.rb', line 5
def node
@node
end
|
Class Method Details
.from_xml(xml) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/boxcars/x_node.rb', line 28
def self.from_xml(xml)
Boxcars::OptionalDependency.require!("nokogiri", feature: "XML trains and XML parsing helpers")
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
68
69
70
|
# File 'lib/boxcars/x_node.rb', line 68
def [](key)
@children[key.to_s]
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
78
79
80
|
# File 'lib/boxcars/x_node.rb', line 78
def respond_to_missing?(method_name, include_private = false)
@valid_names.include?(method_name.to_sym) || super
end
|
#stext ⇒ Object
64
65
66
|
# File 'lib/boxcars/x_node.rb', line 64
def stext
@stext ||= text.gsub(/[[:space:]]+/, " ").strip end
|
#text ⇒ Object
47
48
49
|
# File 'lib/boxcars/x_node.rb', line 47
def text
@node.text
end
|
#xml ⇒ Object
43
44
45
|
# File 'lib/boxcars/x_node.rb', line 43
def xml
@node.to_xml
end
|
#xpath(path) ⇒ Object
51
52
53
|
# File 'lib/boxcars/x_node.rb', line 51
def xpath(path)
@node.xpath(path)
end
|
#xtext(path) ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/boxcars/x_node.rb', line 55
def xtext(path)
rv = xpath(path)&.text&.gsub(/[[:space:]]+/, " ")&.strip
return nil if rv.empty?
rv
end
|