Class: Glossarist::ManagedConceptCollection
- Inherits:
-
Object
- Object
- Glossarist::ManagedConceptCollection
- Includes:
- Enumerable
- Defined in:
- lib/glossarist/managed_concept_collection.rb
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#fetch(id) ⇒ ManagedConcept?
(also: #[])
Returns concept with given ID, if it is present in collection, or
nil
otherwise. -
#fetch_or_initialize(id) ⇒ ManagedConcept
If ManagedConcept with given ID is present in this collection, then returns it.
-
#initialize ⇒ ManagedConceptCollection
constructor
A new instance of ManagedConceptCollection.
- #load_from_files(path) ⇒ Object
- #managed_concepts ⇒ Array<ManagedConcept>
- #managed_concepts=(managed_concepts = []) ⇒ Object
- #save_to_files(path) ⇒ Object
-
#store(managed_concept) ⇒ Object
(also: #<<)
Adds concept to the collection.
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ ManagedConceptCollection
Returns a new instance of ManagedConceptCollection.
7 8 9 10 |
# File 'lib/glossarist/managed_concept_collection.rb', line 7 def initialize @managed_concepts = {} @concept_manager = ConceptManager.new end |
Instance Method Details
#each(&block) ⇒ Object
31 32 33 |
# File 'lib/glossarist/managed_concept_collection.rb', line 31 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.
41 42 43 |
# File 'lib/glossarist/managed_concept_collection.rb', line 41 def fetch(id) @managed_concepts[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.
54 55 56 |
# File 'lib/glossarist/managed_concept_collection.rb', line 54 def fetch_or_initialize(id) fetch(id) or store(ManagedConcept.new(id: id)) end |
#load_from_files(path) ⇒ Object
69 70 71 72 |
# File 'lib/glossarist/managed_concept_collection.rb', line 69 def load_from_files(path) @concept_manager.path = path @concept_manager.load_from_files(collection: self) end |
#managed_concepts ⇒ Array<ManagedConcept>
13 14 15 |
# File 'lib/glossarist/managed_concept_collection.rb', line 13 def managed_concepts @managed_concepts.values end |
#managed_concepts=(managed_concepts = []) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/glossarist/managed_concept_collection.rb', line 17 def managed_concepts=(managed_concepts = []) managed_concepts.each do |managed_concept| store(ManagedConcept.new(managed_concept)) end @managed_concepts.values end |
#save_to_files(path) ⇒ Object
74 75 76 77 |
# File 'lib/glossarist/managed_concept_collection.rb', line 74 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.
63 64 65 |
# File 'lib/glossarist/managed_concept_collection.rb', line 63 def store(managed_concept) @managed_concepts[managed_concept.id] = managed_concept end |
#to_h ⇒ Object
25 26 27 28 29 |
# File 'lib/glossarist/managed_concept_collection.rb', line 25 def to_h { "managed_concepts" => managed_concepts.map(&:to_h), }.compact end |