Class: Metanorma::Mirror::IdStrategy::Positional

Inherits:
Base
  • Object
show all
Defined in:
lib/metanorma/mirror/id_strategy/positional.rb

Overview

Assign positional IDs (sec-X.Y.Z, table-N, fig-N, anx-X) to elements that have UUID IDs. Elements with author-assigned explicit IDs are preserved. Cross-reference targets are translated to use the new IDs.

Positional IDs are derived from the element's resolved section number (the "number" attribute on the presentation XML element), which represents the document's structural position.

Example:

UUID element with number "5.4" → id: "sec-5.4"
UUID element with number "A.2"  → id: "anx-A.2"
Explicit id "sec-3.1.3.4"       → id: "sec-3.1.3.4" (unchanged)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Positional

Returns a new instance of Positional.



50
51
52
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 50

def initialize
  @id_map = {}
end

Class Method Details

.categories ⇒ Object

Registry of Metanorma model classes → positional-ID category (:section, :annex, :figure, :table). Adding new flavors or new model types requires only a new register_category call — no edits to derive/element_category (OCP).



26
27
28
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 26

def categories
  @categories
end

.category_for(element) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 40

def category_for(element)
  return categories[element.class] if categories.key?(element.class)

  categories.each do |klass, category|
    return category if element.is_a?(klass)
  end
  nil
end

.register_category(model_class, category) ⇒ Object



30
31
32
33
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 30

def register_category(model_class, category)
  categories[model_class] = category
  self
end

.unregister_category(model_class) ⇒ Object



35
36
37
38
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 35

def unregister_category(model_class)
  categories.delete(model_class)
  self
end

Instance Method Details

#assign_id(element) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 54

def assign_id(element)
  raw = raw_id(element)
  return raw unless uuid?(raw)

  positional = derive(element)
  return raw unless positional

  @id_map[raw] = positional
  positional
end

#finalize!(document) ⇒ Object



65
66
67
68
# File 'lib/metanorma/mirror/id_strategy/positional.rb', line 65

def finalize!(document)
  translate_targets(document)
  document
end