Class: Glossarist::ConceptManager
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Glossarist::ConceptManager
- Defined in:
- lib/glossarist/concept_manager.rb
Instance Method Summary collapse
- #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_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
#concepts_glob ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/glossarist/concept_manager.rb', line 114 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/glossarist/concept_manager.rb', line 36 def load_concept_from_file(filename) # rubocop:disable Metrics/CyclomaticComplexity raw = File.read(filename, encoding: "utf-8") doc = ConceptDocument.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.instance_variable_set(:@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
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/glossarist/concept_manager.rb', line 12 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 end end |
#load_localized_concept(id, inline_localizations = nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/glossarist/concept_manager.rb', line 59 def load_localized_concept(id, inline_localizations = nil) if inline_localizations l10n = inline_localizations.find { |l| l.id == id } if l10n l10n.instance_variable_set(:@uuid, id) return l10n end end l10n = LocalizedConcept.from_yaml( File.read(localized_concept_path(id), encoding: "utf-8"), ) l10n.instance_variable_set(:@uuid, id) l10n rescue Psych::SyntaxError => e raise Glossarist::ParseError.new(filename: filename, line: e.line) end |
#localized_concept_path(id) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/glossarist/concept_manager.rb', line 130 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/glossarist/concept_manager.rb', line 77 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/glossarist/concept_manager.rb', line 96 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
30 31 32 33 34 |
# File 'lib/glossarist/concept_manager.rb', line 30 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
24 25 26 27 28 |
# File 'lib/glossarist/concept_manager.rb', line 24 def save_to_files(managed_concepts) managed_concepts.each do |concept| save_concept_to_file(concept) end end |
#v1_collection? ⇒ Boolean
155 156 157 158 |
# File 'lib/glossarist/concept_manager.rb', line 155 def v1_collection? @v1_collection ||= !Dir.glob(File.join(path, "concept-*.{yaml,yml}")).empty? end |