Module: Chemicalml::Cml::WireClassRegistry

Defined in:
lib/chemicalml/cml/wire_class_registry.rb

Overview

Lookup table from (schema, role) to the schema's wire class implementing that role. Used by the Translator to instantiate the right class for the requested schema — eliminates the hardcoded Cml::Foo (always Schema3) references that caused from_canonical(schema: :schema24) to produce Schema3 children inside a Schema24 document.

Adding Schema5 later requires no changes here — the registry walks the schema module's constants lazily.

Constant Summary collapse

SCHEMA_MODULES =
{
  schema3:  Chemicalml::Cml::Schema3,
  schema24: Chemicalml::Cml::Schema24
}.freeze

Class Method Summary collapse

Class Method Details

.for(schema, role) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/chemicalml/cml/wire_class_registry.rb', line 20

def self.for(schema, role)
  schema_module = SCHEMA_MODULES[schema.to_sym]
  raise ArgumentError, "unknown schema: #{schema.inspect}" unless schema_module

  role_name = role_name_for(role)
  raise ArgumentError, "unknown role: #{role.inspect}" unless role_name
  unless schema_module.const_defined?(role_name, false)
    raise ArgumentError,
          "#{schema_module.name} does not define #{role_name} " \
          "(not all elements exist in every schema version)"
  end

  schema_module.const_get(role_name, false)
end

.role_name_for(role) ⇒ Object



35
36
37
38
39
# File 'lib/chemicalml/cml/wire_class_registry.rb', line 35

def self.role_name_for(role)
  return role.name.split("::").last if role.is_a?(::Module)

  role.to_s
end