Module: Chemicalml::VersionedParser

Included in:
Cml::Schema24, Cml::Schema3
Defined in:
lib/chemicalml/versioned_parser.rb

Overview

Shared parse entrypoint for versioned schema modules. Each schema module (Cml::Schema3, Cml::Schema24) extends this.

Polymorphic: detects the XML root element and dispatches to the right wire class's from_xml. Supported roots include <cml> (Document), <module> (compchem Module), <dictionary>, <unitList>, <unitTypeList>, and the legacy standalone roots <molecule>, <reaction>, <reactionList>.

Constant Summary collapse

KNOWN_ROOTS =

Maps root element name → constant name on the schema module. Adding a new root type = adding one entry here. OCP — no existing code changes.

{
  "cml"          => :Document,
  "molecule"     => :Molecule,
  "reaction"     => :Reaction,
  "reactionList" => :ReactionList,
  "module"       => :Module,
  "dictionary"   => :Dictionary,
  "unitList"     => :UnitList,
  "unitTypeList" => :UnitTypeList,
}.freeze

Instance Method Summary collapse

Instance Method Details

#configurationObject



55
56
57
# File 'lib/chemicalml/versioned_parser.rb', line 55

def configuration
  const_get(:Configuration)
end

#document_classObject



51
52
53
# File 'lib/chemicalml/versioned_parser.rb', line 51

def document_class
  const_get(:Document)
end

#parse(xml, namespace_exist: true) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chemicalml/versioned_parser.rb', line 27

def parse(xml, namespace_exist: true)
  configuration.ensure_registered!
  root = root_element_of(xml)

  # Resolve the wire class for this root element via O(1) hash
  # lookup. KNOWN_ROOTS provides explicit overrides; the reverse
  # index of Elements::ALL covers everything else.
  class_name = KNOWN_ROOTS[root] || Chemicalml::Cml::Elements::XML_TO_CLASS[root]
  unless class_name
    raise ArgumentError,
          "unsupported CML root element <#{root}>"
  end
  unless const_defined?(class_name, false)
    raise ArgumentError,
          "#{name} does not define #{class_name} " \
          "(not all root elements exist in every schema version)"
  end

  const_get(class_name).from_xml(
    xml_input(xml, namespace_exist, root),
    register: configuration.context_id
  )
end