Module: Chemicalml::ContextConfiguration

Included in:
Chemicalml::Cml::Schema24::Configuration, Chemicalml::Cml::Schema3::Configuration
Defined in:
lib/chemicalml/context_configuration.rb

Overview

Shared behaviour for versioned schema configurations. Each schema version (Cml::Schema3, Cml::Schema24) extends this module via its own Configuration sub-module, giving it:

- `context_id` — the lutaml-model `GlobalContext` id
- `register_model(klass, id:)` — record one wire class
- `populate_context!` — rebuild the type registry on demand
- `ensure_registered!` — lazily populate on first parse

Mirrors Mml::ContextConfiguration. Adapted to Chemicalml's schema-registry terminology.

Instance Method Summary collapse

Instance Method Details

#contextObject



20
21
22
# File 'lib/chemicalml/context_configuration.rb', line 20

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

#context_idObject



16
17
18
# File 'lib/chemicalml/context_configuration.rb', line 16

def context_id
  self::CONTEXT_ID
end

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



68
69
70
71
72
73
74
75
76
77
# File 'lib/chemicalml/context_configuration.rb', line 68

def create_context(id:, registry: nil, fallback_to: [context_id])
  normalized_id = id.to_sym
  ensure_version_registered
  Lutaml::Model::GlobalContext.unregister_context(normalized_id) if Lutaml::Model::GlobalContext.context(normalized_id)
  Lutaml::Model::GlobalContext.create_context(
    id: normalized_id,
    registry: registry || Lutaml::Model::TypeRegistry.new,
    fallback_to: Array(fallback_to).map(&:to_sym)
  ).tap { Lutaml::Model::GlobalContext.clear_caches }
end

#default_context_idObject



79
80
81
# File 'lib/chemicalml/context_configuration.rb', line 79

def default_context_id
  context_id
end

#ensure_registered!Object



24
25
26
27
28
29
# File 'lib/chemicalml/context_configuration.rb', line 24

def ensure_registered!
  return if @models_registered

  register_models!
  @models_registered = true
end

#populate_context!Object



62
63
64
65
66
# File 'lib/chemicalml/context_configuration.rb', line 62

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

#register_elements!(except: []) ⇒ Object

Walk the shared Chemicalml::Cml::Elements::ALL table, look up each class on the parent (schema) module, and register it. Pass except: to skip specific elements (used by Schema 2.4 which lacks <module>).



43
44
45
46
47
48
49
50
51
52
# File 'lib/chemicalml/context_configuration.rb', line 43

def register_elements!(except: [])
  parent = version_parent_module
  skip = except.to_set
  Chemicalml::Cml::Elements::ALL.each do |class_name, element_id|
    next if skip.include?(class_name)
    next unless parent.const_defined?(class_name)

    register_model(parent.const_get(class_name), id: element_id)
  end
end

#register_model(klass, id:) ⇒ Object



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

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

#register_models!Object

Default: subclasses override with a centralized registration block (V4-style). The fallback eager-loads every autoloaded constant on the parent module.



34
35
36
37
# File 'lib/chemicalml/context_configuration.rb', line 34

def register_models!
  parent = version_parent_module
  parent.constants.each { |name| parent.const_get(name) }
end