Module: Lutaml::Xml::PolymorphicValueHandler

Included in:
Adapter::BaseAdapter
Defined in:
lib/lutaml/xml/polymorphic_value_handler.rb

Overview

Handles polymorphic value detection for XML adapters

This module provides a shared method to determine if a value should be treated as polymorphic during serialization. Polymorphism is detected through two mechanisms:

  1. Explicit declaration: The attribute has ‘polymorphic: true` option

  2. Implicit hierarchy: The value’s class has an attribute with ‘polymorphic_class: true`, indicating it’s part of a polymorphic inheritance tree

This logic is identical across all XML adapters (Nokogiri, Oga, Ox) and has been extracted here to maintain DRY principles.

Instance Method Summary collapse

Instance Method Details

#polymorphic_value?(attribute, value) ⇒ Boolean

Determine if a value should be treated as polymorphic

Parameters:

Returns:

  • (Boolean)

    true if value should use polymorphic serialization



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lutaml/xml/polymorphic_value_handler.rb', line 24

def polymorphic_value?(attribute, value)
  return false unless attribute
  return false unless value.respond_to?(:class)

  # Check if attribute explicitly declares polymorphism
  return true if attribute.options[:polymorphic] || attribute.polymorphic?

  # Check if value's class is part of a polymorphic hierarchy
  # (has an attribute with polymorphic_class: true)
  value_class = value.class
  return false unless value_class.is_a?(Class) &&
    value_class.include?(Lutaml::Model::Serialize)

  value_class.attributes.values.any?(&:polymorphic?)
end