Class: Lutaml::Model::XmlAdapter::Builder::Nokogiri
- Inherits:
-
Object
- Object
- Lutaml::Model::XmlAdapter::Builder::Nokogiri
show all
- Defined in:
- lib/lutaml/model/xml_adapter/builder/nokogiri.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#add_attribute(element, name, value) ⇒ Object
-
#add_cdata(element, value) ⇒ Object
-
#add_element(element, child) ⇒ Object
-
#add_namespace_prefix(prefix) ⇒ Object
-
#add_text(element, text, cdata: false) ⇒ Object
-
#add_xml_fragment(element, content) ⇒ Object
-
#create_and_add_element(element_name, prefix: (prefix_unset = true nil), attributes: {}) ⇒ Object
-
#create_element(name, attributes = {}) ⇒ Object
-
#initialize(xml) ⇒ Nokogiri
constructor
A new instance of Nokogiri.
-
#method_missing(method_name, *args, &block) ⇒ Object
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(xml) ⇒ Nokogiri
Returns a new instance of Nokogiri.
18
19
20
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 18
def initialize(xml)
@xml = xml
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 86
def method_missing(method_name, *args, &block)
if block_given?
xml.public_send(method_name, *args, &block)
else
xml.public_send(method_name, *args)
end
end
|
Instance Attribute Details
#xml ⇒ Object
Returns the value of attribute xml.
16
17
18
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 16
def xml
@xml
end
|
Class Method Details
.build(options = {}) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 6
def self.build(options = {})
if block_given?
::Nokogiri::XML::Builder.new(options) do |xml|
yield(new(xml))
end
else
new(::Nokogiri::XML::Builder.new(options))
end
end
|
Instance Method Details
#add_attribute(element, name, value) ⇒ Object
30
31
32
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 30
def add_attribute(element, name, value)
element[name] = value
end
|
#add_cdata(element, value) ⇒ Object
76
77
78
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 76
def add_cdata(element, value)
element.cdata(value)
end
|
#add_element(element, child) ⇒ Object
26
27
28
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 26
def add_element(element, child)
element.add_child(child)
end
|
#add_namespace_prefix(prefix) ⇒ Object
80
81
82
83
84
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 80
def add_namespace_prefix(prefix)
xml[prefix] if prefix
self
end
|
#add_text(element, text, cdata: false) ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 65
def add_text(element, text, cdata: false)
return add_cdata(element, text) if cdata
if element.is_a?(self.class)
element = element.xml.parent
end
text_node = ::Nokogiri::XML::Text.new(text.to_s, element)
element.add_child(text_node)
end
|
#add_xml_fragment(element, content) ⇒ Object
55
56
57
58
59
60
61
62
63
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 55
def add_xml_fragment(element, content)
if element.is_a?(self.class)
element = element.xml.parent
end
fragment = ::Nokogiri::XML::DocumentFragment.parse(content)
element.add_child(fragment)
end
|
#create_and_add_element(element_name, prefix: (prefix_unset = true
nil), attributes: {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 34
def create_and_add_element(
element_name,
prefix: (prefix_unset = true
nil),
attributes: {}
)
add_namespace_prefix(prefix)
element_name = element_name.first if element_name.is_a?(Array)
element_name = "#{element_name}_" if respond_to?(element_name)
if block_given?
public_send(element_name, attributes) do
xml.parent.namespace = nil if prefix.nil? && !prefix_unset
yield(self)
end
else
public_send(element_name, attributes)
end
end
|
#create_element(name, attributes = {}) ⇒ Object
22
23
24
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 22
def create_element(name, attributes = {})
xml.doc.create_element(name, attributes)
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
94
95
96
|
# File 'lib/lutaml/model/xml_adapter/builder/nokogiri.rb', line 94
def respond_to_missing?(method_name, include_private = false)
xml.respond_to?(method_name) || super
end
|