Class: Glossarist::ConceptManager
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Glossarist::ConceptManager
- Defined in:
- lib/glossarist/concept_manager.rb
Instance Method Summary collapse
- #concept_document_class ⇒ Object
- #concepts_glob ⇒ Object
-
#load_concept_from_file(filename) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity.
- #load_from_files(collection: nil) ⇒ Object
- #load_localized_concept(id, inline_localizations = nil) ⇒ Object
- #localized_concept_class ⇒ Object
- #localized_concept_path(id) ⇒ Object
- #save_concept_to_file(concept) ⇒ Object
- #save_grouped_concepts_to_file(concept) ⇒ Object
- #save_grouped_concepts_to_files(managed_concepts) ⇒ Object
- #save_to_files(managed_concepts) ⇒ Object
- #v1_collection? ⇒ Boolean
Instance Method Details
#concept_document_class ⇒ Object
13 14 15 |
# File 'lib/glossarist/concept_manager.rb', line 13 def concept_document_class ConceptDocument.for_version(version) end |
#concepts_glob ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/glossarist/concept_manager.rb', line 130 def concepts_glob return path if File.file?(path) if v1_collection? File.join(path, "concept-*.{yaml,yml}") else # normal v2 collection concepts_glob = File.join(path, "concept", "*.{yaml,yml}") if Dir.glob(concepts_glob).empty? # multiple content YAML files concepts_glob = File.join(path, "*.{yaml,yml}") end concepts_glob end end |
#load_concept_from_file(filename) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/glossarist/concept_manager.rb', line 52 def load_concept_from_file(filename) # rubocop:disable Metrics/CyclomaticComplexity raw = File.read(filename, encoding: "utf-8") doc = concept_document_class.from_yamls(raw) concept = doc.concept unless concept raise Glossarist::ParseError.new(filename: filename) end concept_uuid = concept.identifier || concept.data&.id || File.basename( filename, ".*" ) concept.uuid = concept_uuid concept.data.localized_concepts.each_value do |id| localized_concept = load_localized_concept(id, doc.localizations) concept.add_l10n(localized_concept) end [concept] rescue Psych::SyntaxError => e raise Glossarist::ParseError.new(filename: filename, line: e.line) end |
#load_from_files(collection: nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/glossarist/concept_manager.rb', line 26 def load_from_files(collection: nil) collection ||= ManagedConceptCollection.new Dir.glob(concepts_glob) do |filename| concepts = load_concept_from_file(filename) concepts.each do |concept| collection.store(concept) end rescue StandardError next end end |
#load_localized_concept(id, inline_localizations = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/glossarist/concept_manager.rb', line 75 def load_localized_concept(id, inline_localizations = nil) if inline_localizations l10n = inline_localizations.find { |l| l.id == id } if l10n l10n.uuid = id return l10n end end l10n = localized_concept_class.from_yaml( File.read(localized_concept_path(id), encoding: "utf-8"), ) l10n.uuid = id l10n rescue Psych::SyntaxError => e raise Glossarist::ParseError.new(filename: filename, line: e.line) end |
#localized_concept_class ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/glossarist/concept_manager.rb', line 17 def localized_concept_class if version.to_s == "2" require_relative "v2" V2::LocalizedConcept else LocalizedConcept end end |
#localized_concept_path(id) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/glossarist/concept_manager.rb', line 146 def localized_concept_path(id) localized_concept_possible_dir = { "localized_concept" => File.join( path, "localized_concept", "#{id}.{yaml,yml}", ), "localized-concept" => File.join( path, "localized-concept", "#{id}.{yaml,yml}", ), } localized_concept_possible_dir.each do |dir_name, file_path| actual_path = Dir.glob(file_path)&.first if actual_path @localized_concepts_path = dir_name return actual_path end end end |
#save_concept_to_file(concept) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/glossarist/concept_manager.rb', line 93 def save_concept_to_file(concept) @localized_concepts_path ||= "localized_concept" concept_dir = File.join(path, "concept") localized_concept_dir = File.join(path, @localized_concepts_path) FileUtils.mkdir_p(concept_dir) FileUtils.mkdir_p(localized_concept_dir) filename = File.join(concept_dir, "#{concept.uuid}.yaml") File.write(filename, concept.to_yaml, encoding: "utf-8") concept.localized_concepts.each do |lang, uuid| filename = File.join(localized_concept_dir, "#{uuid}.yaml") File.write(filename, concept.localization(lang).to_yaml, encoding: "utf-8") end end |
#save_grouped_concepts_to_file(concept) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/glossarist/concept_manager.rb', line 112 def save_grouped_concepts_to_file(concept) @localized_concepts_path ||= "localized_concept" concept_dir = File.join(path) FileUtils.mkdir_p(concept_dir) content = [] filename = File.join(concept_dir, "#{concept.uuid}.yaml") content << concept.to_yaml concept.localized_concepts.each_key do |lang| content << concept.localization(lang).to_yaml end File.write(filename, content.join("\n"), encoding: "utf-8") end |
#save_grouped_concepts_to_files(managed_concepts) ⇒ Object
46 47 48 49 50 |
# File 'lib/glossarist/concept_manager.rb', line 46 def save_grouped_concepts_to_files(managed_concepts) managed_concepts.each do |concept| save_grouped_concepts_to_file(concept) end end |
#save_to_files(managed_concepts) ⇒ Object
40 41 42 43 44 |
# File 'lib/glossarist/concept_manager.rb', line 40 def save_to_files(managed_concepts) managed_concepts.each do |concept| save_concept_to_file(concept) end end |
#v1_collection? ⇒ Boolean
171 172 173 174 |
# File 'lib/glossarist/concept_manager.rb', line 171 def v1_collection? @v1_collection ||= !Dir.glob(File.join(path, "concept-*.{yaml,yml}")).empty? end |