Class: Lutaml::Xml::Schema::Builder::Oga::OgaBuilderWrapper

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

Overview

Wrapper that provides a Nokogiri-like API for Oga

Instance Method Summary collapse

Constructor Details

#initialize(document, encoding) ⇒ OgaBuilderWrapper

Returns a new instance of OgaBuilderWrapper.



44
45
46
47
48
# File 'lib/lutaml/xml/schema/builder/oga.rb', line 44

def initialize(document, encoding)
  @document = document
  @encoding = encoding
  @current_element = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Create a method that acts like Nokogiri’s builder DSL e.g., xml.schema { } or xml.element { }



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/lutaml/xml/schema/builder/oga.rb', line 52

def method_missing(method_name, *args)
  attributes = args.first || {}

  # Create the element
  element = ::Oga::XML::Element.new(name: method_name.to_s)

  # Add attributes
  attributes.each do |name, value|
    element.attributes << ::Oga::XML::Attribute.new(
      name: name.to_s,
      value: value.to_s,
    )
  end

  # Add to document or current element
  if @current_element
    @current_element.children << element
  else
    @document.children << element
  end

  # If block given, process nested elements
  if block_given?
    parent = @current_element
    @current_element = element
    yield
    @current_element = parent
  end

  element
end

Instance Method Details

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/lutaml/xml/schema/builder/oga.rb', line 84

def respond_to_missing?(_method_name, _include_private = false)
  true # Accept any method for element creation
end