Class: Lutaml::ModelTransformations::Configuration

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/lutaml/model_transformations/configuration.rb

Overview

Configuration service for model transformations using external YAML configuration.

This class follows the Dependency Inversion Principle by allowing external configuration instead of hardcoded behavior. It uses lutaml-model for structured YAML parsing and validation.

Examples:

Load default configuration

config = Configuration.load
puts config.enabled_formats

Load custom configuration

config = Configuration.load("my_config.yml")
parser_config = config.parser_config_for("xmi")

Defined Under Namespace

Classes: ErrorHandling, FormatDetection, ParserConfig, TransformationOptions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_default_configurationConfiguration

Create default configuration when no config file exists

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lutaml/model_transformations/configuration.rb', line 145

def create_default_configuration
  new.tap do |config|
    config.version = "1.0"
    config.description = "Default Model Transformations Configuration"

    # Default parsers
    config.parsers = [
      create_xmi_parser_config,
      create_qea_parser_config,
    ]

    # Default options
    config.transformation_options = TransformationOptions.new
    config.format_detection = FormatDetection.new
    config.error_handling = ErrorHandling.new
  end
end

.default_config_pathString

Get default configuration file path

Returns:

  • (String)

    Path to default config file



138
139
140
# File 'lib/lutaml/model_transformations/configuration.rb', line 138

def default_config_path
  File.expand_path("../../../config/model_transformations.yml", __dir__)
end

.load(config_path = nil) ⇒ Configuration

Load configuration from YAML file

Parameters:

  • config_path (String, nil) (defaults to: nil)

    Path to configuration file Defaults to config/model_transformations.yml

Returns:

Raises:

  • (Errno::ENOENT)

    if config file not found

  • (Lutaml::Model::Error)

    if YAML is invalid



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lutaml/model_transformations/configuration.rb', line 123

def load(config_path = nil)
  config_path ||= default_config_path

  unless File.exist?(config_path)
    # Create default configuration if none exists
    return create_default_configuration
  end

  yaml_content = File.read(config_path)
  from_yaml(yaml_content)
end

Instance Method Details

#content_sniffing_enabled?Boolean

Check if content sniffing is enabled

Returns:

  • (Boolean)

    true if content sniffing should be used



250
251
252
# File 'lib/lutaml/model_transformations/configuration.rb', line 250

def content_sniffing_enabled?
  format_detection&.use_content_sniffing == true
end

#enabled_formatsArray<String>

Get all enabled format names

Returns:

  • (Array<String>)

    Array of enabled format names



236
237
238
# File 'lib/lutaml/model_transformations/configuration.rb', line 236

def enabled_formats
  enabled_parsers.map(&:format)
end

#enabled_parsersArray<ParserConfig>

Get list of enabled parsers, sorted by priority

Returns:

  • (Array<ParserConfig>)

    Array of enabled parser configurations



199
200
201
# File 'lib/lutaml/model_transformations/configuration.rb', line 199

def enabled_parsers
  parsers&.select(&:enabled)&.sort_by { |p| -p.priority } || []
end

#error_handlingErrorHandling

Get error handling configuration with defaults

Returns:



278
279
280
# File 'lib/lutaml/model_transformations/configuration.rb', line 278

def error_handling
  @error_handling ||= ErrorHandling.new
end

#fallback_parserString?

Get fallback parser when format detection fails

Returns:

  • (String, nil)

    The fallback parser class name



264
265
266
# File 'lib/lutaml/model_transformations/configuration.rb', line 264

def fallback_parser
  format_detection&.fallback_parser
end

#file_extension_detection_enabled?Boolean

Check if file extension detection is enabled

Returns:

  • (Boolean)

    true if file extension should be used for detection



257
258
259
# File 'lib/lutaml/model_transformations/configuration.rb', line 257

def file_extension_detection_enabled?
  format_detection&.use_file_extension == true
end

#format_enabled?(format) ⇒ Boolean

Check if a format is enabled

Parameters:

  • format (String)

    The format name

Returns:

  • (Boolean)

    true if format is enabled



228
229
230
231
# File 'lib/lutaml/model_transformations/configuration.rb', line 228

def format_enabled?(format)
  parser = parser_config_for(format)
  parser&.enabled == true
end

#merge(other) ⇒ Configuration

Merge with another configuration (this takes precedence)

Parameters:

Returns:



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/lutaml/model_transformations/configuration.rb', line 286

def merge(other) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  merged = self.class.new

  # Basic attributes
  merged.version = version || other.version
  merged.description = description || other.description

  # Merge parsers (this config takes precedence)
  merged.parsers = merge_parsers(other.parsers)

  # Use this config's options, fallback to other
  merged.transformation_options = transformation_options ||
    other.transformation_options
  merged.format_detection = format_detection || other.format_detection
  merged.error_handling = error_handling || other.error_handling

  merged
end

#parser_config_for(format) ⇒ ParserConfig?

Get parser configuration by format name

Parameters:

  • format (String)

    The format name (e.g., “xmi”, “qea”)

Returns:

  • (ParserConfig, nil)

    The parser configuration or nil if not found



207
208
209
# File 'lib/lutaml/model_transformations/configuration.rb', line 207

def parser_config_for(format)
  parsers&.find { |p| p.format == format.downcase }
end

#parser_config_for_extension(extension) ⇒ ParserConfig?

Get parser configuration by file extension

Parameters:

  • extension (String)

    The file extension (e.g., “.xmi”, “.qea”)

Returns:

  • (ParserConfig, nil)

    The parser configuration or nil if not found



215
216
217
218
219
220
221
222
# File 'lib/lutaml/model_transformations/configuration.rb', line 215

def parser_config_for_extension(extension)
  normalized_ext = extension.downcase
  unless normalized_ext.start_with?(".")
    normalized_ext = ".#{normalized_ext}"
  end

  enabled_parsers.find { |p| p.handles_extension?(normalized_ext) }
end

#supported_extensionsArray<String>

Get all supported file extensions

Returns:

  • (Array<String>)

    Array of supported extensions



243
244
245
# File 'lib/lutaml/model_transformations/configuration.rb', line 243

def supported_extensions
  enabled_parsers.filter_map(&:extension)
end

#transformation_optionsTransformationOptions

Get transformation options with defaults

Returns:



271
272
273
# File 'lib/lutaml/model_transformations/configuration.rb', line 271

def transformation_options
  @transformation_options ||= TransformationOptions.new
end