Class: Glossarist::GlossaryStore
- Inherits:
-
Object
- Object
- Glossarist::GlossaryStore
- Defined in:
- lib/glossarist/glossary_store.rb
Instance Attribute Summary collapse
-
#localized_concepts_dir_name ⇒ Object
readonly
Returns the value of attribute localized_concepts_dir_name.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
Instance Method Summary collapse
- #add_concept(managed_concept) ⇒ Object
- #add_image(path, content) ⇒ Object
-
#add_relation(hyperedge) ⇒ Object
Add a hyperedge to the in-memory list AND persist it immediately to its per-file location under <dataset_path>/relations/.
-
#bibliography ⇒ Object
── Bibliography ──.
- #bibliography=(value) ⇒ Object
-
#build_metadata(shortname:, version:, **opts) ⇒ Object
── Convenience ──.
- #concept(uuid) ⇒ Object
- #concept_count ⇒ Object
- #concept_exists?(uuid) ⇒ Boolean
-
#concepts ⇒ Object
── Concepts ──.
- #each_concept(&block) ⇒ Object
-
#figures ⇒ Object
── Dataset-level non-verbal entities ──.
- #formulas ⇒ Object
-
#image(path) ⇒ Object
── Images ──.
- #image_paths ⇒ Object
-
#initialize ⇒ GlossaryStore
constructor
A new instance of GlossaryStore.
- #load(path, format: nil) ⇒ Object
-
#load_directory(path, format: nil) ⇒ Object
── Load ──.
- #load_zip(path, format: nil) ⇒ Object
-
#metadata ⇒ Object
── Metadata ──.
- #metadata=(value) ⇒ Object
-
#register_data ⇒ Object
── Register Data ──.
- #register_data=(value) ⇒ Object
-
#relations ⇒ Object
Per-file n-ary relations (PartitiveHyperedge, GenericHyperedge) discovered under
relations/<comprehensive-id>/<criterion-slug>.yaml. -
#relations_for(qualified_id_or_ref) ⇒ Object
Relations whose comprehensive concept matches
qualified_id(e.g. "VIM:112-02-09"). - #remove_concept(uuid) ⇒ Object
-
#remove_relations_if(&predicate) ⇒ Object
Remove a hyperedge.
-
#save_directory(path, format: nil, formats: {}) ⇒ Object
── Save ──.
-
#save_relations(dir) ⇒ Object
Persist all loaded relations to the per-file store under
dir. - #save_zip(path, format: nil, formats: {}) ⇒ Object
-
#stats ⇒ Object
── Stats ──.
- #tables ⇒ Object
Constructor Details
#initialize ⇒ GlossaryStore
Returns a new instance of GlossaryStore.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/glossarist/glossary_store.rb', line 7 def initialize @package = nil @concept_document_class = V3::ConceptDocument @v1_concepts = nil @localized_concepts_dir_name = nil @dataset_path = nil @figures = nil @tables = nil @formulas = nil end |
Instance Attribute Details
#localized_concepts_dir_name ⇒ Object (readonly)
Returns the value of attribute localized_concepts_dir_name.
5 6 7 |
# File 'lib/glossarist/glossary_store.rb', line 5 def localized_concepts_dir_name @localized_concepts_dir_name end |
#package ⇒ Object (readonly)
Returns the value of attribute package.
5 6 7 |
# File 'lib/glossarist/glossary_store.rb', line 5 def package @package end |
Instance Method Details
#add_concept(managed_concept) ⇒ Object
145 146 147 148 149 150 |
# File 'lib/glossarist/glossary_store.rb', line 145 def add_concept(managed_concept) ensure_package doc = @concept_document_class.from_managed_concept(managed_concept) doc.id = managed_concept.uuid @package.add_model(doc) end |
#add_image(path, content) ⇒ Object
207 208 209 210 |
# File 'lib/glossarist/glossary_store.rb', line 207 def add_image(path, content) ensure_package @package.add_asset(path, content) end |
#add_relation(hyperedge) ⇒ Object
Add a hyperedge to the in-memory list AND persist it immediately to its per-file location under <dataset_path>/relations/. Returns the written file path (or nil if dataset has no path).
105 106 107 108 109 110 111 |
# File 'lib/glossarist/glossary_store.rb', line 105 def add_relation(hyperedge) (@relations ||= []) << hyperedge return nil unless @dataset_path relations_dir = File.join(@dataset_path, "relations") V3::HyperedgeWriter.write(hyperedge, relations_dir) end |
#bibliography ⇒ Object
── Bibliography ──
190 191 192 |
# File 'lib/glossarist/glossary_store.rb', line 190 def bibliography @package.models_for(BibliographyData).first end |
#bibliography=(value) ⇒ Object
194 195 196 197 198 199 |
# File 'lib/glossarist/glossary_store.rb', line 194 def bibliography=(value) ensure_package existing = bibliography @package.remove_model(BibliographyData, existing.shortname) if existing @package.add_model(value) end |
#build_metadata(shortname:, version:, **opts) ⇒ Object
── Convenience ──
270 271 272 273 274 275 276 |
# File 'lib/glossarist/glossary_store.rb', line 270 def (shortname:, version:, **opts) GcrMetadata.from_concepts(concepts, register_data: register_data, options: { shortname: shortname, version: version, **opts, }) end |
#concept(uuid) ⇒ Object
140 141 142 143 |
# File 'lib/glossarist/glossary_store.rb', line 140 def concept(uuid) doc = @package.fetch_model(@concept_document_class, uuid) doc&.to_managed_concept end |
#concept_count ⇒ Object
156 157 158 |
# File 'lib/glossarist/glossary_store.rb', line 156 def concept_count @package.model_count(@concept_document_class) end |
#concept_exists?(uuid) ⇒ Boolean
160 161 162 |
# File 'lib/glossarist/glossary_store.rb', line 160 def concept_exists?(uuid) @package.model_exists?(@concept_document_class, uuid) end |
#concepts ⇒ Object
── Concepts ──
122 123 124 125 126 |
# File 'lib/glossarist/glossary_store.rb', line 122 def concepts return @v1_concepts if @v1_concepts @package.models_for(@concept_document_class).map(&:to_managed_concept) end |
#each_concept(&block) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/glossarist/glossary_store.rb', line 128 def each_concept(&block) return enum_for(:each_concept) unless block if @v1_concepts @v1_concepts.each(&block) else @package.models_for(@concept_document_class).each do |doc| yield doc.to_managed_concept end end end |
#figures ⇒ Object
── Dataset-level non-verbal entities ──
Loaded lazily from the dataset's figures/, tables/, formulas/ subdirectories. Each YAML file is parsed as the corresponding model (Figure, Table, Formula). The entities are referenced from ManagedConceptData via FigureReference/TableReference/FormulaReference but live as standalone shared entities at the dataset level. Wired into ConceptToGlossTransform via the figures:/tables:/formulas: kwargs on transform_document.
226 227 228 |
# File 'lib/glossarist/glossary_store.rb', line 226 def figures @figures ||= load_dataset_entities("figures", Figure) end |
#formulas ⇒ Object
234 235 236 |
# File 'lib/glossarist/glossary_store.rb', line 234 def formulas @formulas ||= load_dataset_entities("formulas", Formula) end |
#image(path) ⇒ Object
── Images ──
203 204 205 |
# File 'lib/glossarist/glossary_store.rb', line 203 def image(path) @package.asset(path) end |
#image_paths ⇒ Object
212 213 214 |
# File 'lib/glossarist/glossary_store.rb', line 212 def image_paths @package.asset_paths.select { |p| p.start_with?("images/") } end |
#load(path, format: nil) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/glossarist/glossary_store.rb', line 67 def load(path, format: nil) ext = File.extname(path).downcase if [".gcr", ".zip"].include?(ext) load_zip(path, format: format) else load_directory(path, format: format) end end |
#load_directory(path, format: nil) ⇒ Object
── Load ──
20 21 22 23 24 25 26 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/glossarist/glossary_store.rb', line 20 def load_directory(path, format: nil) @dataset_path = File.(path) if v1_dataset?(path) load_v1_fallback(path) return self end if legacy_managed_layout?(path) load_legacy_managed(path) return self end if grouped_at_root?(path) load_grouped_at_root(path) return self end = (path) @concept_document_class = resolve_concept_document_class() definition = GcrPackageDefinition.definition( concept_document_class: @concept_document_class, ) @package = Lutaml::Store::PackageStore.load( definition, path, transport: :directory, format: format ) () self end |
#load_zip(path, format: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/glossarist/glossary_store.rb', line 51 def load_zip(path, format: nil) @dataset_path = File.(path) = (path) @concept_document_class = resolve_concept_document_class() definition = GcrPackageDefinition.definition( concept_document_class: @concept_document_class, ) @package = Lutaml::Store::PackageStore.load( definition, path, transport: :zip, format: format ) () self end |
#metadata ⇒ Object
── Metadata ──
166 167 168 |
# File 'lib/glossarist/glossary_store.rb', line 166 def @package&. end |
#metadata=(value) ⇒ Object
170 171 172 173 |
# File 'lib/glossarist/glossary_store.rb', line 170 def (value) ensure_package @package. = value end |
#register_data ⇒ Object
── Register Data ──
177 178 179 |
# File 'lib/glossarist/glossary_store.rb', line 177 def register_data @package.models_for(RegisterData).first end |
#register_data=(value) ⇒ Object
181 182 183 184 185 186 |
# File 'lib/glossarist/glossary_store.rb', line 181 def register_data=(value) ensure_package existing = register_data @package.remove_model(RegisterData, existing.key) if existing @package.add_model(value) end |
#relations ⇒ Object
Per-file n-ary relations (PartitiveHyperedge, GenericHyperedge)
discovered under relations/<comprehensive-id>/<criterion-slug>.yaml.
Returns a flat Array
244 245 246 |
# File 'lib/glossarist/glossary_store.rb', line 244 def relations @relations ||= load_relations end |
#relations_for(qualified_id_or_ref) ⇒ Object
Relations whose comprehensive concept matches qualified_id
(e.g. "VIM:112-02-09"). Accepts either a qualified-id string or
a ConceptRef — the lookup goes through ConceptRef.qualified_id.
251 252 253 254 255 256 257 258 259 260 |
# File 'lib/glossarist/glossary_store.rb', line 251 def relations_for(qualified_id_or_ref) target = if qualified_id_or_ref.is_a?(Glossarist::ConceptRef) Glossarist::ConceptRef.qualified_id(qualified_id_or_ref) else qualified_id_or_ref.to_s end relations.select do |rel| Glossarist::ConceptRef.qualified_id(rel.comprehensive) == target end end |
#remove_concept(uuid) ⇒ Object
152 153 154 |
# File 'lib/glossarist/glossary_store.rb', line 152 def remove_concept(uuid) @package.remove_model(@concept_document_class, uuid) end |
#remove_relations_if(&predicate) ⇒ Object
Remove a hyperedge. Caller-supplied predicate selects which to remove. The per-file storage is NOT modified by this method — use #delete_relation_file to remove a single file.
116 117 118 |
# File 'lib/glossarist/glossary_store.rb', line 116 def remove_relations_if(&predicate) @relations&.reject!(&predicate) end |
#save_directory(path, format: nil, formats: {}) ⇒ Object
── Save ──
78 79 80 81 82 83 84 |
# File 'lib/glossarist/glossary_store.rb', line 78 def save_directory(path, format: nil, formats: {}) @package.save(path, transport: :directory, format: format, formats: formats) # Persist per-file hyperedges alongside concepts. The relations/ # subdirectory mirrors the load path (relations/<comp-id>/<slug>.yaml). save_relations(File.join(path, "relations")) if @relations && !@relations.empty? end |
#save_relations(dir) ⇒ Object
Persist all loaded relations to the per-file store under dir.
Each hyperedge becomes relations/
96 97 98 99 100 |
# File 'lib/glossarist/glossary_store.rb', line 96 def save_relations(dir) return if @relations.nil? || @relations.empty? V3::HyperedgeWriter.write_all(@relations, dir) end |
#save_zip(path, format: nil, formats: {}) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/glossarist/glossary_store.rb', line 86 def save_zip(path, format: nil, formats: {}) @package.save(path, transport: :zip, format: format, formats: formats) # ZIP transport doesn't expose a writable filesystem path here — # callers persisting to ZIP must use #save_relations separately # against a staging dir before zipping. Documented limitation. end |
#stats ⇒ Object
── Stats ──
264 265 266 |
# File 'lib/glossarist/glossary_store.rb', line 264 def stats @package&.stats end |
#tables ⇒ Object
230 231 232 |
# File 'lib/glossarist/glossary_store.rb', line 230 def tables @tables ||= load_dataset_entities("tables", Table) end |