Class: Lutaml::KeyValue::Transformation::CollectionSerializer

Inherits:
Object
  • Object
show all
Includes:
Model::RenderPolicy
Defined in:
lib/lutaml/key_value/transformation/collection_serializer.rb

Overview

Serializes collections for key-value formats.

This is an independent class with explicit dependencies that can be tested in isolation from Transformation.

Handles:

  • Array collections

  • Keyed collections (map_key feature)

  • Root mappings pattern

  • render_nil and render_empty options

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Model::RenderPolicy

derived_attribute_for?, #should_skip_delegated_value?, #should_skip_value?

Constructor Details

#initialize(format:, register_id:, value_serializer:, transformation_factory:) ⇒ CollectionSerializer

Initialize the CollectionSerializer with explicit dependencies.

Parameters:

  • format (Symbol)

    The serialization format

  • register_id (Symbol, nil)

    The register ID

  • value_serializer (ValueSerializer)

    The value serializer

  • transformation_factory (Proc)

    Factory lambda ->(type_class) { Transformation }



38
39
40
41
42
43
44
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 38

def initialize(format:, register_id:, value_serializer:,
transformation_factory:)
  @format = format
  @register_id = register_id
  @value_serializer = value_serializer
  @transformation_factory = transformation_factory
end

Instance Attribute Details

#formatSymbol (readonly)

Returns The serialization format (:json, :yaml, :toml).

Returns:

  • (Symbol)

    The serialization format (:json, :yaml, :toml)



21
22
23
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 21

def format
  @format
end

#register_idSymbol? (readonly)

Returns The register ID for attribute lookup.

Returns:

  • (Symbol, nil)

    The register ID for attribute lookup



24
25
26
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 24

def register_id
  @register_id
end

#transformation_factoryProc (readonly)

Returns Factory lambda for creating child transformations.

Returns:

  • (Proc)

    Factory lambda for creating child transformations



30
31
32
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 30

def transformation_factory
  @transformation_factory
end

#value_serializerValueSerializer (readonly)

Returns The value serializer for item serialization.

Returns:



27
28
29
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 27

def value_serializer
  @value_serializer
end

Instance Method Details

#keyed_collection_key_attribute(child_mappings) ⇒ Symbol?

Get the key attribute for a keyed collection from child_mappings.

Parameters:

  • child_mappings (Hash)

    The child mappings (e.g., { id: :key })

Returns:

  • (Symbol, nil)

    The attribute name to use as key, or nil



95
96
97
98
99
100
101
102
103
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 95

def keyed_collection_key_attribute(child_mappings)
  return nil unless child_mappings

  child_mappings.each do |attr_name, mapping|
    return attr_name.to_sym if mapping == :key
  end

  nil
end

#serialize(parent, collection, rule, options = {}, converted_from_empty_to_nil: false, converted_from_nil_to_empty: false) ⇒ void

This method returns an undefined value.

Serialize a collection to elements.

This is the main entry point for collection serialization.

Parameters:

  • parent (Lutaml::KeyValue::DataModel::Element)

    Parent element

  • collection (Object)

    The collection to serialize

  • rule (CompiledRule)

    The compiled rule

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

    Serialization options

  • converted_from_empty_to_nil (Boolean) (defaults to: false)

    Whether nil was converted from empty

  • converted_from_nil_to_empty (Boolean) (defaults to: false)

    Whether empty was converted from nil



57
58
59
60
61
62
63
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 57

def serialize(parent, collection, rule, options = {},
              converted_from_empty_to_nil: false,
              converted_from_nil_to_empty: false)
  create_collection_element(parent, collection, rule, options,
                            converted_from_empty_to_nil: converted_from_empty_to_nil,
                            converted_from_nil_to_empty: converted_from_nil_to_empty)
end

#serialize_array(parent, items, rule, options = {}) ⇒ void

This method returns an undefined value.

Serialize an array collection.

Parameters:

  • parent (Lutaml::KeyValue::DataModel::Element)

    Parent element

  • items (Array)

    The collection items

  • rule (CompiledRule)

    The compiled rule

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

    Serialization options



87
88
89
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 87

def serialize_array(parent, items, rule, options = {})
  create_array_collection_element(parent, rule, items, options)
end

#serialize_keyed(parent, items, rule, key_attribute, child_mappings, options = {}) ⇒ void

This method returns an undefined value.

Serialize a keyed collection (map_key feature).

Parameters:

  • parent (Lutaml::KeyValue::DataModel::Element)

    Parent element

  • items (Array)

    The collection items

  • rule (CompiledRule)

    The compiled rule

  • key_attribute (Symbol)

    The attribute to use as hash key

  • child_mappings (Hash)

    The child mappings configuration

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

    Serialization options



74
75
76
77
78
# File 'lib/lutaml/key_value/transformation/collection_serializer.rb', line 74

def serialize_keyed(parent, items, rule, key_attribute, child_mappings,
options = {})
  create_keyed_collection_element(parent, rule, items, key_attribute,
                                  child_mappings, options)
end