Class: Lutaml::Model::Transformation Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/transformation.rb

Overview

This class is abstract.

Subclasses must implement #transform and #compile_rules

Abstract base class for format-specific transformations.

A Transformation converts a model instance into a format-specific intermediate representation (like XmlElement) without triggering type resolution or imports during the transformation process.

Transformations are pre-compiled at class definition time and frozen to prevent modifications. They contain all necessary information (compiled rules, namespaces, etc.) for transforming instances.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, mapping_dsl, format, register) ⇒ Transformation

Initialize a new transformation

Parameters:

  • model_class (Class)

    The model class to transform

  • mapping_dsl (Mapping, nil)

    The mapping DSL to compile

  • format (Symbol)

    The format (:xml, :json, :yaml, etc.)

  • register (Register, nil)

    The register for type resolution



35
36
37
38
39
40
41
# File 'lib/lutaml/model/transformation.rb', line 35

def initialize(model_class, mapping_dsl, format, register)
  @model_class = model_class
  @format = format
  @register = register
  @compiled_rules = compile_rules(mapping_dsl)
  freeze
end

Instance Attribute Details

#compiled_rulesArray<CompiledRule> (readonly)

Returns Pre-compiled transformation rules.

Returns:

  • (Array<CompiledRule>)

    Pre-compiled transformation rules



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

def compiled_rules
  @compiled_rules
end

#formatSymbol (readonly)

Returns The format (:xml, :json, :yaml, etc.).

Returns:

  • (Symbol)

    The format (:xml, :json, :yaml, etc.)



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

def format
  @format
end

#model_classClass (readonly)

Returns The model class this transformation applies to.

Returns:

  • (Class)

    The model class this transformation applies to



18
19
20
# File 'lib/lutaml/model/transformation.rb', line 18

def model_class
  @model_class
end

#registerRegister? (readonly)

Returns The register used for type resolution.

Returns:

  • (Register, nil)

    The register used for type resolution



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

def register
  @register
end

Instance Method Details

#all_namespacesArray<Class>

Collect all namespaces used in this transformation

This method traverses compiled rules recursively to collect all namespace classes, enabling namespace declaration planning without triggering type resolution.

Returns:

  • (Array<Class>)

    Array of XmlNamespace classes



62
63
64
65
66
67
68
# File 'lib/lutaml/model/transformation.rb', line 62

def all_namespaces
  namespaces = []
  compiled_rules.each do |rule|
    namespaces.concat(rule.all_namespaces)
  end
  namespaces.uniq
end

#transform(model_instance, options = {}) ⇒ Object

This method is abstract.

Subclasses must implement this method

Transform a model instance into format-specific representation

Parameters:

  • model_instance (Object)

    The model instance to transform

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

    Format-specific options

Returns:

  • (Object)

    Format-specific intermediate representation

Raises:

  • (NotImplementedError)

    if not implemented by subclass



50
51
52
53
# File 'lib/lutaml/model/transformation.rb', line 50

def transform(model_instance, options = {})
  raise NotImplementedError,
        "#{self.class}#transform must be implemented"
end