Module: Lutaml::Xml::Configurable::ClassMethods

Defined in:
lib/lutaml/xml/configurable.rb

Overview

Class methods for XML configuration

Instance Method Summary collapse

Instance Method Details

#create_xml_mappingObject

Create a new XML mapping instance

Override in including class to provide specific mapping type. Model classes should return Lutaml::Xml::Mapping.new Type classes should return Lutaml::Model::Type::ValueXmlMapping.new

Returns:

  • (Object)

    a new mapping instance

Raises:

  • (NotImplementedError)

    if not overridden



104
105
106
107
# File 'lib/lutaml/xml/configurable.rb', line 104

def create_xml_mapping
  raise NotImplementedError,
        "#{self.class} must implement #create_xml_mapping"
end

#xml { ... } ⇒ Object #xml(mapping_class) { ... } ⇒ Object

XML configuration block

Provides unified XML configuration API. The behavior differs between Model and Type classes:

  • Model classes use Lutaml::Xml::Mapping

  • Type classes use Lutaml::Model::Type::ValueXmlMapping

Overloads:

  • #xml { ... } ⇒ Object

    Configure XML mapping inline with a block.

    Examples:

    Configuration with block

    xml do
      root 'MyModel'
      namespace MyNamespace
      map_element 'name', to: :name
    end

    Yields:

    • block for XML configuration

    Returns:

    • (Object)

      the XML mapping object

  • #xml(mapping_class) { ... } ⇒ Object

    Inherit from a reusable mapping class and optionally add more config.

    Examples:

    Reference a mapping class

    xml MyMapping

    Reference a mapping class with additional config

    xml MyMapping do
      map_element 'Extra', to: :extra
    end

    Parameters:

    • mapping_class (Class)

      A Lutaml::Xml::Mapping subclass

    Yields:

    • optional block for additional configuration

    Returns:

    • (Object)

      the XML mapping object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lutaml/xml/configurable.rb', line 73

def xml(mapping_class = nil, &block)
  @xml_mapping ||= create_xml_mapping

  if mapping_class
    if mapping_class < Lutaml::Xml::Mapping
      inherit_mapping_from(mapping_class)
    elsif mapping_class.is_a?(Class)
      raise ArgumentError,
            "#{mapping_class} must be a subclass of Lutaml::Xml::Mapping"
    end
  end

  @xml_mapping.instance_eval(&block) if block
  @xml_mapping
end

#xml_mappingObject?

Get the XML mapping for this class

Returns:

  • (Object, nil)

    the XML mapping object



92
93
94
# File 'lib/lutaml/xml/configurable.rb', line 92

def xml_mapping
  @xml_mapping
end