Sha256: 520acf330f4f9a38be6b0cd9568c5705709629a1c420a57a08d32e419ac7e9a8

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Glossarist
  class ConceptManager
    # Path to concepts directory.
    # @return [String]
    attr_accessor :path

    # @param path [String]
    #   concepts directory path, either absolute or relative to CWD
    def initialize(path: nil)
      @path = path
    end

    # Reads all concepts from files.
    def load_from_files(collection: nil)
      collection ||= ManagedConceptCollection.new

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

    # Writes all concepts to files.
    def save_to_files(managed_concepts)
      managed_concepts.each_value &method(:save_concept_to_file)
    end

    def load_concept_from_file(filename)
      ManagedConcept.new(Psych.safe_load(File.read(filename)))
    end

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

    private

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

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
glossarist-1.0.8 lib/glossarist/concept_manager.rb
glossarist-1.0.7 lib/glossarist/concept_manager.rb
glossarist-1.0.6 lib/glossarist/concept_manager.rb
glossarist-1.0.5 lib/glossarist/concept_manager.rb
glossarist-new-1.0.4 lib/glossarist/concept_manager.rb
glossarist-new-1.0.3 lib/glossarist/concept_manager.rb
glossarist-new-1.0.2 lib/glossarist/concept_manager.rb