Sha256: 272c47fdfbfb57c3615d973108a37ed4bcce9f0c5e749ba8ce8db19c84f8b2d5

Contents?: true

Size: 1.61 KB

Versions: 7

Compression:

Stored size: 1.61 KB

Contents

module Fontist
  class Font
    def initialize(options = {})
      @name = options.fetch(:name, nil)
      @confirmation = options.fetch(:confirmation, "no")

      check_or_create_fontist_path!
    end

    def self.all
      new.all
    end

    def self.find(name)
      new(name: name).find
    end

    def self.install(name, confirmation: "no")
      new(name: name, confirmation: confirmation).install
    end

    def find
      find_system_font || downloadable_font || raise(
        Fontist::Errors::NonSupportedFontError
      )
    end

    def install
      find_system_font || download_font || raise(
        Fontist::Errors::NonSupportedFontError
      )
    end

    def all
      Fontist::Formula.all.to_h.map { |_name, formula| formula.fonts }.flatten
    end

    private

    attr_reader :name, :confirmation

    def find_system_font
      Fontist::SystemFont.find(name)
    end

    def check_or_create_fontist_path!
      unless Fontist.fonts_path.exist?
        require "fileutils"
        FileUtils.mkdir_p(Fontist.fonts_path)
      end
    end

    def font_installer(formula)
      Object.const_get(formula.installer)
    end

    def formula
      @formula ||= Fontist::Formula.find(name)
    end

    def downloadable_font
      if formula
        raise(
          Fontist::Errors::MissingFontError,
          "Fonts are missing, please run " \
          "Fontist::Font.install('#{name}', confirmation: 'yes') to " \
          "download the font"
        )
      end
    end

    def download_font
      if formula
        font_installer(formula).fetch_font(name, confirmation: confirmation)
      end
    end
  end
end

Version data entries

7 entries across 6 versions & 2 rubygems

Version Path
fontist-1.2.1 lib/fontist/font.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/fontist-1.1.0/lib/fontist/font.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/fontist-1.2.0/lib/fontist/font.rb
fontist-1.2.0 lib/fontist/font.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/fontist-1.1.0/lib/fontist/font.rb
fontist-1.1.0 lib/fontist/font.rb
fontist-1.0.0 lib/fontist/font.rb