Module: Lutaml::Xml::TransformationSupport::RuleApplier
- Includes:
- ElementBuilder, SkipLogic
- Included in:
- Lutaml::Xml::Transformation
- Defined in:
- lib/lutaml/xml/transformation/rule_applier.rb
Overview
Module for applying transformation rules to create XML elements.
Dispatches rule application to specific handlers based on mapping type:
-
Element rules -> apply_element_rule
-
Attribute rules -> apply_attribute_rule
-
Content rules -> apply_content_rule
-
Raw rules -> apply_raw_rule
Instance Method Summary collapse
-
#apply_attribute_rule(parent, rule, value, options, model_class, register_id) ⇒ Object
Apply an attribute rule.
-
#apply_content_rule(parent, rule, value, options, model_class, register_id) ⇒ Object
Apply a content mapping rule (map_content directive).
-
#apply_element_rule(parent, rule, value, options, model_class, register_id, register) ⇒ Object
Apply an element rule.
-
#apply_raw_rule(parent, _rule, value) ⇒ Object
Apply a raw mapping rule (map_all directive).
-
#apply_rule(parent, rule, model_instance, options, model_class, register_id, register) ⇒ Object
Apply a single transformation rule.
Methods included from ElementBuilder
#add_hash_children, #apply_element_content, #apply_nil_marker, #apply_nil_marker_for_empty, #apply_nil_marker_for_nil, #apply_nil_marker_for_uninitialized, #create_element_for_value, #create_fallback_nested_element, #create_nested_model_element, #create_simple_value_element, #create_transformed_nested_element, #determine_element_namespace, #namespace_prefix_valid_for_class, #resolve_polymorphic_class
Methods included from ValueSerializer
Methods included from SkipLogic
#should_skip_delegated_value?, #should_skip_value?
Methods included from Model::RenderPolicy
derived_attribute_for?, #should_skip_delegated_value?, #should_skip_value?
Instance Method Details
#apply_attribute_rule(parent, rule, value, options, model_class, register_id) ⇒ Object
Apply an attribute rule
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 |
# File 'lib/lutaml/xml/transformation/rule_applier.rb', line 127 def apply_attribute_rule(parent, rule, value, , model_class, register_id) # Handle custom serialization methods if rule.has_custom_methods? && rule.custom_methods[:to] apply_custom_method(parent, rule, model_class, [:current_model]) return end text = serialize_value(value, rule, model_class, register_id) return if text.nil? # Determine attribute namespace - type namespace takes precedence attr_type = rule.attribute_type attr_namespace_class = if attr_type.is_a?(Class) && attr_type <= Lutaml::Model::Type::Value && attr_type.namespace_class attr_type.namespace_class else rule.namespace_class end attr = ::Lutaml::Xml::DataModel::XmlAttribute.new( rule.serialized_name, text, attr_namespace_class, ) parent.add_attribute(attr) end |
#apply_content_rule(parent, rule, value, options, model_class, register_id) ⇒ Object
Apply a content mapping rule (map_content directive)
For mixed content, when value is an Array, each item is added as a separate child text node. For regular content, value is set as text_content.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/lutaml/xml/transformation/rule_applier.rb', line 167 def apply_content_rule(parent, rule, value, , model_class, register_id) # Handle custom serialization methods if rule.has_custom_methods? && rule.custom_methods[:to] apply_custom_method(parent, rule, model_class, [:current_model]) return end return if value.nil? return if Lutaml::Model::Utils.uninitialized?(value) xml_mapping = model_class.mappings_for(:xml) is_mixed = rule.mixed_content || xml_mapping&.mixed_content? || value.is_a?(Array) if value.is_a?(Array) && is_mixed apply_mixed_content(parent, value) else # For regular content, serialize value and set as text_content text = serialize_value(value, rule, model_class, register_id) parent.text_content = text if text end parent.cdata = rule.cdata if rule.cdata end |
#apply_element_rule(parent, rule, value, options, model_class, register_id, register) ⇒ Object
Apply an element rule
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 |
# File 'lib/lutaml/xml/transformation/rule_applier.rb', line 78 def apply_element_rule(parent, rule, value, , model_class, register_id, register) # Handle custom serialization methods if rule.has_custom_methods? && rule.custom_methods[:to] apply_custom_method(parent, rule, model_class, [:current_model]) return end # raw: :element — value is a complete XML element string, inject directly if rule.raw == :element add_raw_element_fragments(parent, value) return end # Extract parent's namespace info for element_form_default inheritance parent_ns_class = parent.namespace_class # Only pass element_form_default VALUE if it was explicitly set # When not set (defaults to :unqualified), pass nil to avoid incorrect blank namespace treatment parent_element_form_default = if parent_ns_class&.element_form_default_set? parent_ns_class.element_form_default end # Performance: Only create new options hash if values differ # This avoids allocations for unchanged namespace inheritance if [:parent_namespace_class] == parent_ns_class && [:parent_element_form_default] == parent_element_form_default && [:parent_element] == parent = else = .dup [:parent_namespace_class] = parent_ns_class [:parent_element_form_default] = parent_element_form_default [:parent_element] = parent end apply_element_value(parent, rule, value, , model_class, register_id, register) end |
#apply_raw_rule(parent, _rule, value) ⇒ Object
Apply a raw mapping rule (map_all directive)
Raw content is the entire inner XML as a string, including elements and text. Store it on the parent element so adapters can serialize it as raw XML fragment.
201 202 203 204 205 206 |
# File 'lib/lutaml/xml/transformation/rule_applier.rb', line 201 def apply_raw_rule(parent, _rule, value) return unless value return if value.to_s.empty? parent.raw_content = value.to_s end |
#apply_rule(parent, rule, model_instance, options, model_class, register_id, register) ⇒ Object
Apply a single transformation rule
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 |
# File 'lib/lutaml/xml/transformation/rule_applier.rb', line 26 def apply_rule(parent, rule, model_instance, , model_class, register_id, register) # Skip pseudo-rules like root_namespace return if rule.option(:mapping_type) == :root_namespace # Check if this is a custom-method-only rule is_custom_method_only = custom_method_only?(rule) # Get attribute value - handle delegation and custom methods value = extract_rule_value(rule, model_instance, is_custom_method_only) # Handle render options and value_map should_skip = if is_custom_method_only false elsif rule.option(:delegate_from) delegate_obj = model_instance.public_send(rule.option(:delegate_from)) should_skip_delegated_value?(value, rule, delegate_obj) else should_skip_value?(value, rule, model_instance) end return if should_skip # Apply export transformation if present BEFORE any other processing value = rule.transform_value(value, :export) if rule.value_transformer # Handle based on rule type case rule.option(:mapping_type) when :element apply_element_rule(parent, rule, value, , model_class, register_id, register) when :attribute apply_attribute_rule(parent, rule, value, , model_class, register_id) when :content apply_content_rule(parent, rule, value, , model_class, register_id) when :raw apply_raw_rule(parent, rule, value) end end |