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
Defined Under Namespace
Classes: KvRulePlan, KvSerializePlan
Instance Method Summary collapse
- #data_to_model(data, format, options = {}) ⇒ Object
- #model_to_data(instance, format, options = {}) ⇒ Object
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 27 28 29 30 31 32 33 |
# 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 # TODO.max-perf/32: constant per (model class, register) — the # transform itself is cached per that pair, so resolve once. child_register = @kv_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) rules = mappings.mappings(lutaml_register) # lutaml-model#88: nil unless the mapping partitions a wire key # with when_attribute rules — the common case pays one scan. partition = kv_partition(rules) rules.each do |rule| process_mapping_rule(data, instance, format, rule, , partition) end instance end |
#model_to_data(instance, format, options = {}) ⇒ Object
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 |
# File 'lib/lutaml/key_value/transform.rb', line 35 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.is_a?(Lutaml::KeyValue::Transformation) # 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) rules = mappings.mappings(lutaml_register) partition = kv_partition(rules) hash = {} handled_groups = nil rules.each do |rule| next unless valid_mapping?(rule, ) group = partition && kv_partition_group(rule, partition) if group next if handled_groups&.include?(group) (handled_groups ||= []) << group process_partition_group!(instance, group, hash, format, ) else process_rule!(instance, rule, hash, format, mappings, ) end end hash.keys == [""] ? hash[""] : hash end |