Class: Lutaml::Xml::Mapper

Inherits:
Shale::Mapper
  • Object
show all
Defined in:
lib/lutaml/xml/mapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Mapper

Returns a new instance of Mapper.



441
442
443
444
445
# File 'lib/lutaml/xml/mapper.rb', line 441

def initialize(*args)
  @all_content = []

  super
end

Instance Attribute Details

#all_contentObject

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength rubocop:enable Metrics/BlockLength rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity



439
440
441
# File 'lib/lutaml/xml/mapper.rb', line 439

def all_content
  @all_content
end

Class Method Details

.as_xml(instance, node_name = nil, doc = nil, _cdata = nil, only: nil, except: nil, context: nil, version: nil) ⇒ ::REXML::Document, ...

Convert Object to XML document

Parameters:

  • instance (any)

    Object to convert

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

    XML node name

  • doc (Shale::Adapter::<xml adapter>::Document, nil) (defaults to: nil)

    Object to convert

  • only (Array<Symbol>) (defaults to: nil)
  • except (Array<Symbol>) (defaults to: nil)
  • context (any) (defaults to: nil)

Returns:

  • (::REXML::Document, ::Nokogiri::Document, ::Ox::Document)

Raises:

  • (IncorrectModelError)


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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/lutaml/xml/mapper.rb', line 204

def as_xml(
  instance,
  node_name = nil,
  doc = nil,
  _cdata = nil,
  only: nil,
  except: nil,
  context: nil,
  version: nil
)
  unless instance.is_a?(model)
    msg = "argument is a '#{instance.class}' but should be a '#{model}'"
    raise IncorrectModelError, msg
  end

  unless doc
    doc = Shale.xml_adapter.create_document(version)

    element = as_xml(
      instance,
      xml_mapping.prefixed_root,
      doc,
      only: only,
      except: except,
      context: context,
    )
    doc.add_element(doc.doc, element)

    return doc.doc
  end

  element = doc.create_element(node_name)

  doc.add_namespace(
    xml_mapping.default_namespace.prefix,
    xml_mapping.default_namespace.name,
  )

  grouping = Shale::Mapping::Group::XmlGrouping.new

  only = to_partial_render_attributes(only)
  except = to_partial_render_attributes(except)

  # rubocop:disable Metrics/BlockNesting
  xml_mapping.attributes.each_value do |mapping|
    if mapping.group
      grouping.add(mapping, :attribute, nil)
    elsif mapping.method_to
      mapper = new

      if mapper.method(mapping.method_to).arity == 4
        mapper.send(mapping.method_to, instance, element, doc, context)
      else
        mapper.send(mapping.method_to, instance, element, doc)
      end
    else
      receiver = if mapping.receiver
                   instance.send(mapping.receiver)
                 else
                   instance
                 end

      receiver_attributes = get_receiver_attributes(mapping)
      attribute = receiver_attributes[mapping.attribute]
      next unless attribute

      next if only && !only.key?(attribute.name)
      next if except&.key?(attribute.name)

      value = receiver.send(attribute.name) if receiver

      if mapping.render_nil? || !value.nil?
        doc.add_namespace(mapping.namespace.prefix,
                          mapping.namespace.name)
        doc.add_attribute(element, mapping.prefixed_name, value)
      end
    end
  end
  # rubocop:enable Metrics/BlockNesting

  # rubocop:disable Metrics/BlockNesting
  instance.all_content.each do |mapping, content|
    if mapping == "comment"
      comment = Nokogiri::XML::Comment.new(doc.doc, content)
      doc.add_element(element, comment)
    elsif mapping == "content"
      content_mapping = xml_mapping.content
      if content_mapping
        if content_mapping.group
          grouping.add(content_mapping, :content, nil)
        elsif content_mapping.method_to
          mapper = new

          if mapper.method(content_mapping.method_to).arity == 4
            mapper.send(content_mapping.method_to, instance, element,
                        doc, context)
          else
            mapper.send(content_mapping.method_to, instance, element, doc)
          end
        else
          receiver_attributes = get_receiver_attributes(content_mapping)
          attribute = receiver_attributes[content_mapping.attribute]

          if attribute
            skip = false

            skip = true if only && !only.key?(attribute.name)
            skip = true if except&.key?(attribute.name)

            unless skip
              # value = receiver.send(attribute.name) if receiver
              value = content

              if content_mapping.cdata
                doc.create_cdata(value, element)
              else
                a = Nokogiri::XML::Text.new(value, doc.doc)
                doc.add_element(element, a)
              end
            end
          end
        end
      end
    elsif mapping.group
      grouping.add(mapping, :element, nil)
    elsif mapping.method_to
      mapper = new

      if mapper.method(mapping.method_to).arity == 4
        mapper.send(mapping.method_to, instance, element, doc, context)
      else
        mapper.send(mapping.method_to, instance, element, doc)
      end
    else
      receiver_attributes = get_receiver_attributes(mapping)
      attribute = receiver_attributes[mapping.attribute]
      next unless attribute

      if only
        attribute_only = only[attribute.name]
        next unless only.key?(attribute.name)
      end

      if except
        attribute_except = except[attribute.name]
        next if except.key?(attribute.name) && attribute_except.nil?
      end

      value = content

      if mapping.render_nil? || !value.nil?
        doc.add_namespace(mapping.namespace.prefix,
                          mapping.namespace.name)
      end

      if value.nil?
        if mapping.render_nil?
          child = doc.create_element(mapping.prefixed_name)
          doc.add_element(element, child)
        end
      elsif attribute.collection?
        [*value].each do |v|
          next if v.nil?

          child = attribute.type.as_xml(
            v,
            mapping.prefixed_name,
            doc,
            mapping.cdata,
            only: attribute_only,
            except: attribute_except,
            context: context,
          )
          doc.add_element(element, child)
        end
      else
        child = attribute.type.as_xml(
          value,
          mapping.prefixed_name,
          doc,
          mapping.cdata,
          only: attribute_only,
          except: attribute_except,
          context: context,
        )
        doc.add_element(element, child)
      end
    end
  end
  # rubocop:enable Metrics/BlockNesting

  grouping.each do |group|
    mapper = new

    if mapper.method(group.method_to).arity == 4
      mapper.send(group.method_to, instance, element, doc, context)
    else
      mapper.send(group.method_to, instance, element, doc)
    end
  end

  element
end

.get_content_value(content_mapping, element, only, except, delegates, instance) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/lutaml/xml/mapper.rb', line 408

def get_content_value(content_mapping, element, only, except, delegates, instance)
  receiver_attributes = get_receiver_attributes(content_mapping)
  attribute = receiver_attributes[content_mapping.attribute]

  if attribute
    skip = false

    # rubocop:disable Metrics/BlockNesting
    skip = true if only && !only.key?(attribute.name)
    skip = true if except&.key?(attribute.name)

    unless skip
      value = attribute.type.cast(attribute.type.of_xml(element))

      if content_mapping.receiver
        delegates.add(attributes[content_mapping.receiver],
                      attribute.setter, value)
      else
        instance.send(attribute.setter, value)
      end
    end
    # rubocop:enable Metrics/BlockNesting
  end
end

.of_xml(element, only: nil, except: nil, context: nil) ⇒ model instance

Convert XML document to Object

Parameters:

  • element (Shale::Adapter::<XML adapter>::Node)
  • only (Array<Symbol>) (defaults to: nil)
  • except (Array<Symbol>) (defaults to: nil)
  • context (any) (defaults to: nil)

Returns:

  • (model instance)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
93
94
95
96
97
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/lutaml/xml/mapper.rb', line 22

def of_xml(element, only: nil, except: nil, context: nil)
  instance = model.new

  attributes
    .values
    .select(&:default)
    .each { |attr| instance.send(attr.setter, attr.default.call) }

  grouping = Shale::Mapping::Group::XmlGrouping.new
  delegates = Shale::Mapping::Delegates.new

  only = to_partial_render_attributes(only)
  except = to_partial_render_attributes(except)

  element.attributes.each do |key, value|
    mapping = xml_mapping.attributes[key.to_s]
    next unless mapping

    if mapping.group
      grouping.add(mapping, :attribute, value)
    elsif mapping.method_from
      mapper = new

      if mapper.method(mapping.method_from).arity == 3
        mapper.send(mapping.method_from, instance, value, context)
      else
        mapper.send(mapping.method_from, instance, value)
      end
    else
      receiver_attributes = get_receiver_attributes(mapping)
      attribute = receiver_attributes[mapping.attribute]
      next unless attribute

      next if only && !only.key?(attribute.name)
      next if except&.key?(attribute.name)

      casted_value = attribute.type.cast(value)

      if attribute.collection?
        if mapping.receiver
          delegates.add_collection(
            attributes[mapping.receiver],
            attribute.setter,
            casted_value,
          )
        else
          instance.send(attribute.name) << casted_value
        end
      elsif mapping.receiver
        delegates.add(attributes[mapping.receiver], attribute.setter,
                      casted_value)
      else
        instance.send(attribute.setter, casted_value)
      end
    end
  end

  content_mapping = xml_mapping.content

  if content_mapping
    if content_mapping.group
      grouping.add(content_mapping, :content, element)
    elsif content_mapping.method_from
      mapper = new

      if mapper.method(content_mapping.method_from).arity == 3
        mapper.send(content_mapping.method_from, instance, element,
                    context)
      else
        mapper.send(content_mapping.method_from, instance, element)
      end
    else
      get_content_value(content_mapping, element, only, except,
                        delegates, instance)
    end
  end

  # rubocop:disable Metrics/BlockNesting
  element.instance_variable_get(:@node)&.children&.each do |nokogiri_node|
    if nokogiri_node.name == "text"
      content_value = get_content_value(content_mapping, nokogiri_node,
                                        only, except, delegates, instance)
      instance.all_content << ["content", content_value]
      next
    elsif nokogiri_node.name == "comment"
      instance.all_content << ["comment", nokogiri_node.text]
      next
    end

    node = element.class.new(nokogiri_node)
    mapping = xml_mapping.elements[node.name]

    next unless mapping

    if mapping.group
      grouping.add(mapping, :element, node)
    elsif mapping.method_from
      mapper = new

      if mapper.method(mapping.method_from).arity == 3
        mapper.send(mapping.method_from, instance, node, context)
      else
        mapper.send(mapping.method_from, instance, node)
      end
    else
      receiver_attributes = get_receiver_attributes(mapping)
      attribute = receiver_attributes[mapping.attribute]
      next unless attribute

      if only
        attribute_only = only[attribute.name]
        next unless only.key?(attribute.name)
      end

      if except
        attribute_except = except[attribute.name]
        next if except.key?(attribute.name) && attribute_except.nil?
      end

      value = attribute.type.of_xml(
        node,
        only: attribute_only,
        except: attribute_except,
        context: context,
      )

      casted_value = attribute.type.cast(value)
      instance.all_content << [mapping, casted_value]

      if attribute.collection?
        if mapping.receiver
          delegates.add_collection(
            attributes[mapping.receiver],
            attribute.setter,
            casted_value,
          )
        else
          instance.send(attribute.name) << casted_value
        end
      elsif mapping.receiver
        delegates.add(attributes[mapping.receiver], attribute.setter,
                      casted_value)
      else
        instance.send(attribute.setter, casted_value)
      end
    end
  end
  # rubocop:enable Metrics/BlockNesting

  delegates.each do |delegate|
    receiver = get_receiver(instance, delegate.receiver_attribute)
    receiver.send(delegate.setter, delegate.value)
  end

  grouping.each do |group|
    mapper = new

    if mapper.method(group.method_from).arity == 3
      mapper.send(group.method_from, instance, group.dict, context)
    else
      mapper.send(group.method_from, instance, group.dict)
    end
  end

  instance
end