Module: Lutaml::Xml::HoistingAlgorithm

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

Overview

HoistingAlgorithm defines strategies for namespace declaration placement.

When serializing XML, we must decide WHERE to declare each namespace. This module provides swappable algorithms for different hoisting strategies.

Key concepts:

  • namespace_scope makes namespaces ELIGIBLE for hoisting to any parent

  • The algorithm determines WHERE eligible namespaces are actually declared

  • Round-trip preservation takes priority (use stored plan if available)

Algorithm selection priority:

  1. PRESERVED - if stored plan exists (round-trip fidelity)

  2. User option - to_xml(hoisting: :lca)

  3. Class config - xml { hoisting_algorithm :first_usage }

  4. Global default - Config.default_hoisting_algorithm

Defined Under Namespace

Classes: Base, FirstUsage, LCA, NamespaceScopeOnly, Preserved

Constant Summary collapse

ALGORITHMS =

Registry of available algorithms

{
  lca: LCA,
  first_usage: FirstUsage,
  namespace_scope_only: NamespaceScopeOnly,
  preserved: Preserved,
}.freeze

Class Method Summary collapse

Class Method Details

.defaultObject

Default algorithm



229
230
231
# File 'lib/lutaml/xml/hoisting_algorithm.rb', line 229

def self.default
  LCA.new
end

.get(name, **options) ⇒ Base

Get algorithm instance by name

Parameters:

  • name (Symbol)

    Algorithm name (:lca, :first_usage, etc.)

  • options (Hash)

    Options to pass to algorithm constructor

Returns:

  • (Base)

    Algorithm instance



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/lutaml/xml/hoisting_algorithm.rb', line 213

def self.get(name, **options)
  klass = ALGORITHMS[name]
  unless klass
    raise ArgumentError,
          "Unknown hoisting algorithm: #{name}"
  end

  if klass == Preserved && options[:fallback]
    fallback = get(options[:fallback])
    klass.new(fallback: fallback)
  else
    klass.new
  end
end