Class: Glossarist::ManagedConceptCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/glossarist/managed_concept_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_key: nil) ⇒ ManagedConceptCollection

Returns a new instance of ManagedConceptCollection.



9
10
11
12
13
# File 'lib/glossarist/managed_concept_collection.rb', line 9

def initialize(file_key: nil)
  @managed_concepts = []
  @managed_concepts_ids = {}
  @file_key = file_key
end

Instance Attribute Details

#managed_conceptsObject

Returns the value of attribute managed_concepts.



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

def managed_concepts
  @managed_concepts
end

Instance Method Details

#by_id_and(id, version = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/glossarist/managed_concept_collection.rb', line 38

def by_id_and(id, version = nil)
  return fetch(id) if version.nil?

  @managed_concepts.find do |c|
    next false unless c.uuid == id || c.uuid == @managed_concepts_ids[id]

    c.version == version
  end
end

#eachObject



21
22
23
# File 'lib/glossarist/managed_concept_collection.rb', line 21

def each(&)
  @managed_concepts.each(&)
end

#fetch(id) ⇒ ManagedConcept? Also known as: []

Returns concept with given ID, if it is present in collection, or nil otherwise.

Parameters:

  • id (String)

    ManagedConcept ID

Returns:



31
32
33
34
35
# File 'lib/glossarist/managed_concept_collection.rb', line 31

def fetch(id)
  @managed_concepts.find do |c|
    c.uuid == id || c.uuid == @managed_concepts_ids[id]
  end
end

#fetch_or_initialize(id) ⇒ ManagedConcept

If ManagedConcept with given ID is present in this collection, then returns it. Otherwise, instantiates a new ManagedConcept, adds it to the collection, and returns it.

Parameters:

  • id (String)

    ManagedConcept ID

Returns:



55
56
57
# File 'lib/glossarist/managed_concept_collection.rb', line 55

def fetch_or_initialize(id)
  fetch(id) or store(Config.class_for(:managed_concept).of_yaml(data: { id: id }))
end

#load_from_files(path) ⇒ Object



76
77
78
79
80
81
# File 'lib/glossarist/managed_concept_collection.rb', line 76

def load_from_files(path)
  store = GlossaryStore.new
  store.load(path)
  store.concepts.each { |mc| store(mc) }
  @localized_concepts_path = store.localized_concepts_dir_name || "localized_concept"
end

#save_grouped_concepts_to_files(path) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/glossarist/managed_concept_collection.rb', line 103

def save_grouped_concepts_to_files(path)
  FileUtils.mkdir_p(path)

  @managed_concepts.each do |mc|
    parts = [mc.to_yaml]
    mc.localized_concepts.each_key do |lang|
      l10n = mc.localization(lang)
      parts << l10n.to_yaml if l10n
    end
    File.write(File.join(path, "#{file_key(mc)}.yaml"), parts.join("\n"),
               encoding: "utf-8")
  end
end

#save_to_files(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/glossarist/managed_concept_collection.rb', line 83

def save_to_files(path)
  concept_dir = File.join(path, "concept")
  lc_dir = File.join(path, @localized_concepts_path || "localized_concept")
  FileUtils.mkdir_p(concept_dir)
  FileUtils.mkdir_p(lc_dir)

  @managed_concepts.each do |mc|
    File.write(File.join(concept_dir, "#{file_key(mc)}.yaml"), mc.to_yaml,
               encoding: "utf-8")

    mc.localized_concepts.each do |lang, uuid|
      l10n = mc.localization(lang)
      next unless l10n

      File.write(File.join(lc_dir, "#{uuid}.yaml"), l10n.to_yaml,
                 encoding: "utf-8")
    end
  end
end

#store(managed_concept) ⇒ Object Also known as: <<

Adds concept to the collection. If collection contains a concept with the same ID already, that concept is replaced.

Parameters:

  • managed_concept (ManagedConcept)

    ManagedConcept about to be added



64
65
66
67
68
69
70
71
72
73
# File 'lib/glossarist/managed_concept_collection.rb', line 64

def store(managed_concept)
  @managed_concepts ||= []
  @managed_concepts << managed_concept
  if managed_concept.data.id
    @managed_concepts_ids[managed_concept.data.id] =
      managed_concept.uuid
  end

  managed_concept
end

#to_hObject



15
16
17
18
19
# File 'lib/glossarist/managed_concept_collection.rb', line 15

def to_h
  {
    "managed_concepts" => managed_concepts.map(&:to_yaml_hash),
  }.compact
end