Sha256: 04bf38b111ecdd5ef05a8b9722f853a2ff58259a865d007e4bc2881411515490

Contents?: true

Size: 1.99 KB

Versions: 34

Compression:

Stored size: 1.99 KB

Contents

require_relative "index_formula"

module Fontist
  module Indexes
    class BaseIndex
      def self.from_yaml
        @from_yaml ||= begin
          unless Dir.exist?(Fontist.formulas_repo_path)
            raise Errors::MainRepoNotFoundError.new(
              "Please fetch formulas with `fontist update`.",
            )
          end

          rebuild unless File.exist?(path)

          data = YAML.load_file(path)
          new(data)
        end
      end

      def self.path
        raise NotImplementedError, "Please define path of an index"
      end

      def self.reset_cache
        @from_yaml = nil
      end

      def self.rebuild
        index = new
        index.build
        index.to_yaml
      end

      def initialize(data = {})
        @index = {}

        data.each_pair do |key, paths|
          paths.each do |path|
            add_index_formula(key, IndexFormula.new(path))
          end
        end
      end

      def build
        Formula.all.each do |formula|
          add_formula(formula)
        end
      end

      def add_formula(_formula)
        raise NotImplementedError, "Please define how to add formula to an index, use #add_index_formula"
      end

      def add_index_formula(key_raw, index_formula)
        key = normalize_key(key_raw)
        @index[key] ||= []
        @index[key] << index_formula unless @index[key].include?(index_formula)
      end

      def load_formulas(key)
        index_formulas(key).map(&:to_full)
      end

      def load_index_formulas(key)
        index_formulas(key)
      end

      def to_yaml
        dir = File.dirname(self.class.path)
        FileUtils.mkdir_p(dir) unless File.exist?(dir)
        File.write(self.class.path, YAML.dump(to_h))
      end

      def to_h
        @index.map do |key, index_formulas|
          [key, index_formulas.map(&:to_s)]
        end.to_h
      end

      private

      def index_formulas(key)
        @index[normalize_key(key)] || []
      end

      def normalize_key(key)
        key
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
fontist-1.21.2 lib/fontist/indexes/base_index.rb
fontist-1.21.1 lib/fontist/indexes/base_index.rb
fontist-1.20.0 lib/fontist/indexes/base_index.rb
fontist-1.19.0 lib/fontist/indexes/base_index.rb
fontist-1.18.2 lib/fontist/indexes/base_index.rb
fontist-1.17.1 lib/fontist/indexes/base_index.rb
fontist-1.17.0 lib/fontist/indexes/base_index.rb
fontist-1.16.0 lib/fontist/indexes/base_index.rb
fontist-1.15.2 lib/fontist/indexes/base_index.rb
fontist-1.15.1 lib/fontist/indexes/base_index.rb
fontist-1.15.0 lib/fontist/indexes/base_index.rb
fontist-1.14.6 lib/fontist/indexes/base_index.rb
fontist-1.14.5 lib/fontist/indexes/base_index.rb
fontist-1.14.4 lib/fontist/indexes/base_index.rb
fontist-1.14.3 lib/fontist/indexes/base_index.rb
fontist-1.14.2 lib/fontist/indexes/base_index.rb
fontist-1.14.1 lib/fontist/indexes/base_index.rb
fontist-1.14.0 lib/fontist/indexes/base_index.rb
fontist-1.13.2 lib/fontist/indexes/base_index.rb
fontist-1.13.1 lib/fontist/indexes/base_index.rb