Class: Lutaml::Xml::CustomMethodWrapper
- Inherits:
-
Object
- Object
- Lutaml::Xml::CustomMethodWrapper
- Defined in:
- lib/lutaml/xml/transformation/custom_method_wrapper.rb
Overview
Wrapper for custom method compatibility with XmlDataModel.
Custom methods use the old adapter API (doc.create_and_add_element), but the new transformation works with XmlDataModel. This wrapper bridges the gap by implementing the old adapter interface while working with XmlDataModel elements under the hood.
The wrapper tracks a "current context" element. When create_and_add_element is called with a block, the new element becomes the context for the duration of the block, allowing nested element creation.
Defined Under Namespace
Classes: ElementWrapper
Class Method Summary collapse
-
.build_element(name, attributes) ⇒ DataModel::XmlElement
Shared factory: create an XmlElement with optional attributes.
Instance Method Summary collapse
-
#add_attribute(element, name, value) ⇒ Object
Add attribute to element (mimics old adapter API).
-
#add_element(parent_or_element, element_or_string = nil) ⇒ XmlDataModel::XmlElement
Add an element to the current context (mimics old adapter API).
-
#add_text(element, text) ⇒ Object
Add text to element (mimics old adapter API).
-
#create_and_add_element(name, attributes: {}) {|ElementWrapper| ... } ⇒ XmlElement
Create and add an element (mimics old adapter API).
-
#create_element(name) ⇒ XmlDataModel::XmlElement
Create an element (mimics old adapter API).
-
#current_context ⇒ XmlDataModel::XmlElement
Get the current context element (top of stack).
-
#initialize(parent) ⇒ CustomMethodWrapper
constructor
A new instance of CustomMethodWrapper.
-
#pop_context ⇒ XmlDataModel::XmlElement
Pop the current context from the stack.
-
#push_context(element) ⇒ Object
Push a new context onto the stack.
Constructor Details
#initialize(parent) ⇒ CustomMethodWrapper
Returns a new instance of CustomMethodWrapper.
17 18 19 20 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 17 def initialize(parent) @parent = parent @context_stack = [parent] end |
Class Method Details
.build_element(name, attributes) ⇒ DataModel::XmlElement
Shared factory: create an XmlElement with optional attributes. Public so ElementWrapper can call it without an instance.
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 237 def self.build_element(name, attributes) element = Lutaml::Xml::DataModel::XmlElement.new(name) attributes&.each do |attr_name, attr_value| attr = Lutaml::Xml::DataModel::XmlAttribute.new( attr_name.to_s, attr_value.to_s ) element.add_attribute(attr) end element end |
Instance Method Details
#add_attribute(element, name, value) ⇒ Object
Add attribute to element (mimics old adapter API)
163 164 165 166 167 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 163 def add_attribute(element, name, value) attr = Lutaml::Xml::DataModel::XmlAttribute.new(name.to_s, value.to_s) element.add_attribute(attr) end |
#add_element(parent_or_element, element_or_string = nil) ⇒ XmlDataModel::XmlElement
Add an element to the current context (mimics old adapter API)
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 56 def add_element(parent_or_element, element_or_string = nil) # Handle overloaded API: add_element(element) or add_element(parent, element) if element_or_string.nil? # Single argument: parent_or_element is the element to add to current context element = parent_or_element current_context.add_child(element) return element end parent = parent_or_element if element_or_string.is_a?(String) add_xml_fragment_or_raw_content(parent, element_or_string) elsif element_or_string.is_a?(::Lutaml::Xml::DataModel::XmlElement) parent.add_child(element_or_string) else raise TypeError, "add_element expects a String or XmlElement, got " \ "#{element_or_string.class}. Call .to_xml on the element first." end element_or_string end |
#add_text(element, text) ⇒ Object
Add text to element (mimics old adapter API)
149 150 151 152 153 154 155 156 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 149 def add_text(element, text) target = if element == self || element.nil? current_context else element end target.text_content = text end |
#create_and_add_element(name, attributes: {}) {|ElementWrapper| ... } ⇒ XmlElement
Create and add an element (mimics old adapter API)
When called with a block, the new element becomes the context for nested operations inside the block.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 178 def create_and_add_element(name, attributes: {}) element = self.class.build_element(name, attributes) current_context.add_child(element) if block_given? push_context(element) begin yield ElementWrapper.new(element, self) ensure pop_context end end element end |
#create_element(name) ⇒ XmlDataModel::XmlElement
Create an element (mimics old adapter API)
47 48 49 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 47 def create_element(name) Lutaml::Xml::DataModel::XmlElement.new(name) end |
#current_context ⇒ XmlDataModel::XmlElement
Get the current context element (top of stack)
25 26 27 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 25 def current_context @context_stack.last end |
#pop_context ⇒ XmlDataModel::XmlElement
Pop the current context from the stack
39 40 41 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 39 def pop_context @context_stack.pop if @context_stack.size > 1 end |
#push_context(element) ⇒ Object
Push a new context onto the stack
32 33 34 |
# File 'lib/lutaml/xml/transformation/custom_method_wrapper.rb', line 32 def push_context(element) @context_stack.push(element) end |