Class: Lutaml::Xml::NamespaceDeclaration

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/xml/namespace_declaration.rb

Overview

Represents a single namespace declaration in an XML element

REFACTORED (Session 176): Stores DATA only, NO XML strings Accepts NamespaceDeclarationData in constructor Adapters build xmlns strings from this data

CRITICAL ARCHITECTURAL PRINCIPLE: This class stores WHAT and HOW (data decisions) Adapters build the actual xmlns=“uri” or xmlns:prefix=“uri” strings

Examples:

Default namespace declaration

data = NamespaceDeclarationData.new(
  namespace_class: MyNamespace,
  format: :default,
  declared_at: :here
)
decl = NamespaceDeclaration.new(data)

Prefixed namespace declaration

data = NamespaceDeclarationData.new(
  namespace_class: MyNamespace,
  format: :prefix,
  declared_at: :inherited
)
decl = NamespaceDeclaration.new(data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ NamespaceDeclaration

Initialize from NamespaceDeclarationData

Parameters:



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/lutaml/xml/namespace_declaration.rb', line 54

def initialize(data)
  unless data.is_a?(NamespaceDeclarationData)
    raise ArgumentError,
          "Expected NamespaceDeclarationData, got #{data.class}"
  end

  @ns_object = data.namespace_class
  @format = data.format
  @declared_at = data.declared_at
  @source = data.source
  @prefix_override = data.prefix_override
end

Instance Attribute Details

#declared_atSymbol (readonly)

Returns Where this declaration is made

  • :here - declared at this element

  • :inherited - inherited from parent

  • :local_on_use - should be declared locally when used.

Returns:

  • (Symbol)

    Where this declaration is made

    • :here - declared at this element

    • :inherited - inherited from parent

    • :local_on_use - should be declared locally when used



43
44
45
# File 'lib/lutaml/xml/namespace_declaration.rb', line 43

def declared_at
  @declared_at
end

#formatSymbol (readonly)

Returns Format of declaration (:default or :prefix).

Returns:

  • (Symbol)

    Format of declaration (:default or :prefix)



37
38
39
# File 'lib/lutaml/xml/namespace_declaration.rb', line 37

def format
  @format
end

#ns_objectClass (readonly)

Returns The XmlNamespace class for this declaration.

Returns:

  • (Class)

    The XmlNamespace class for this declaration



34
35
36
# File 'lib/lutaml/xml/namespace_declaration.rb', line 34

def ns_object
  @ns_object
end

#prefix_overrideString? (readonly)

Returns Custom prefix override from options.

Returns:

  • (String, nil)

    Custom prefix override from options



49
50
51
# File 'lib/lutaml/xml/namespace_declaration.rb', line 49

def prefix_override
  @prefix_override
end

#sourceSymbol? (readonly)

Returns Source of this declaration (:input for parsed XML).

Returns:

  • (Symbol, nil)

    Source of this declaration (:input for parsed XML)



46
47
48
# File 'lib/lutaml/xml/namespace_declaration.rb', line 46

def source
  @source
end

Instance Method Details

#attribute_form_defaultSymbol

Get attribute_form_default setting

Returns:

  • (Symbol)

    :qualified or :unqualified



144
145
146
147
148
149
150
# File 'lib/lutaml/xml/namespace_declaration.rb', line 144

def attribute_form_default
  if @ns_object.respond_to?(:attribute_form_default)
    @ns_object.attribute_form_default
  else
    :unqualified # W3C default
  end
end

#declared_here?Boolean

Check if this declaration is made at this element

Returns:

  • (Boolean)

    true if declared_at is :here



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

def declared_here?
  @declared_at == :here
end

#default_format?Boolean

Check if this declaration uses default format

Returns:

  • (Boolean)

    true if format is :default



91
92
93
# File 'lib/lutaml/xml/namespace_declaration.rb', line 91

def default_format?
  @format == :default
end

#element_form_defaultSymbol

Get element_form_default setting

Returns:

  • (Symbol)

    :qualified or :unqualified



133
134
135
136
137
138
139
# File 'lib/lutaml/xml/namespace_declaration.rb', line 133

def element_form_default
  if @ns_object.respond_to?(:element_form_default)
    @ns_object.element_form_default
  else
    :qualified # W3C default
  end
end

#from_input?Boolean

Check if this declaration came from input XML

Returns:

  • (Boolean)

    true if source is :input



126
127
128
# File 'lib/lutaml/xml/namespace_declaration.rb', line 126

def from_input?
  @source == :input
end

#inherited?Boolean

Check if this declaration is inherited from parent

Returns:

  • (Boolean)

    true if declared_at is :inherited



112
113
114
# File 'lib/lutaml/xml/namespace_declaration.rb', line 112

def inherited?
  @declared_at == :inherited
end

#inspectString

String representation for debugging

Returns:

  • (String)


184
185
186
# File 'lib/lutaml/xml/namespace_declaration.rb', line 184

def inspect
  "#<NamespaceDeclaration #{@ns_object} format=#{@format} declared_at=#{@declared_at}>"
end

#keyString

Get the namespace key for lookups

Returns:

  • (String)

    The namespace key



70
71
72
# File 'lib/lutaml/xml/namespace_declaration.rb', line 70

def key
  @ns_object.to_key
end

#local_on_use?Boolean

Check if this declaration should be made locally on use

Returns:

  • (Boolean)

    true if declared_at is :local_on_use



119
120
121
# File 'lib/lutaml/xml/namespace_declaration.rb', line 119

def local_on_use?
  @declared_at == :local_on_use
end

#merge(attrs) ⇒ NamespaceDeclaration

Create a copy with updated attributes

Parameters:

  • attrs (Hash)

    Attributes to update

Returns:



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/lutaml/xml/namespace_declaration.rb', line 156

def merge(attrs)
  # Create new NamespaceDeclarationData with merged attributes
  data = NamespaceDeclarationData.new(
    namespace_class: attrs[:ns_object] || @ns_object,
    format: attrs[:format] || @format,
    declared_at: attrs[:declared_at] || @declared_at,
    source: attrs.key?(:source) ? attrs[:source] : @source,
    prefix_override: attrs.key?(:prefix_override) ? attrs[:prefix_override] : @prefix_override,
  )
  NamespaceDeclaration.new(data)
end

#prefixString?

Get the namespace prefix (if any)

Returns:

  • (String, nil)

    The namespace prefix (override if present, otherwise default)



84
85
86
# File 'lib/lutaml/xml/namespace_declaration.rb', line 84

def prefix
  @prefix_override || @ns_object.prefix_default
end

#prefix_format?Boolean

Check if this declaration uses prefix format

Returns:

  • (Boolean)

    true if format is :prefix



98
99
100
# File 'lib/lutaml/xml/namespace_declaration.rb', line 98

def prefix_format?
  @format == :prefix
end

#to_hHash

Convert to hash for debugging

Returns:

  • (Hash)

    Hash representation



171
172
173
174
175
176
177
178
179
# File 'lib/lutaml/xml/namespace_declaration.rb', line 171

def to_h
  {
    ns_object: @ns_object,
    format: @format,
    declared_at: @declared_at,
    source: @source,
    prefix_override: @prefix_override,
  }.compact
end

#uriString

Get the namespace URI

Returns:

  • (String)

    The namespace URI



77
78
79
# File 'lib/lutaml/xml/namespace_declaration.rb', line 77

def uri
  @ns_object.uri
end

#xmlns_declarationString

Backward compatibility: Build xmlns declaration string (Legacy API from pre-Session 176 refactoring)

Returns:

  • (String)

    The xmlns attribute string (e.g., “xmlns="uri"” or “xmlns:prefix="uri"”)



192
193
194
195
196
197
198
199
# File 'lib/lutaml/xml/namespace_declaration.rb', line 192

def xmlns_declaration
  if @format == :default
    "xmlns=\"#{uri}\""
  else
    pfx = prefix
    "xmlns:#{pfx}=\"#{uri}\""
  end
end