Module: Lutaml::Model::Serialize::AttributeDefinition

Included in:
ClassMethods
Defined in:
lib/lutaml/model/serialize/attribute_definition.rb

Overview

Handles attribute definition methods for Serialize::ClassMethods

Extracted from serialize.rb to improve code organization. Provides methods for defining and validating model attributes.

Instance Method Summary collapse

Instance Method Details

#any_importable_models?Boolean

Check if there are any importable models

Returns:

  • (Boolean)

    True if there are pending imports



210
211
212
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 210

def any_importable_models?
  importable_choices.any? || importable_models.any?
end

#attribute(name, type, options = {}) ⇒ Attribute

Define an attribute for the model

Parameters:

  • name (Symbol)

    The attribute name

  • type (Class, Symbol, Hash)

    The attribute type

  • options (Hash) (defaults to: {})

    Attribute options

Returns:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 169

def attribute(name, type, options = {})
  type, options = process_type_hash(type, options) if type.is_a?(::Hash)

  # Handle direct method option in options hash
  if options[:method]
    options[:method_name] = options.delete(:method)
  end

  attr = Attribute.new(name, type, options)
  @attributes[name] = attr
  define_attribute_methods(attr)

  attr
end

#define_attribute_methods(attr, register = nil) ⇒ Object

Define attribute methods on the model class

Parameters:

  • attr (Attribute)

    The attribute to define methods for

  • register (Symbol, nil) (defaults to: nil)

    The register for type resolution



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 15

def define_attribute_methods(attr, register = nil)
  name = attr.name
  register_id = extract_register_id(register)

  if attr.enum?
    add_enum_methods_to_model(
      model,
      name,
      attr.options[:values],
      collection: attr.options[:collection],
    )
  elsif attr.derived? && name != attr.method_name
    unless method_defined?(name, false)
      define_method(name) do
        value = public_send(attr.method_name)
        # Cast the derived value to the specified type
        attr.cast_element(value, register_id)
      end
    end
  elsif attr.unresolved_type == Lutaml::Model::Type::Reference
    define_reference_methods(name, register_id)
  else
    define_regular_attribute_methods(name, attr)
  end
end

#define_reference_methods(name, register) ⇒ Object

Define reference-type attribute methods

Reference types store a reference key that can be resolved to the actual object.

Parameters:

  • name (Symbol)

    The attribute name

  • register (Symbol)

    The register ID



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 48

def define_reference_methods(name, register)
  register_id = register
  attr = attributes[name]

  unless method_defined?(:"#{name}_ref", false)
    define_method("#{name}_ref") do
      instance_variable_get(:"@#{name}_ref")
    end
  end

  key_method_name = if attr.options[:collection]
                      Utils.pluralize(attr.options[:ref_key_attribute].to_s)
                    else
                      attr.options[:ref_key_attribute]
                    end

  unless method_defined?(:"#{name}_#{key_method_name}", false)
    define_method("#{name}_#{key_method_name}") do
      ref = instance_variable_get(:"@#{name}_ref")
      resolve_reference_key(ref)
    end
  end

  unless method_defined?(name, false)
    define_method(name) do
      ref = instance_variable_get(:"@#{name}_ref")
      resolve_reference_value(ref)
    end
  end

  unless method_defined?(:"#{name}=", false)
    define_method(:"#{name}=") do |value|
      value_set_for(name)
      casted_value = value
      unless casted_value.is_a?(Lutaml::Model::Type::Reference)
        casted_value = attr.cast_value(value, register_id)
      end

      instance_variable_set(:"@#{name}_ref", casted_value)

      resolved_reference = resolve_reference_key(casted_value)
      instance_variable_set(:"@#{name}", resolved_reference)
    end
  end
end

#define_regular_attribute_methods(name, attr) ⇒ Object

Define regular (non-reference, non-enum) attribute methods

Parameters:

  • name (Symbol)

    The attribute name

  • attr (Attribute)

    The attribute definition



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 98

def define_regular_attribute_methods(name, attr)
  # For collection attributes, the getter accepts an optional argument
  # for builder-style syntax: g.member(item) appends to the collection
  if attr.collection?
    define_method(name) do |*args|
      if args.empty?
        instance_variable_get(:"@#{name}")
      else
        # Builder-style: g.member(item) appends to collection
        value = args.first
        current = instance_variable_get(:"@#{name}") || []
        new_value = current.is_a?(Array) ? current + [value] : value
        instance_variable_set(:"@#{name}", new_value)
        # Track order for mixed_content serialization
        track_order(name, value, nil) if @__order_tracking__
        value
      end
    end
  else
    # For non-collection attributes, getter accepts optional argument
    # for builder-style syntax: g.description(value) sets the value
    define_method(name) do |*args|
      if args.empty?
        instance_variable_get(:"@#{name}")
      else
        # Builder-style: g.description(value) sets the value
        value = args.first
        send(:"#{name}=", value)
        # Track order for mixed_content serialization
        track_order(name, value, nil) if @__order_tracking__
        value
      end
    end
  end

  enum_shorthand_names = instance_variable_get(:@__enum_shorthand_names__) || Set.new
  unless method_defined?(:"#{name}=",
                         false) && !enum_shorthand_names.include?(name.to_s)
    if attr.collection?
      define_method(:"#{name}=") do |value|
        value_set_for(name)
        value = attr.cast_value(value, lutaml_register)
        # Preserve the frozen sentinel when the deserialization pipeline
        # would overwrite it with nil/UninitializedClass (meaning "no data
        # found for this collection"). This maintains the zero-allocation
        # guarantee for unused collections. The sentinel is replaced with
        # a real Array only when actual data is set.
        current = instance_variable_get(:"@#{name}")
        if current.equal?(LAZY_EMPTY_COLLECTION) &&
            (value.nil? || Lutaml::Model::Utils.uninitialized?(value))
          # Sentinel stays — no allocation for truly empty collections
        else
          instance_variable_set(:"@#{name}", value)
        end
      end
    else
      define_method(:"#{name}=") do |value|
        value_set_for(name)
        value = attr.cast_value(value, lutaml_register)
        instance_variable_set(:"@#{name}", value)
      end
    end
  end
end

#restrict(name, options = {}) ⇒ Symbol

Restrict options on an existing attribute

Parameters:

  • name (Symbol)

    The attribute name to restrict

  • options (Hash) (defaults to: {})

    New options to merge

Returns:

  • (Symbol)

    The attribute name



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 189

def restrict(name, options = {})
  register_id = options.delete(:register) || Lutaml::Model::Config.default_register

  if !@attributes.key?(name) && !register_record(register_id)&.dig(
    :attributes, name
  )
    return restrict_attributes[name] = options if any_importable_models?

    raise Lutaml::Model::UndefinedAttributeError.new(name, self)
  end

  validate_attribute_options!(name, options)
  attr = attributes(register_id)[name]
  attr.options.merge!(options)
  attr.process_options!
  name
end

#validate_attribute_options!(name, options) ⇒ Object

Validate attribute options

Parameters:

  • name (Symbol)

    The attribute name

  • options (Hash)

    The options to validate

Raises:



219
220
221
222
223
224
225
# File 'lib/lutaml/model/serialize/attribute_definition.rb', line 219

def validate_attribute_options!(name, options)
  invalid_opts = options.keys - Attribute::ALLOWED_OPTIONS
  return if invalid_opts.empty?

  raise Lutaml::Model::InvalidAttributeOptionsError.new(name,
                                                        invalid_opts)
end