Class: Glossarist::Collection
- Inherits:
-
Object
- Object
- Glossarist::Collection
- Includes:
- Enumerable
- Defined in:
- lib/glossarist/collection.rb
Instance Attribute Summary collapse
-
#path ⇒ String
Path to concepts directory.
Instance Method Summary collapse
- #concepts_glob ⇒ Object
- #each(&block) ⇒ Object
-
#fetch(id) ⇒ Concept?
(also: #[])
Returns concept with given ID, if it is present in collection, or
nil
otherwise. -
#fetch_or_initialize(id) ⇒ Concept
If concept with given ID is present in this collection, returns that concept.
-
#initialize(path: nil) ⇒ Collection
constructor
A new instance of Collection.
- #load_concept_from_file(filename) ⇒ Object
-
#load_concepts ⇒ Object
Reads all concepts from files.
- #save_concept_to_file(concept) ⇒ Object
-
#save_concepts ⇒ Object
Writes all concepts to files.
-
#store(concept) ⇒ Object
(also: #<<)
Adds concept to the collection.
Constructor Details
#initialize(path: nil) ⇒ Collection
Returns a new instance of Collection.
13 14 15 16 |
# File 'lib/glossarist/collection.rb', line 13 def initialize(path: nil) @path = path @index = {} end |
Instance Attribute Details
#path ⇒ String
Path to concepts directory.
9 10 11 |
# File 'lib/glossarist/collection.rb', line 9 def path @path end |
Instance Method Details
#concepts_glob ⇒ Object
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.
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.
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_concepts ⇒ Object
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_concepts ⇒ Object
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.
49 50 51 |
# File 'lib/glossarist/collection.rb', line 49 def store(concept) @index[concept.id] = concept end |