Class: Lutaml::KeyValue::Transform
- Inherits:
-
Model::Transform
- Object
- Model::Transform
- Lutaml::KeyValue::Transform
- Defined in:
- lib/lutaml/key_value/transform.rb
Direct Known Subclasses
HashFormat::Adapter::Transform, Json::Adapter::Transform, Jsonl::Adapter::Transform, Adapter::Hash::Transform, Adapter::Json::Transform, Adapter::Jsonl::Transform, Adapter::Toml::Transform, Adapter::Yaml::Transform, Adapter::Yamls::Transform, Toml::Adapter::Transform, Yaml::Adapter::Transform, Yamls::Adapter::Transform
Instance Attribute Summary
Attributes inherited from Model::Transform
#attributes, #context, #lutaml_register
Instance Method Summary collapse
- #data_to_model(data, format, options = {}) ⇒ Object
- #model_to_data(instance, format, options = {}) ⇒ Object
Methods inherited from Model::Transform
data_to_model, #initialize, #model_class, model_to_data
Constructor Details
This class inherits a constructor from Lutaml::Model::Transform
Instance Method Details
#data_to_model(data, format, options = {}) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/lutaml/key_value/transform.rb', line 4 def data_to_model(data, format, = {}) # Use child's own default register if it has one # This ensures versioned schemas (e.g., MML v2 with lutaml_default_register = :mml_v2) # are instantiated with their native context child_register = Lutaml::Model::Register.resolve_for_child( model_class, lutaml_register ) if model_class.include?(Lutaml::Model::Serialize) instance = model_class.new(lutaml_register: child_register) else instance = model_class.new register_accessor_methods_for(instance, child_register) end root_and_parent_assignment(instance, ) mappings = extract_mappings(, format) mappings.mappings(lutaml_register).each do |rule| process_mapping_rule(data, instance, format, rule, ) end instance end |
#model_to_data(instance, format, options = {}) ⇒ Object
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 |
# File 'lib/lutaml/key_value/transform.rb', line 28 def model_to_data(instance, format, = {}) # NEW ARCHITECTURE: Use KeyValue::Transformation if available # This provides symmetric OOP architecture with XmlDataModel if context.is_a?(Class) && context.include?(Lutaml::Model::Serialize) transformation = context.transformation_for(format, lutaml_register) # transformation_for returns nil for cyclic dependencies or :building sentinel # Fall back to legacy approach in these cases if transformation.respond_to?(:transform) # Use new Transformation to get KeyValueElement kv_element = transformation.transform(instance, ) # Convert KeyValueElement to hash for backward compatibility with adapters # The to_hash method returns {"__root__" => {actual_hash}} kv_hash = kv_element.to_hash # For root element, return just the content hash return kv_hash["__root__"] || kv_hash end end # LEGACY ARCHITECTURE: Fall back to Hash-based approach # This maintains backward compatibility for models without transformations mappings = extract_mappings(, format) hash = {} mappings.mappings(lutaml_register).each do |rule| next unless valid_mapping?(rule, ) process_rule!(instance, rule, hash, format, mappings, ) end hash.keys == [""] ? hash[""] : hash end |