Class: Glossarist::ConceptManager

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/glossarist/concept_manager.rb

Instance Method Summary collapse

Instance Method Details

#concepts_globObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/glossarist/concept_manager.rb', line 131

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

#group_concept_hashes(mixed_hashes) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glossarist/concept_manager.rb', line 35

def group_concept_hashes(mixed_hashes)
  concept_hashes = mixed_hashes.select do |concept_hash|
    !concept_hash["data"]["localized_concepts"].nil? ||
      !concept_hash["data"]["localizedConcepts"].nil?
  end

  localized_concept_hashes = mixed_hashes.select do |concept_hash|
    concept_hash["data"]["localized_concepts"].nil? &&
      concept_hash["data"]["localizedConcepts"].nil?
  end

  [concept_hashes, localized_concept_hashes]
end

#load_concept_from_file(filename) ⇒ Object



49
50
51
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 49

def load_concept_from_file(filename)
  mixed_hashes = YAML.load_stream(File.read(filename))
  concepts = []

  concept_hashes, localized_concept_hashes =
    group_concept_hashes(mixed_hashes)

  concept_hashes.each do |concept_hash|
    concept_hash["uuid"] = concept_hash["id"] ||
      File.basename(filename, ".*")
    concept = Config.class_for(:managed_concept).of_yaml(concept_hash)

    concept.data.localized_concepts.each_value do |id|
      localized_concept =
        load_localized_concept(id, localized_concept_hashes)
      concept.add_l10n(localized_concept)
    end

    concepts << concept
  end

  concepts
rescue Psych::SyntaxError => e
  raise Glossarist::ParseError.new(filename: filename, line: e.line)
end

#load_from_files(collection: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/glossarist/concept_manager.rb', line 11

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, localized_concept_hashes = []) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/glossarist/concept_manager.rb', line 75

def load_localized_concept(id, localized_concept_hashes = [])
  {}

  concept_hash = if localized_concept_hashes.empty?
                   Psych.safe_load(
                     File.read(localized_concept_path(id)),
                     permitted_classes: [Date, Time],
                   )
                 else
                   localized_concept_hashes.find do |hash|
                     hash["id"] == id
                   end
                 end

  concept_hash["uuid"] = id
  Config.class_for(:localized_concept).of_yaml(concept_hash)
rescue Psych::SyntaxError => e
  raise Glossarist::ParseError.new(filename: filename, line: e.line)
end

#localized_concept_path(id) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/glossarist/concept_manager.rb', line 147

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/glossarist/concept_manager.rb', line 95

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)

  Dir.mkdir(concept_dir) unless Dir.exist?(concept_dir)
  Dir.mkdir(localized_concept_dir) unless Dir.exist?(localized_concept_dir)

  filename = File.join(concept_dir, "#{concept.uuid}.yaml")
  File.write(filename, concept.to_yaml)

  concept.localized_concepts.each do |lang, uuid|
    filename = File.join(localized_concept_dir, "#{uuid}.yaml")
    File.write(filename, concept.localization(lang).to_yaml)
  end
end

#save_grouped_concepts_to_file(concept) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/glossarist/concept_manager.rb', line 113

def save_grouped_concepts_to_file(concept)
  @localized_concepts_path ||= "localized_concept"
  concept_dir = File.join(path)

  Dir.mkdir(concept_dir) unless Dir.exist?(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"))
end

#save_grouped_concepts_to_files(managed_concepts) ⇒ Object



29
30
31
32
33
# File 'lib/glossarist/concept_manager.rb', line 29

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



23
24
25
26
27
# File 'lib/glossarist/concept_manager.rb', line 23

def save_to_files(managed_concepts)
  managed_concepts.each do |concept|
    save_concept_to_file(concept)
  end
end

#v1_collection?Boolean

Returns:

  • (Boolean)


172
173
174
175
# File 'lib/glossarist/concept_manager.rb', line 172

def v1_collection?
  @v1_collection ||= !Dir.glob(File.join(path,
                                         "concept-*.{yaml,yml}")).empty?
end