Module: Mml::ContextConfiguration

Included in:
V2::Configuration, V3::Configuration, V4::Configuration
Defined in:
lib/mml/context_configuration.rb

Instance Method Summary collapse

Instance Method Details

#adapterObject



61
62
63
# File 'lib/mml/context_configuration.rb', line 61

def adapter
  Lutaml::Model::Config.xml_adapter_type
end

#adapter=(adapter) ⇒ Object



65
66
67
# File 'lib/mml/context_configuration.rb', line 65

def adapter=(adapter)
  Lutaml::Model::Config.xml_adapter_type = adapter
end

#clear_custom_modelsObject



94
95
96
# File 'lib/mml/context_configuration.rb', line 94

def clear_custom_models
  @default_context_id = nil
end

#contextObject

Expose the version’s built-in context without mutating global state.



10
11
12
# File 'lib/mml/context_configuration.rb', line 10

def context
  Lutaml::Model::GlobalContext.context(context_id)
end

#context_idObject



5
6
7
# File 'lib/mml/context_configuration.rb', line 5

def context_id
  self::CONTEXT_ID
end

#create_context(id:, registry: nil, fallback_to: [context_id], substitutions: []) ⇒ Object

Create the built-in context or a derived context with substitutions.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mml/context_configuration.rb', line 15

def create_context(
  id:,
  registry: nil,
  fallback_to: [context_id],
  substitutions: []
)
  normalized_id = Mml::ContextOptions.normalize_context_reference(id)

  return populate_context! if normalized_id == context_id

  Lutaml::Model::GlobalContext.unregister_context(normalized_id) if Lutaml::Model::GlobalContext.context(normalized_id)
  create_type_context(
    id: normalized_id,
    registry: registry || Lutaml::Model::TypeRegistry.new,
    fallback_to: normalize_fallbacks(fallback_to),
    substitutions: substitutions,
  )
end

#custom_modelsObject



86
87
88
# File 'lib/mml/context_configuration.rb', line 86

def custom_models
  registered_substitutions ||= {}
end

#custom_models=(models) ⇒ Object

Convenience API for setting up model substitutions. Creates a derived context with the given substitutions.

Examples:

Mml::V4::Configuration.custom_models = { Mml::V4::Mover => Overset }
math = Mml::V4.parse(xml, context: :custom_models)

Parameters:

  • models (Hash)

    A hash mapping original classes to substitute classes e.g., { Mml::V4::Mover => Overset }



78
79
80
81
82
83
84
# File 'lib/mml/context_configuration.rb', line 78

def custom_models=(models)
  substitutions = models.map do |from_type, to_type|
    { from_type: from_type, to_type: to_type }
  end
  create_context(id: :custom_models, substitutions: substitutions)
  @default_context_id = :custom_models
end

#default_context_idObject



90
91
92
# File 'lib/mml/context_configuration.rb', line 90

def default_context_id
  @default_context_id || context_id
end

#populate_context!Object

Explicitly rebuild the built-in version context.



35
36
37
38
# File 'lib/mml/context_configuration.rb', line 35

def populate_context!
  Lutaml::Model::GlobalContext.unregister_context(context_id) if context
  register_models_in(base_type_context)
end

#registerObject



49
50
51
52
# File 'lib/mml/context_configuration.rb', line 49

def register
  Mml::ContextOptions.warn_register_deprecation("#{name}.register")
  @deprecated_register_context_id || context_id
end

#register=(value) ⇒ Object



54
55
56
57
58
59
# File 'lib/mml/context_configuration.rb', line 54

def register=(value)
  Mml::ContextOptions.warn_register_deprecation("#{name}.register=")
  @deprecated_register_context_id =
    Mml::ContextOptions.normalize_context_reference(value)
  create_context(id: @deprecated_register_context_id) unless Lutaml::Model::GlobalContext.context(@deprecated_register_context_id)
end

#register_model(klass, id:) ⇒ Object

Register a model now and remember it for later rebuilds.



41
42
43
44
45
46
47
# File 'lib/mml/context_configuration.rb', line 41

def register_model(klass, id:)
  normalized_id = id.to_sym
  registered_models[normalized_id] = klass
  (context || populate_base_context).registry.register(normalized_id, klass)
  clear_cache(klass)
  klass
end