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

#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 Attribute Details

#managed_conceptsObject

Returns the value of attribute managed_concepts.



5
6
7
# File 'lib/glossarist/managed_concept_collection.rb', line 5

def managed_concepts
  @managed_concepts
end

Instance Method Details

#each(&block) ⇒ Object



19
20
21
# File 'lib/glossarist/managed_concept_collection.rb', line 19

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



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

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:



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

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



64
65
66
67
# File 'lib/glossarist/managed_concept_collection.rb', line 64

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

#save_grouped_concepts_to_files(path) ⇒ Object



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

def save_grouped_concepts_to_files(path)
  @concept_manager.path = path
  @concept_manager.save_grouped_concepts_to_files(@managed_concepts)
end

#save_to_files(path) ⇒ Object



69
70
71
72
# File 'lib/glossarist/managed_concept_collection.rb', line 69

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



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

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



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

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