Sha256: 32acc74c5476d9f5db1dc260145eaf0d4cdcbf6af8ef13b372ea731def6b8b72

Contents?: true

Size: 1.44 KB

Versions: 8

Compression:

Stored size: 1.44 KB

Contents

require "git"
require "fontist/utils"
require "fontist/font_formula"
require "fontist/formula_template"

module Fontist
  module Formulas
    def self.register_formulas
      load_formulas
      update_registry
    end

    def self.fetch_formulas
      if Dir.exist?(Fontist.formulas_repo_path)
        Git.open(Fontist.formulas_repo_path).pull
      else
        Git.clone(Fontist.formulas_repo_url,
                  Fontist.formulas_repo_path,
                  depth: 1)
      end
    end

    def self.load_formulas
      Dir[Fontist.formulas_path.join("**/*.yml").to_s].sort.each do |file|
        create_formula_class(file)
      end
    end

    def self.create_formula_class(file)
      formula = parse_to_object(YAML.load_file(file))
      class_name = name_to_classname(formula.name)
      return if Formulas.const_defined?(class_name)

      klass = FormulaTemplate.create_formula_class(formula)
      Formulas.const_set(class_name, klass)
    end

    def self.parse_to_object(hash)
      JSON.parse(hash.to_json, object_class: OpenStruct)
    end

    def self.update_registry
      Formulas.constants.select do |constant|
        if Formulas.const_get(constant).is_a?(Class)
          klass = "Fontist::Formulas::#{constant}"
          Fontist::Registry.register(Object.const_get(klass))
        end
      end
    end

    def self.name_to_classname(name)
      cleanname = name.gsub(/ /, "").sub(/\S/, &:upcase)
      "#{cleanname}Font"
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
fontist-1.7.3 lib/fontist/formulas.rb
fontist-1.7.2 lib/fontist/formulas.rb
fontist-1.7.1 lib/fontist/formulas.rb
fontist-1.7.0 lib/fontist/formulas.rb
fontist-1.6.0 lib/fontist/formulas.rb
fontist-1.5.1 lib/fontist/formulas.rb
fontist-1.5.0 lib/fontist/formulas.rb
fontist-1.4.0 lib/fontist/formulas.rb