Class: Coradoc::AsciiDoc::Model::Serialization::AsciidocTransform

Inherits:
Lutaml::Model::Transform
  • Object
show all
Defined in:
lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb

Overview

Transform class for converting between Coradoc::Element and Lutaml::Model objects

This handles bidirectional transformation for AsciiDoc serialization:

  • data_to_model: Coradoc::Element -> Lutaml::Model::Serializable

  • model_to_data: Lutaml::Model::Serializable -> Coradoc::Element

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.data_to_model(context, data, _format, options = {}) ⇒ Lutaml::Model::Serializable

Convert Coradoc::Element data to Lutaml::Model instance

Parameters:

  • context (Context)

    The context with attribute and mapping management

  • data (Coradoc::Element::Base)

    The Coradoc element to convert

  • format (Symbol)

    The format type (:asciidoc)

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

    Additional options

Returns:

  • (Lutaml::Model::Serializable)

    The model instance



21
22
23
# File 'lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb', line 21

def self.data_to_model(context, data, _format, options = {})
  new(context).data_to_model(data, options)
end

.model_to_data(context, model, _format, options = {}) ⇒ Coradoc::Element::Base

Convert Lutaml::Model instance to Coradoc::Element data

Parameters:

  • context (Context)

    The context with attribute and mapping management

  • model (Lutaml::Model::Serializable)

    The model to convert

  • format (Symbol)

    The format type (:asciidoc)

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

    Additional options

Returns:

  • (Coradoc::Element::Base)

    The Coradoc element



32
33
34
# File 'lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb', line 32

def self.model_to_data(context, model, _format, options = {})
  new(context).model_to_data(model, options)
end

Instance Method Details

#data_to_model(data, options = {}) ⇒ Object

Transform Coradoc::Element to model instance



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
# File 'lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb', line 37

def data_to_model(data, options = {})
  return nil if data.nil?

  # Get mappings for asciidoc format
  mappings = context.mappings_for(:asciidoc)
  return nil unless mappings

  mapping_rules = mappings.mappings
  return nil unless mapping_rules

  # Find the parsed_element mapping to get target class
  parsed_element_rule = mapping_rules.find { |m| m.field_type == :parsed_element }
  return nil unless parsed_element_rule

  target_class = parsed_element_rule.to
  return nil unless target_class

  # Validate data type if from is specified
  return nil if parsed_element_rule.from && !data.is_a?(parsed_element_rule.from)

  # Create instance and populate attributes
  instance = target_class.new
  attributes = target_class.attributes
  defaults_used = []

  mapping_rules.reject(&:model_map?).each do |rule|
    attr = attributes[rule.to]
    next if attr&.derived?

    # Get value from source data
    source_value = data.public_send(rule.name || rule.to)

    # Transform the value
    value = transform_value(source_value, attr, options)

    # Handle defaults
    if value.nil? && (instance.using_default?(rule.to) || rule.render_default)
      defaults_used << rule.to
      value = attr&.default || rule.to_value_for(instance)
    end

    # Apply value map if present
    value = apply_value_map(value, rule.value_map(:from, options), attr)

    # Set the value
    instance.public_send(:"#{rule.to}=", value) if value
  end

  # Mark defaults as used
  defaults_used.each { |attr_name| instance.using_default_for(attr_name) }

  instance
end

#model_to_data(model, options = {}) ⇒ Object

Transform model instance to Coradoc::Element



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
# File 'lib/coradoc/asciidoc/model/serialization/asciidoc_transform.rb', line 92

def model_to_data(model, options = {})
  return nil if model.nil?

  # Get mappings for asciidoc format
  mappings = context.mappings_for(:asciidoc)
  return nil unless mappings

  mapping_rules = mappings.mappings
  return nil unless mapping_rules

  # Find the parsed_element mapping to get target class
  parsed_element_rule = mapping_rules.find { |m| m.field_type == :parsed_element }
  return nil unless parsed_element_rule

  target_class = parsed_element_rule.from
  return nil unless target_class

  # Create instance and populate attributes
  attributes = model.class.attributes
  instance = target_class.new

  mapping_rules.reject(&:model_map?).each do |rule|
    attr = attributes[rule.to]
    next if attr&.derived?

    # Get value from model
    model_value = model.public_send(rule.to)
    next if model_value.nil?

    # Transform the value back
    value = transform_value_to_data(model_value, attr, options)

    # Set on target instance
    target_attr = rule.name || rule.to
    instance.public_send(:"#{target_attr}=", value)
  end

  instance
end