Class: Glossarist::ConceptManager
- Inherits:
-
Object
- Object
- Glossarist::ConceptManager
- Defined in:
- lib/glossarist/concept_manager.rb
Instance Attribute Summary collapse
-
#path ⇒ String
Path to concepts directory.
Instance Method Summary collapse
-
#initialize(path: nil) ⇒ ConceptManager
constructor
A new instance of ConceptManager.
- #load_concept_from_file(filename) ⇒ Object
-
#load_from_files(collection: nil) ⇒ Object
Reads all concepts from files.
- #save_concept_to_file(concept) ⇒ Object
-
#save_to_files(managed_concepts) ⇒ Object
Writes all concepts to files.
Constructor Details
#initialize(path: nil) ⇒ ConceptManager
Returns a new instance of ConceptManager.
11 12 13 |
# File 'lib/glossarist/concept_manager.rb', line 11 def initialize(path: nil) @path = path end |
Instance Attribute Details
#path ⇒ String
Path to concepts directory.
7 8 9 |
# File 'lib/glossarist/concept_manager.rb', line 7 def path @path end |
Instance Method Details
#load_concept_from_file(filename) ⇒ Object
29 30 31 |
# File 'lib/glossarist/concept_manager.rb', line 29 def load_concept_from_file(filename) ManagedConcept.new(Psych.safe_load(File.read(filename))) end |
#load_from_files(collection: nil) ⇒ Object
Reads all concepts from files.
16 17 18 19 20 21 22 |
# File 'lib/glossarist/concept_manager.rb', line 16 def load_from_files(collection: nil) collection ||= ManagedConceptCollection.new Dir.glob(concepts_glob) do |filename| collection.store(load_concept_from_file(filename)) end end |
#save_concept_to_file(concept) ⇒ Object
33 34 35 36 |
# File 'lib/glossarist/concept_manager.rb', line 33 def save_concept_to_file(concept) filename = File.join(path, "concept-#{concept.id}.yaml") File.write(filename, Psych.dump(concept.to_h)) end |
#save_to_files(managed_concepts) ⇒ Object
Writes all concepts to files.
25 26 27 |
# File 'lib/glossarist/concept_manager.rb', line 25 def save_to_files(managed_concepts) managed_concepts.each_value &method(:save_concept_to_file) end |