Class: Glossarist::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Collection

Returns a new instance of Collection.

Parameters:

  • path (String) (defaults to: nil)

    concepts directory path, either absolute or relative to CWD



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

def initialize(path: nil)
  @path = path
  @index = {}
end

Instance Attribute Details

#pathString

Path to concepts directory.

Returns:

  • (String)


9
10
11
# File 'lib/glossarist/collection.rb', line 9

def path
  @path
end

Instance Method Details

#concepts_globObject



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

def concepts_glob
  File.file?(path) ? path : File.join(path, "concept-*.{yaml,yml}")
end

#each(&block) ⇒ Object



18
19
20
# File 'lib/glossarist/collection.rb', line 18

def each(&block)
  @index.each_value(&block)
end

#fetch(id) ⇒ Concept? Also known as: []

Returns concept with given ID, if it is present in collection, or nil otherwise.

Parameters:

  • id (String)

    Concept ID

Returns:



28
29
30
# File 'lib/glossarist/collection.rb', line 28

def fetch(id)
  @index[id]
end

#fetch_or_initialize(id) ⇒ Concept

If concept with given ID is present in this collection, returns that concept. Otherwise, instantiates a new concept, adds it to the collection, and returns it.

Parameters:

  • id (String)

    Concept ID

Returns:



40
41
42
# File 'lib/glossarist/collection.rb', line 40

def fetch_or_initialize(id)
  fetch(id) or store(Concept.of_yaml({ id: id }))
end

#load_concept_from_file(filename) ⇒ Object



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

def load_concept_from_file(filename)
  Concept.from_yaml(File.read(filename))
rescue Psych::SyntaxError => e
  raise Glossarist::ParseError.new(filename: filename, line: e.line)
end

#load_conceptsObject

Reads all concepts from files.



55
56
57
58
59
# File 'lib/glossarist/collection.rb', line 55

def load_concepts
  Dir.glob(concepts_glob) do |filename|
    store(load_concept_from_file(filename))
  end
end

#save_concept_to_file(concept) ⇒ Object



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

def save_concept_to_file(concept)
  filename = File.join(path, "concept-#{concept.id}.yaml")
  File.write(filename, Psych.dump(concept.to_h))
end

#save_conceptsObject

Writes all concepts to files.



62
63
64
65
66
# File 'lib/glossarist/collection.rb', line 62

def save_concepts
  @index.each_value do |concept|
    save_concept_to_file(concept)
  end
end

#store(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:

  • concept (Concept)

    concept about to be added



49
50
51
# File 'lib/glossarist/collection.rb', line 49

def store(concept)
  @index[concept.id] = concept
end