Module: Chemicalml::Dictionary::Registry
- Defined in:
- lib/chemicalml/dictionary/registry.rb
Overview
Registry of built-in dictionaries. Lazily loads YAML from
data/dictionaries/ on first access; subsequent lookups hit the
in-memory cache.
Adding a new built-in dictionary = dropping a new .yaml file
under data/dictionaries/. No code change required.
Class Method Summary collapse
- .builtin_names ⇒ Object
- .load_builtin(name) ⇒ Object
- .load_by_prefix(prefix) ⇒ Object
- .lookup(qname) ⇒ Object
- .lookup!(qname) ⇒ Object
- .reset! ⇒ Object
Class Method Details
.builtin_names ⇒ Object
51 52 53 54 55 56 |
# File 'lib/chemicalml/dictionary/registry.rb', line 51 def self.builtin_names Dir.glob(File.join(BUILTIN_DIR, "*.yaml")) .reject { |p| File.basename(p).start_with?("_") } .map { |p| File.basename(p, ".yaml").to_sym } .sort end |
.load_builtin(name) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chemicalml/dictionary/registry.rb', line 29 def self.load_builtin(name) @mutex.synchronize do @cache[name.to_s] ||= begin path = File.join(Chemicalml::Dictionary::BUILTIN_DIR, "#{name}.yaml") raise KeyError, "no built-in dictionary #{name.inspect}" unless File.exist?(path) Loader.from_file(path) end end end |
.load_by_prefix(prefix) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/chemicalml/dictionary/registry.rb', line 40 def self.load_by_prefix(prefix) manifest.each do |name, info| return load_builtin(name) if info["prefix"] == prefix end nil end |
.lookup(qname) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/chemicalml/dictionary/registry.rb', line 15 def self.lookup(qname) prefix, id = qname.to_s.split(":", 2) return nil unless id dict = load_by_prefix(prefix) return nil unless dict dict.lookup(qname) end |
.lookup!(qname) ⇒ Object
25 26 27 |
# File 'lib/chemicalml/dictionary/registry.rb', line 25 def self.lookup!(qname) lookup(qname) || raise(KeyError, "no dictionary entry #{qname.inspect}") end |
.reset! ⇒ Object
47 48 49 |
# File 'lib/chemicalml/dictionary/registry.rb', line 47 def self.reset! @mutex.synchronize { @cache.clear } end |