Class: Lutaml::Model::Type::Reference

Inherits:
Value
  • Object
show all
Defined in:
lib/lutaml/model/type/reference.rb

Constant Summary

Constants inherited from Value

Value::EMPTY_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Value

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

cast, format_type_serializer_for, from_format, #initialized?, register_format_to_from_methods, register_format_type_serializer, #to_s

Methods included from Xml::Type::Configurable

included

Methods included from UninitializedClassGuard

#cast, #serialize

Constructor Details

#initialize(model_class, key_attribute, key = nil) ⇒ Reference

Returns a new instance of Reference.



9
10
11
12
13
14
# File 'lib/lutaml/model/type/reference.rb', line 9

def initialize(model_class, key_attribute, key = nil)
  @model_class = model_class.to_s
  @key_attribute = key_attribute
  @key = key
  super(object)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/lutaml/model/type/reference.rb', line 7

def key
  @key
end

Class Method Details

.cast_with_metadata(value, model_class, key_attribute) ⇒ Object

Enhanced casting method that receives metadata



34
35
36
37
38
# File 'lib/lutaml/model/type/reference.rb', line 34

def self.(value, model_class, key_attribute)
  return value if value.is_a?(Reference)

  new(model_class, key_attribute, value)
end

.serialize(value) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/lutaml/model/type/reference.rb', line 40

def self.serialize(value)
  case value
  when Reference
    value.key
  else
    value.to_s
  end
end

.xsd_type(target_xsd_type = nil) ⇒ String

XSD type for Reference

Returns xs:IDREF if the target uses xs:ID, otherwise xs:string. This is a three-tier architecture:

  1. Application level: Reference objects for type safety

  2. Schema level: xs:IDREF for validation

  3. Instance level: key values for serialization

Parameters:

  • target_xsd_type (String, nil) (defaults to: nil)

    the XSD type of the target attribute

Returns:

  • (String)

    xs:IDREF or xs:string



59
60
61
# File 'lib/lutaml/model/type/reference.rb', line 59

def self.xsd_type(target_xsd_type = nil)
  target_xsd_type == "xs:ID" ? "xs:IDREF" : "xs:string"
end

Instance Method Details

#objectObject



16
17
18
19
20
21
22
23
# File 'lib/lutaml/model/type/reference.rb', line 16

def object
  return @value if resolved?
  return nil unless @key

  @value = Lutaml::Model::Store.resolve(@model_class, @key_attribute,
                                        @key)
  @value
end

#pretty_print_instance_variablesObject



25
26
27
# File 'lib/lutaml/model/type/reference.rb', line 25

def pretty_print_instance_variables
  (instance_variables - %i[@value @key_attribute @model_class]).sort
end

#resolved?Boolean

Returns:



29
30
31
# File 'lib/lutaml/model/type/reference.rb', line 29

def resolved?
  model_instance?(@value)
end