Class: Glossarist::ConceptManager

Inherits:
Object
  • Object
show all
Defined in:
lib/glossarist/concept_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ ConceptManager

Returns a new instance of ConceptManager.

Parameters:

  • path (String) (defaults to: nil)

    concepts directory path, either absolute or relative to CWD



11
12
13
# File 'lib/glossarist/concept_manager.rb', line 11

def initialize(path: nil)
  @path = path
end

Instance Attribute Details

#pathString

Path to concepts directory.

Returns:

  • (String)


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