Module: Lutaml::Xml::PlanSerializer

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

Overview

Serialize-side mirror of the plan fast path: builds the output tree directly through leptris's fused create_child chain — no XmlElement tree, no DeclarationPlanner pass, no builder context stack, no moxml wrapper minting. Serialized shapes must be the plain ones (attributes, scalars, collections, nested models, raw fragments, content runs); custom-method, polymorphic, union, and multi-spelling rows keep the interpretive serializer (their output flows through hash-shaped custom APIs).

Ordered/mixed models serialize from element_order when the instance carries one (parsed models): text runs, comments, and element entries in document order, mirroring the interpretive order applier — including its content-attr-first text reads (mutations to the model show up) and CDATA flattening. Models without element_order fall back interpretively (return nil).

Constant Summary collapse

SERIALIZABLE_KINDS =
%i[scalar collection_native collection_cb
nested ordered_deferred raw content
content_deferred].freeze
CONTENT_KINDS =
%i[content content_deferred].freeze

Class Method Summary collapse

Class Method Details

.call(instance, plan, register = nil) ⇒ Object

plan: the compiler's entry for instance's class



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lutaml/xml/plan_serializer.rb', line 28

def call(instance, plan, register = nil)
  # The child model's declared lutaml_default_register takes
  # precedence over the ambient register — the resolve_for_child
  # contract (#876); without it, child plans and attribute
  # serialization resolve in the parent context.
  register = Lutaml::Model::Register.resolve_for_child(
    instance.class,
    register || Lutaml::Model::Config.default_register,
  )
  return nil if plan[:ordered] && instance.element_order.nil?

  doc = ::Leptris::XML::Document.create
  root = doc.create_element(plan[:tree][:name])
  doc.root = root
  if plan[:ordered]
    build_ordered(root, instance, plan, doc, register)
  else
    build(root, instance, plan, doc, register)
  end
  doc.to_xml(indent: 2, no_decl: true)
end

.serializable?(plan) ⇒ Boolean

Whether the compiled plan is serialize-shaped. Ordered/mixed models serialize through build_ordered from element_order; plain models through plan rows.

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/lutaml/xml/plan_serializer.rb', line 53

def serializable?(plan)
  plan[:rows].all? do |_rule, _attr, kind, _sp, _del|
    SERIALIZABLE_KINDS.include?(kind)
  end
end