Class: Metanorma::Plugin::Glossarist::DatasetRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/plugin/glossarist/dataset_registry.rb

Overview

Resolves, caches, and exposes dataset models for a document.

Single source of truth for everything the preprocessor needs from a glossarist dataset: concepts, section hierarchy, bibliography, and dataset-level non-verbal entities (figures, tables, formulas). Each is exposed as the typed Glossarist model object so callers never poke at raw YAML hashes.

Constant Summary collapse

BIBLIOGRAPHY_FILENAME =
"bibliography.yaml"
REGISTER_FILENAME =
"register.yaml"
NON_VERBAL_KINDS =

Map of plural kind symbol → (collection class, subdirectory name). Adding a new non-verbal kind = adding one entry here. The accessor {kind}_for and loader are derived from this table.

{
  figures: [::Glossarist::Collections::FigureCollection, "figures"],
  tables: [::Glossarist::Collections::TableCollection, "tables"],
  formulas: [::Glossarist::Collections::FormulaCollection, "formulas"],
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeDatasetRegistry

Returns a new instance of DatasetRegistry.



26
27
28
29
30
31
32
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 26

def initialize
  @stores = {}
  @registers = {}
  @bibliographies = {}
  @non_verbal = {}
  @context_paths = {}
end

Instance Method Details

#bibliography_for(context_name) ⇒ Object

Returns the BibliographyData for a registered context, or nil. Exposed as the typed model so callers iterate entries via BibliographyEntry accessors (#id, #reference, #title, #link).



80
81
82
83
84
85
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 80

def bibliography_for(context_name)
  path = @context_paths[context_name]
  return nil unless path

  bibliography_at(path)
end

#concepts_at(path) ⇒ Object

Returns concepts cached at an absolute path. Used by Liquid blocks that receive a pre-resolved absolute path.



89
90
91
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 89

def concepts_at(path)
  store_for(path).concepts
end

#context_path(key) ⇒ Object



59
60
61
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 59

def context_path(key)
  @context_paths[key]
end

#find_concept(dataset_name, concept_name, document = nil) ⇒ Object



52
53
54
55
56
57
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 52

def find_concept(dataset_name, concept_name, document = nil)
  dataset = resolve_dataset(document, dataset_name)
  return unless dataset

  dataset.find { |concept| concept.default_designation == concept_name }
end

#non_verbal_collection(context_name, kind) ⇒ Object

Returns the typed NonVerbalCollection for a registered context (e.g. FigureCollection), or nil if the dataset has no such subdirectory. kind is one of the keys of NON_VERBAL_KINDS.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 96

def non_verbal_collection(context_name, kind)
  unless NON_VERBAL_KINDS.key?(kind)
    raise ArgumentError, "unknown non-verbal kind: #{kind.inspect}"
  end

  path = @context_paths[context_name]
  return nil unless path

  collection_class, subdir = NON_VERBAL_KINDS.fetch(kind)
  non_verbal_at(path, kind, subdir, collection_class)
end

#non_verbal_collections(context_name) ⇒ Object

Returns all available non-verbal collections for a context as a hash keyed by kind symbol (e.g. { figures: FigureCollection }). Kinds whose subdirectory doesn't exist are omitted. Convenient for building a NonVerbalRenderer in one call.



118
119
120
121
122
123
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 118

def non_verbal_collections(context_name)
  NON_VERBAL_KINDS.each_with_object({}) do |(kind, _), memo|
    collection = non_verbal_collection(context_name, kind)
    memo[kind] = collection if collection
  end
end

#register(document, contexts) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 34

def register(document, contexts)
  contexts.split(";").map do |context|
    context_name, file_path = context.split(":", 2).map(&:strip)
    path = relative_file_path(document, file_path)
    @context_paths[context_name] = path
    "#{context_name}=#{path}"
  end
end

#register_for(context_name) ⇒ Object

Returns the DatasetRegister for a registered context, or nil. The DatasetRegister is the single source of truth for section hierarchy and concept→section membership (cascading ancestors).



66
67
68
69
70
71
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 66

def register_for(context_name)
  path = @context_paths[context_name]
  return nil unless path

  register_at(path)
end

#register_sections(context_name) ⇒ Object



73
74
75
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 73

def register_sections(context_name)
  register_for(context_name)&.sections
end

#resolve_dataset(document, dataset_name) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 43

def resolve_dataset(document, dataset_name)
  return concepts_for(dataset_name) if @context_paths.key?(dataset_name)

  path = relative_file_path(document, dataset_name) if document
  return unless path

  concepts_at(path)
end