Sha256: 791b12edc8a9555ce0f06e568c836305d39502fb534649d1477c98be6b7ca779

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

module Fontist
  module Manifest
    class Locations
      def initialize(manifest)
        @manifest = manifest
      end

      def self.from_file(file, **keywords)
        raise Fontist::Errors::ManifestCouldNotBeFoundError unless File.exist?(file)

        manifest = YAML.load_file(file)
        raise Fontist::Errors::ManifestCouldNotBeReadError unless manifest.is_a?(Hash)

        from_hash(manifest, **keywords)
      end

      def self.from_hash(manifest, **keywords)
        if keywords.empty?
          new(manifest).call
        else
          new(manifest, **keywords).call
        end
      end

      def call
        font_names.zip(font_paths).to_h
      end

      private

      attr_reader :manifest

      def font_names
        manifest.keys
      end

      def font_paths
        manifest.map do |font, styles|
          styles_to_ary = [styles].flatten
          style_paths_map(font, styles_to_ary)
        end
      end

      def style_paths_map(font, names)
        paths = style_paths(font, names)
        names.zip(paths).to_h
      end

      def style_paths(font, names)
        names.map do |style|
          file_paths(font, style)
        end
      end

      def file_paths(font, style)
        find_font_with_name(font, style).tap do |x|
          if x["paths"].empty?
            raise Errors::MissingFontError.new(font, style)
          end
        end
      end

      def find_font_with_name(font, style)
        Fontist::SystemFont.find_with_name(font, style).map { |k, v| [k.to_s, v] }.to_h
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
fontist-1.8.11 lib/fontist/manifest/locations.rb
fontist-1.8.10 lib/fontist/manifest/locations.rb
fontist-1.8.9 lib/fontist/manifest/locations.rb
fontist-1.8.8 lib/fontist/manifest/locations.rb
fontist-1.8.7 lib/fontist/manifest/locations.rb
fontist-1.8.6 lib/fontist/manifest/locations.rb
fontist-1.8.5 lib/fontist/manifest/locations.rb
fontist-1.8.4 lib/fontist/manifest/locations.rb
fontist-1.8.3 lib/fontist/manifest/locations.rb