Class: Glossarist::ConceptManager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ ConceptManager

Returns a new instance of ConceptManager.

Parameters:

  • path (String) (defaults to: nil)

    concepts directory path, either absolute or relative to CWD



12
13
14
# File 'lib/glossarist/concept_manager.rb', line 12

def initialize(path: nil)
  @path = path
end

Instance Attribute Details

#localized_concepts_pathObject

Returns the value of attribute localized_concepts_path.



8
9
10
# File 'lib/glossarist/concept_manager.rb', line 8

def localized_concepts_path
  @localized_concepts_path
end

#pathString

Path to concepts directory.

Returns:

  • (String)


7
8
9
# File 'lib/glossarist/concept_manager.rb', line 7

def path
  @path
end

Instance Method Details

#load_concept_from_file(filename) ⇒ Object



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

def load_concept_from_file(filename)
  concept_hash = Psych.safe_load(File.read(filename), permitted_classes: [Date, Time])
  concept_hash["uuid"] = concept_hash["id"] || File.basename(filename, ".*")

  concept = Config.class_for(:managed_concept).new(concept_hash)
  concept.localized_concepts.each do |_lang, id|
    localized_concept = load_localized_concept(id)
    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

Reads all concepts from files.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/glossarist/concept_manager.rb', line 17

def load_from_files(collection: nil)
  collection ||= ManagedConceptCollection.new

  Dir.glob(concepts_glob) do |filename|
    concept = if v1_collection?
                Glossarist::V1Reader.load_concept_from_file(filename)
              else
                load_concept_from_file(filename)
              end

    collection.store(concept)
  end
end

#load_localized_concept(id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/glossarist/concept_manager.rb', line 51

def load_localized_concept(id)
  concept_hash = Psych.safe_load(
    File.read(localized_concept_path(id)),
    permitted_classes: [Date, Time],
  )
  concept_hash["uuid"] = id

  Config.class_for(:localized_concept).new(concept_hash)
rescue Psych::SyntaxError => e
  raise Glossarist::ParseError.new(filename: filename, line: e.line)
end

#save_concept_to_file(concept) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/glossarist/concept_manager.rb', line 63

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, Psych.dump(concept.to_h))

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

#save_to_files(managed_concepts) ⇒ Object

Writes all concepts to files.



32
33
34
# File 'lib/glossarist/concept_manager.rb', line 32

def save_to_files(managed_concepts)
  managed_concepts.each_value &method(:save_concept_to_file)
end