Class: Lutaml::Xml::TypeNamespace::Reference

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

Overview

Value object representing a reference to a type namespace

Type namespaces are declared on attribute types via xml_namespace. This reference tracks the attribute, rule, and context for later resolution.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, rule, context, mapper_class: nil) ⇒ Reference

Returns a new instance of Reference.

Parameters:

  • attribute (Attribute)

    The attribute definition

  • rule (MappingRule)

    The mapping rule

  • context (Symbol)

    :attribute or :element

  • mapper_class (Class, nil) (defaults to: nil)

    The model class that owns this attribute



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 17

def initialize(attribute, rule, context, mapper_class: nil)
  unless %i[
    attribute element
  ].include?(context)
    raise ArgumentError,
          "Context must be :attribute or :element"
  end

  @attribute = attribute
  @rule = rule
  @context = context
  @mapper_class = mapper_class
  freeze
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



11
12
13
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 11

def attribute
  @attribute
end

#contextObject (readonly)

Returns the value of attribute context.



11
12
13
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 11

def context
  @context
end

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



11
12
13
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 11

def mapper_class
  @mapper_class
end

#ruleObject (readonly)

Returns the value of attribute rule.



11
12
13
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 11

def rule
  @rule
end

Instance Method Details

#attribute_context?Boolean

Check if this is for an attribute context

Returns:

  • (Boolean)


60
61
62
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 60

def attribute_context?
  @context == :attribute
end

#element_context?Boolean

Check if this is for an element context

Returns:

  • (Boolean)


65
66
67
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 65

def element_context?
  @context == :element
end

#namespace_class(register) ⇒ XmlNamespace?

Get the namespace class from the type, resolving the register context from the mapper_class if available.

Parameters:

  • register (Object)

    The type register

Returns:



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

def namespace_class(register)
  type = type_class(register)
  return nil unless type.is_a?(Class) && type <= Lutaml::Model::Type::Value

  type.namespace_class
end

#type_class(register) ⇒ Class?

Get the type class from the attribute, resolving the register context from the mapper_class if available.

Parameters:

  • register (Object)

    The type register

Returns:

  • (Class, nil)

    The type class



37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/xml/type_namespace/reference.rb', line 37

def type_class(register)
  resolved = if @mapper_class
               Lutaml::Model::Register.resolve_for_child(@mapper_class,
                                                         register)
             else
               register
             end
  @attribute.type(resolved)
end