Class: Lutaml::Xml::CustomMethodWrapper::ElementWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/transformation/custom_method_wrapper.rb

Overview

Wrapper for XmlDataModel::XmlElement that adds compatibility methods expected by custom serialization methods

Instance Method Summary collapse

Constructor Details

#initialize(element, parent_wrapper = nil) ⇒ ElementWrapper

Returns a new instance of ElementWrapper.



208
209
210
211
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 208

def initialize(element, parent_wrapper = nil)
  @element = element
  @parent_wrapper = parent_wrapper
end

Instance Method Details

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

Add text content to the element (old adapter API)

Parameters:

  • _self (XmlElement)

    Self parameter (ignored, for compatibility)

  • text (String)

    Text content

  • cdata (Boolean, Hash) (defaults to: false)

    Whether to use CDATA (true or true)



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 218

def add_text(_self, text, cdata: false)
  # Handle both cdata: true and cdata: {cdata: true} formats
  use_cdata = if cdata.is_a?(Hash)
                cdata[:cdata] || false
              else
                cdata
              end

  @element.text_content = text
  @element.cdata = use_cdata
end

#create_and_add_element(name, attributes: {}) {|ElementWrapper| ... } ⇒ XmlElement

Create and add a child element (old adapter API)

Parameters:

  • name (String)

    Element name

  • attributes (Hash) (defaults to: {})

    Optional attributes to add to the element

Yields:

Returns:



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 236

def create_and_add_element(name, attributes: {})
  # Create XmlDataModel element
  child = Lutaml::Xml::DataModel::XmlElement.new(name)

  # Add attributes if provided
  attributes&.each do |attr_name, attr_value|
    attr = Lutaml::Xml::DataModel::XmlAttribute.new(
      attr_name.to_s, attr_value.to_s
    )
    child.add_attribute(attr)
  end

  # Add to this element
  @element.add_child(child)

  if block_given?
    # Wrap the child and yield
    wrapped_child = ElementWrapper.new(child, @parent_wrapper)
    yield wrapped_child
  end

  child
end