Module: Lutaml::Model::Serialize::ClassMethods

Includes:
Liquefiable::ClassMethods
Defined in:
lib/lutaml/model/serialize.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Liquefiable::ClassMethods

#drop_class, #drop_class_name, #register_drop_method, #register_liquid_drop_class, #validate_liquid!

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



32
33
34
# File 'lib/lutaml/model/serialize.rb', line 32

def attributes
  @attributes
end

#choice_attributesObject

Returns the value of attribute choice_attributes.



32
33
34
# File 'lib/lutaml/model/serialize.rb', line 32

def choice_attributes
  @choice_attributes
end

#mappingsObject

Returns the value of attribute mappings.



32
33
34
# File 'lib/lutaml/model/serialize.rb', line 32

def mappings
  @mappings
end

Instance Method Details

#add_custom_handling_methods_to_model(klass) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lutaml/model/serialize.rb', line 60

def add_custom_handling_methods_to_model(klass)
  Utils.add_boolean_accessor_if_not_defined(klass, :ordered)
  Utils.add_boolean_accessor_if_not_defined(klass, :mixed)
  Utils.add_accessor_if_not_defined(klass, :element_order)
  Utils.add_accessor_if_not_defined(klass, :encoding)

  Utils.add_method_if_not_defined(klass,
                                  :using_default_for) do |attribute_name|
    @using_default ||= {}
    @using_default[attribute_name] = true
  end

  Utils.add_method_if_not_defined(klass,
                                  :value_set_for) do |attribute_name|
    @using_default ||= {}
    @using_default[attribute_name] = false
  end

  Utils.add_method_if_not_defined(klass,
                                  :using_default?) do |attribute_name|
    @using_default ||= {}
    !!@using_default[attribute_name]
  end
end

#add_enum_getter_if_not_defined(klass, enum_name, collection) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/lutaml/model/serialize.rb', line 233

def add_enum_getter_if_not_defined(klass, enum_name, collection)
  Utils.add_method_if_not_defined(klass, enum_name) do
    i = instance_variable_get(:"@#{enum_name}") || []

    if !collection && i.is_a?(Array)
      i.first
    else
      i.uniq
    end
  end
end

#add_enum_methods_to_model(klass, enum_name, values, collection: false) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/lutaml/model/serialize.rb', line 186

def add_enum_methods_to_model(klass, enum_name, values, collection: false)
  add_enum_getter_if_not_defined(klass, enum_name, collection)
  add_enum_setter_if_not_defined(klass, enum_name, values, collection)

  return unless values.all?(::String)

  values.each do |value|
    Utils.add_method_if_not_defined(klass, "#{value}?") do
      curr_value = public_send(:"#{enum_name}")

      if collection
        curr_value.include?(value)
      else
        curr_value == value
      end
    end

    Utils.add_method_if_not_defined(klass, value.to_s) do
      public_send(:"#{value}?")
    end

    Utils.add_method_if_not_defined(klass, "#{value}=") do |val|
      value_set_for(enum_name)
      enum_vals = public_send(:"#{enum_name}")

      enum_vals = if !!val
                    if collection
                      enum_vals << value
                    else
                      [value]
                    end
                  elsif collection
                    enum_vals.delete(value)
                    enum_vals
                  else
                    instance_variable_get(:"@#{enum_name}") - [value]
                  end

      instance_variable_set(:"@#{enum_name}", enum_vals)
    end

    Utils.add_method_if_not_defined(klass, "#{value}!") do
      public_send(:"#{value}=", true)
    end
  end
end

#add_enum_setter_if_not_defined(klass, enum_name, _values, collection) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/lutaml/model/serialize.rb', line 245

def add_enum_setter_if_not_defined(klass, enum_name, _values, collection)
  Utils.add_method_if_not_defined(klass, "#{enum_name}=") do |value|
    value = [] if value.nil?
    value = [value] if !value.is_a?(Array)

    value_set_for(enum_name)

    if collection
      curr_value = public_send(:"#{enum_name}")

      instance_variable_set(:"@#{enum_name}", curr_value + value)
    else
      instance_variable_set(:"@#{enum_name}", value)
    end
  end
end

#apply_mappings(doc, format, options = {}) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/lutaml/model/serialize.rb', line 357

def apply_mappings(doc, format, options = {})
  instance = options[:instance] || model.new
  return instance if Utils.blank?(doc)

  mappings = mappings_for(format)

  if mappings.polymorphic_mapping
    return resolve_polymorphic(doc, format, mappings, instance, options)
  end

  # options[:mappings] = mappings.mappings
  transformer = Lutaml::Model::Config.transformer_for(format)
  transformer.data_to_model(self, doc, format, options)
end

#apply_value_map(value, value_map, attr) ⇒ Object



383
384
385
386
387
388
389
390
391
392
393
# File 'lib/lutaml/model/serialize.rb', line 383

def apply_value_map(value, value_map, attr)
  if value.nil?
    value_for_option(value_map[:nil], attr)
  elsif Utils.empty?(value)
    value_for_option(value_map[:empty], attr, value)
  elsif Utils.uninitialized?(value)
    value_for_option(value_map[:omitted], attr)
  else
    value
  end
end

#as(format, instance, options = {}) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/lutaml/model/serialize.rb', line 316

def as(format, instance, options = {})
  if instance.is_a?(Array)
    return instance.map { |item| public_send(:"as_#{format}", item) }
  end

  unless instance.is_a?(model)
    msg = "argument is a '#{instance.class}' but should be a '#{model}'"
    raise Lutaml::Model::IncorrectModelError, msg
  end

  transformer = Lutaml::Model::Config.transformer_for(format)
  transformer.model_to_data(self, instance, format, options)
end

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

Define an attribute for the model



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lutaml/model/serialize.rb', line 121

def attribute(name, type, options = {})
  if type.is_a?(Hash)
    options[:method_name] = type[:method]
    type = nil
  end

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

  attr
end

#cast(value) ⇒ Object



85
86
87
# File 'lib/lutaml/model/serialize.rb', line 85

def cast(value)
  value
end

#choice(min: 1, max: 1, &block) ⇒ Object



89
90
91
92
93
# File 'lib/lutaml/model/serialize.rb', line 89

def choice(min: 1, max: 1, &block)
  @choice_attributes << Choice.new(self, min, max).tap do |c|
    c.instance_eval(&block)
  end
end

#default_mappings(format) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/lutaml/model/serialize.rb', line 341

def default_mappings(format)
  klass = ::Lutaml::Model::Config.mappings_class_for(format)
  mappings = klass.new

  mappings.tap do |mapping|
    attributes&.each_key do |name|
      mapping.map_element(
        name.to_s,
        to: name,
      )
    end

    mapping.root(to_s.split("::").last) if format == :xml
  end
end

#define_attribute_methods(attr) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/lutaml/model/serialize.rb', line 95

def define_attribute_methods(attr)
  name = attr.name

  if attr.enum?
    add_enum_methods_to_model(
      model,
      name,
      attr.options[:values],
      collection: attr.options[:collection],
    )
  elsif attr.derived? && name != attr.method_name
    define_method(name) do
      public_send(attr.method_name)
    end
  else
    define_method(name) do
      instance_variable_get(:"@#{name}")
    end
    define_method(:"#{name}=") do |value|
      value_set_for(name)
      instance_variable_set(:"@#{name}", attr.cast_value(value))
    end
  end
end

#empty_object(attr) ⇒ Object



402
403
404
405
406
# File 'lib/lutaml/model/serialize.rb', line 402

def empty_object(attr)
  return [] if attr.collection?

  ""
end

#ensure_utf8(value) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/lutaml/model/serialize.rb', line 408

def ensure_utf8(value)
  case value
  when String
    value.encode("UTF-8", invalid: :replace, undef: :replace,
                          replace: "")
  when Array
    value.map { |v| ensure_utf8(v) }
  when Hash
    value.transform_keys do |k|
      ensure_utf8(k)
    end.transform_values do |v|
      ensure_utf8(v)
    end
  else
    value
  end
end

#enumsObject



262
263
264
# File 'lib/lutaml/model/serialize.rb', line 262

def enums
  attributes.select { |_, attr| attr.enum? }
end

#from(format, data, options = {}) ⇒ Object



283
284
285
286
287
288
289
290
# File 'lib/lutaml/model/serialize.rb', line 283

def from(format, data, options = {})
  return data if Utils.uninitialized?(data)

  adapter = Lutaml::Model::Config.adapter_for(format)

  doc = adapter.parse(data, options)
  public_send(:"of_#{format}", doc, options)
end

#handle_key_value_mappings(mapping, format) ⇒ Object



175
176
177
178
# File 'lib/lutaml/model/serialize.rb', line 175

def handle_key_value_mappings(mapping, format)
  @mappings[format] ||= KeyValueMapping.new
  @mappings[format].mappings.concat(mapping.mappings)
end

#import_model(model) ⇒ Object



180
181
182
183
184
# File 'lib/lutaml/model/serialize.rb', line 180

def import_model(model)
  import_model_with_root_error(model)
  import_model_attributes(model)
  import_model_mappings(model)
end

#import_model_attributes(model) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/lutaml/model/serialize.rb', line 144

def import_model_attributes(model)
  model.attributes.each_value do |attr|
    define_attribute_methods(attr)
  end

  @choice_attributes.concat(Utils.deep_dup(model.choice_attributes))
  @attributes.merge!(Utils.deep_dup(model.attributes))
end

#import_model_mappings(model) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/lutaml/model/serialize.rb', line 153

def import_model_mappings(model)
  import_model_with_root_error(model)

  Lutaml::Model::Config::AVAILABLE_FORMATS.each do |format|
    next unless model.mappings.key?(format)

    mapping = model.mappings_for(format)
    mapping = Utils.deep_dup(mapping)

    klass = ::Lutaml::Model::Config.mappings_class_for(format)
    @mappings[format] ||= klass.new

    if format == :xml
      @mappings[format].merge_mapping_attributes(mapping)
      @mappings[format].merge_mapping_elements(mapping)
      @mappings[format].merge_elements_sequence(mapping)
    else
      @mappings[format].mappings.concat(mapping.mappings)
    end
  end
end

#import_model_with_root_error(model) ⇒ Object



138
139
140
141
142
# File 'lib/lutaml/model/serialize.rb', line 138

def import_model_with_root_error(model)
  return unless model.mappings.key?(:xml) && model.root?

  raise Lutaml::Model::ImportModelWithRootError.new(model)
end

#included(base) ⇒ Object



39
40
41
42
# File 'lib/lutaml/model/serialize.rb', line 39

def included(base)
  base.extend(ClassMethods)
  base.initialize_attrs(self)
end

#inherited(subclass) ⇒ Object



34
35
36
37
# File 'lib/lutaml/model/serialize.rb', line 34

def inherited(subclass)
  super
  subclass.initialize_attrs(self)
end

#initialize_attrs(source_class) ⇒ Object



44
45
46
47
48
49
# File 'lib/lutaml/model/serialize.rb', line 44

def initialize_attrs(source_class)
  @mappings = Utils.deep_dup(source_class.instance_variable_get(:@mappings)) || {}
  @attributes = Utils.deep_dup(source_class.instance_variable_get(:@attributes)) || {}
  @choice_attributes = Utils.deep_dup(source_class.instance_variable_get(:@choice_attributes)) || []
  instance_variable_set(:@model, self)
end

#key_value(&block) ⇒ Object



330
331
332
333
334
335
# File 'lib/lutaml/model/serialize.rb', line 330

def key_value(&block)
  Lutaml::Model::Config::KEY_VALUE_FORMATS.each do |format|
    mappings[format] ||= KeyValueMapping.new(format)
    mappings[format].instance_eval(&block)
  end
end

#mappings_for(format) ⇒ Object



337
338
339
# File 'lib/lutaml/model/serialize.rb', line 337

def mappings_for(format)
  mappings[format] || default_mappings(format)
end

#model(klass = nil) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/lutaml/model/serialize.rb', line 51

def model(klass = nil)
  if klass
    @model = klass
    add_custom_handling_methods_to_model(klass)
  else
    @model
  end
end

#of(format, doc, options = {}) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/lutaml/model/serialize.rb', line 292

def of(format, doc, options = {})
  if doc.is_a?(Array)
    return doc.map { |item| send(:"of_#{format}", item) }
  end

  if format == :xml
    raise Lutaml::Model::NoRootMappingError.new(self) unless root?

    options[:encoding] = doc.encoding
  end

  transformer = Lutaml::Model::Config.transformer_for(format)
  transformer.data_to_model(self, doc, format, options)
end

#process_mapping(format, &block) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/lutaml/model/serialize.rb', line 266

def process_mapping(format, &block)
  # klass = Lutaml::Model.const_get("#{format.to_s.capitalize}Mapping")
  # mappings[format] ||= klass.new
  # mappings[format].instance_eval(&block)

  # handle_root_assignment(mappings, format)

  klass = ::Lutaml::Model::Config.mappings_class_for(format)
  mappings[format] ||= klass.new

  mappings[format].instance_eval(&block)

  if mappings[format].respond_to?(:finalize)
    mappings[format].finalize(self)
  end
end

#resolve_polymorphic(doc, format, mappings, instance, options = {}) ⇒ Object



372
373
374
375
376
377
378
379
380
381
# File 'lib/lutaml/model/serialize.rb', line 372

def resolve_polymorphic(doc, format, mappings, instance, options = {})
  polymorphic_mapping = mappings.polymorphic_mapping
  return instance if polymorphic_mapping.polymorphic_map.empty?

  klass_key = doc[polymorphic_mapping.name]
  klass_name = polymorphic_mapping.polymorphic_map[klass_key]
  klass = Object.const_get(klass_name)

  klass.apply_mappings(doc, format, options)
end

#root?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/lutaml/model/serialize.rb', line 134

def root?
  mappings_for(:xml)&.root?
end

#to(format, instance, options = {}) ⇒ Object



307
308
309
310
311
312
313
314
# File 'lib/lutaml/model/serialize.rb', line 307

def to(format, instance, options = {})
  value = public_send(:"as_#{format}", instance, options)
  adapter = Lutaml::Model::Config.adapter_for(format)

  options[:mapper_class] = self if format == :xml

  adapter.new(value).public_send(:"to_#{format}", options)
end

#value_for_option(option, attr, empty_value = nil) ⇒ Object



395
396
397
398
399
400
# File 'lib/lutaml/model/serialize.rb', line 395

def value_for_option(option, attr, empty_value = nil)
  return nil if option == :nil
  return empty_value || empty_object(attr) if option == :empty

  Lutaml::Model::UninitializedClass.instance
end