Class: Lutaml::Model::Type::Reference
- Defined in:
- lib/lutaml/model/type/reference.rb
Constant Summary
Constants inherited from Value
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Attributes inherited from Value
Class Method Summary collapse
-
.cast_with_metadata(value, model_class, key_attribute) ⇒ Object
Enhanced casting method that receives metadata.
- .serialize(value) ⇒ Object
-
.xsd_type(target_xsd_type = nil) ⇒ String
XSD type for Reference.
Instance Method Summary collapse
-
#initialize(model_class, key_attribute, key = nil) ⇒ Reference
constructor
A new instance of Reference.
- #object ⇒ Object
- #pretty_print_instance_variables ⇒ Object
- #resolved? ⇒ Boolean
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
Methods included from UninitializedClassGuard
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
#key ⇒ Object (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:
-
Application level: Reference objects for type safety
-
Schema level: xs:IDREF for validation
-
Instance level: key values for serialization
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
#object ⇒ Object
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_variables ⇒ Object
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
29 30 31 |
# File 'lib/lutaml/model/type/reference.rb', line 29 def resolved? model_instance?(@value) end |