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

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

Instance Method Summary collapse

Constructor Details

#initializeDatasetRegistry

Returns a new instance of DatasetRegistry.



9
10
11
12
13
14
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 9

def initialize
  @datasets = {}
  @path_cache = {}
  @bibliography_data = {}
  @context_names = []
end

Instance Method Details

#bibliography_dataObject



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

def bibliography_data
  @bibliography_data
end

#context_path(key) ⇒ Object



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

def context_path(key)
  return nil if @context_names.empty?

  found = @context_names.find do |context|
    context_name, = context.split("=")
    context_name.strip == key
  end
  found&.split("=")&.last&.strip
end

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



40
41
42
43
44
45
46
47
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 40

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

  dataset.find do |concept|
    concept.default_designation == concept_name
  end
end

#load_cached(path) ⇒ Object



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

def load_cached(path)
  @path_cache[path] ||= load_dataset(path)
end

#register(document, contexts) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 16

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

#resolve_dataset(document, dataset_name) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/metanorma/plugin/glossarist/dataset_registry.rb', line 30

def resolve_dataset(document, dataset_name)
  dataset = @datasets[dataset_name]
  return dataset if dataset

  return unless document

  path = relative_file_path(document, dataset_name)
  @datasets[dataset_name] = load_dataset(path).to_a
end