Class: Lutaml::Xml::Decisions::Rules::InheritParentPrefixRule

Inherits:
DecisionRule
  • Object
show all
Defined in:
lib/lutaml/xml/decisions/rules/inherit_parent_prefix_rule.rb

Overview

Array position 2.5: Inherit parent’s prefix format

When parent uses prefix format and child shares the same namespace, child should use the same prefix to stay in that namespace.

This handles the case where:

  • Grandparent hoisted namespace as prefix

  • Parent uses prefix format but doesn’t re-declare (already hoisted)

  • Child needs to use prefix to stay in same namespace

Instance Method Summary collapse

Methods inherited from DecisionRule

#<=>, #name, #priority

Instance Method Details

#applies?(context) ⇒ Boolean

Applies when:

  • Element has namespace

  • Parent uses prefix format

  • Element shares parent’s namespace

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/lutaml/xml/decisions/rules/inherit_parent_prefix_rule.rb', line 22

def applies?(context)
  return false unless context.has_namespace?
  return false unless context.parent_uses_prefix_format?
  return true if context.same_namespace_as_parent?

  false
end

#decide(context) ⇒ Object

Decision: Use parent’s prefix (actual or default)



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/lutaml/xml/decisions/rules/inherit_parent_prefix_rule.rb', line 31

def decide(context)
  # Try to get the actual prefix being used by parent from parent_hoisted
  # This handles custom prefix case (e.g., prefix: "v" instead of "vcard")
  prefix = find_prefix_for_namespace(context.parent_hoisted,
                                     context.namespace_uri)

  # If not found in parent_hoisted, check for root_prefix
  # When user specifies prefix: "v", root_prefix contains "v"
  if prefix.nil? && context.root_prefix
    prefix = context.root_prefix
  end

  # Fall back to default prefix if still not found
  prefix ||= context.parent_namespace_class.prefix_default

  Decision.prefix(
    prefix: prefix,
    namespace_class: context.namespace_class,
    reason: "Inherit parent's prefix format",
  )
end