Class: Lutaml::Model::Xml::Builder::Oga

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/xml/builder/oga.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Oga

Returns a new instance of Oga.

Yields:

  • (_self)

Yield Parameters:



14
15
16
17
18
19
# File 'lib/lutaml/model/xml/builder/oga.rb', line 14

def initialize(options = {})
  @document = Xml::Oga::Document.new
  @current_node = @document
  @encoding = options[:encoding]
  yield(self) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



152
153
154
155
156
157
158
159
160
# File 'lib/lutaml/model/xml/builder/oga.rb', line 152

def method_missing(method_name, *args)
  if block_given?
    @current_node.public_send(method_name, *args) do
      yield(self)
    end
  else
    @current_node.public_send(method_name, *args)
  end
end

Instance Attribute Details

#current_nodeObject (readonly)

Returns the value of attribute current_node.



12
13
14
# File 'lib/lutaml/model/xml/builder/oga.rb', line 12

def current_node
  @current_node
end

#documentObject (readonly)

Returns the value of attribute document.



12
13
14
# File 'lib/lutaml/model/xml/builder/oga.rb', line 12

def document
  @document
end

#encodingObject (readonly)

Returns the value of attribute encoding.



12
13
14
# File 'lib/lutaml/model/xml/builder/oga.rb', line 12

def encoding
  @encoding
end

Class Method Details

.build(options = {}, &block) ⇒ Object



8
9
10
# File 'lib/lutaml/model/xml/builder/oga.rb', line 8

def self.build(options = {}, &block)
  new(options, &block)
end

Instance Method Details

#<<(text) ⇒ Object



95
96
97
# File 'lib/lutaml/model/xml/builder/oga.rb', line 95

def <<(text)
  @current_node.text(text)
end

#add_attribute(element, name, value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lutaml/model/xml/builder/oga.rb', line 60

def add_attribute(element, name, value)
  attribute = ::Oga::XML::Attribute.new(
    name: name,
    value: value.to_s,
  )
  if element.is_a?(Xml::Oga::Document)
    element.children.last.attributes << attribute
  else
    element.attributes << attribute
  end
end

#add_cdata(element, value) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/lutaml/model/xml/builder/oga.rb', line 127

def add_cdata(element, value)
  oga_cdata = ::Oga::XML::CData.new(text: value.to_s)
  if element.is_a?(Xml::Oga::Document)
    element.children.last.children << oga_cdata
  else
    element.children << oga_cdata
  end
end

#add_element(oga_element, child) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/lutaml/model/xml/builder/oga.rb', line 49

def add_element(oga_element, child)
  if child.is_a?(String)
    current_element = oga_element.is_a?(Xml::Oga::Document) ? current_node : oga_element
    add_xml_fragment(current_element, child)
  elsif oga_element.is_a?(Xml::Oga::Document)
    oga_element.children.last.children << child
  else
    oga_element.children << child
  end
end

#add_namespace_prefix(prefix) ⇒ Object



136
137
138
139
# File 'lib/lutaml/model/xml/builder/oga.rb', line 136

def add_namespace_prefix(prefix)
  @current_namespace = prefix
  self
end

#add_text(element, text, cdata: false) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/lutaml/model/xml/builder/oga.rb', line 110

def add_text(element, text, cdata: false)
  text = text&.encode(encoding) if encoding && text.is_a?(String)
  return add_cdata(element, text) if cdata

  oga_text = ::Oga::XML::Text.new(text: text.to_s)
  append_text_node(element, oga_text)
end

#add_xml_fragment(element, content) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/lutaml/model/xml/builder/oga.rb', line 99

def add_xml_fragment(element, content)
  fragment = "<fragment>#{content}</fragment>"
  parsed_fragment = ::Oga.parse_xml(fragment)
  parsed_children = parsed_fragment.children.first.children
  if element.is_a?(Xml::Oga::Document)
    element.children.last.children += parsed_children
  else
    element.children += parsed_children
  end
end

#append_text_node(element, oga_text) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/lutaml/model/xml/builder/oga.rb', line 118

def append_text_node(element, oga_text)
  if element.is_a?(Xml::Oga::Document)
    children = element.children
    children.empty? ? children << oga_text : children.last.children << oga_text
  else
    element.children << oga_text
  end
end

#create_and_add_element(element_name, prefix: (prefix_unset = true nil), attributes: {}, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lutaml/model/xml/builder/oga.rb', line 72

def create_and_add_element(
  element_name,
  prefix: (prefix_unset = true
           nil),
  attributes: {},
  &block
)
  @current_namespace = nil if prefix.nil? && !prefix_unset
  prefixed_name = if prefix
                    "#{prefix}:#{element_name}"
                  elsif @current_namespace && !element_name.start_with?("#{@current_namespace}:")
                    "#{@current_namespace}:#{element_name}"
                  else
                    element_name
                  end

  if block
    element(prefixed_name, attributes, &block)
  else
    element(prefixed_name, attributes)
  end
end

#create_element(name, attributes = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/lutaml/model/xml/builder/oga.rb', line 21

def create_element(name, attributes = {}, &block)
  if @current_namespace && !name.start_with?("#{@current_namespace}:")
    name = "#{@current_namespace}:#{name}"
  end

  if block
    element(name, attributes, &block)
  else
    element(name, attributes)
  end
end

#element(name, attributes = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lutaml/model/xml/builder/oga.rb', line 33

def element(name, attributes = {})
  oga_element = ::Oga::XML::Element.new(name: name)
  if block_given?
    element_attributes(oga_element, attributes)
    @current_node.children << oga_element
    # Save previous node to reset the pointer for the rest of the iteration
    previous_node = @current_node
    # Set current node to new element as pointer for the block
    @current_node = oga_element
    yield(self)
    # Reset the pointer for the rest of the iterations
    @current_node = previous_node
  end
  oga_element
end

#parentObject



141
142
143
# File 'lib/lutaml/model/xml/builder/oga.rb', line 141

def parent
  @document
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/lutaml/model/xml/builder/oga.rb', line 162

def respond_to_missing?(method_name, include_private = false)
  @current_node.respond_to?(method_name) || super
end

#text(value = nil) ⇒ Object



145
146
147
148
149
150
# File 'lib/lutaml/model/xml/builder/oga.rb', line 145

def text(value = nil)
  return @current_node.inner_text if value.nil?

  str = value.is_a?(Array) ? value.join : value
  @current_node.children << ::Oga::XML::Text.new(text: str)
end