Class: Glossarist::ManagedConceptCollection

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

Instance Method Summary collapse

Constructor Details

#initializeManagedConceptCollection

Returns a new instance of ManagedConceptCollection.



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

def initialize
  @managed_concepts = {}
  @managed_concepts_ids = {}
  @concept_manager = ConceptManager.new
end

Instance Method Details

#each(&block) ⇒ Object



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

def each(&block)
  @managed_concepts.each_value(&block)
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:



42
43
44
# File 'lib/glossarist/managed_concept_collection.rb', line 42

def fetch(id)
  @managed_concepts[id] || @managed_concepts[@managed_concepts_ids[id]]
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).new(data: { id: id }))
end

#load_from_files(path) ⇒ Object



73
74
75
76
# File 'lib/glossarist/managed_concept_collection.rb', line 73

def load_from_files(path)
  @concept_manager.path = path
  @concept_manager.load_from_files(collection: self)
end

#managed_conceptsArray<ManagedConcept>

Returns:



14
15
16
# File 'lib/glossarist/managed_concept_collection.rb', line 14

def managed_concepts
  @managed_concepts.values
end

#managed_concepts=(managed_concepts = []) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/glossarist/managed_concept_collection.rb', line 18

def managed_concepts=(managed_concepts = [])
  managed_concepts.each do |managed_concept|
    store(Config.class_for(:managed_concept).new(managed_concept))
  end

  @managed_concepts.values
end

#save_to_files(path) ⇒ Object



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

def save_to_files(path)
  @concept_manager.path = path
  @concept_manager.save_to_files(@managed_concepts)
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
# File 'lib/glossarist/managed_concept_collection.rb', line 64

def store(managed_concept)
  @managed_concepts[managed_concept.uuid] = managed_concept
  @managed_concepts_ids[managed_concept.id] = managed_concept.uuid if managed_concept.id

  managed_concept
end

#to_hObject



26
27
28
29
30
# File 'lib/glossarist/managed_concept_collection.rb', line 26

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