Module: Fontist::Indexes::IndexMixin

Included in:
DefaultFamilyFontIndex, FilenameIndex, PreferredFamilyFontIndex
Defined in:
lib/fontist/indexes/index_mixin.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/fontist/indexes/index_mixin.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#add_formula(formula) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fontist/indexes/index_mixin.rb', line 41

def add_formula(formula)
  raise unless formula.is_a?(Formula)

  formula.all_fonts.each do |font|
    font.styles.each do |style|
      add_index_formula(style, formula.path)
    end
  end

  entries
end

#add_index_formula(style, formula_path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fontist/indexes/index_mixin.rb', line 57

def add_index_formula(style, formula_path)
  key = index_key_for_style(style)
  raise if key.nil? || key.empty?

  key = normalize_key(key)
  formula_path = Array(formula_path)
  paths = formula_path.map { |p| relative_formula_path(p) }

  if index_formula(key)
    index_formula(key).formula_path.concat(paths).uniq!
    return
  end

  entries << FormulaKeyToPath.new(
    key: key,
    formula_path: paths,
  )
end

#buildObject



31
32
33
34
35
36
37
38
39
# File 'lib/fontist/indexes/index_mixin.rb', line 31

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

  to_file

  self
end

#index_key_for_style(_style) ⇒ Object

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/fontist/indexes/index_mixin.rb', line 53

def index_key_for_style(_style)
  raise NotImplementedError, "index_key_for_style(style) must be implemented in including class"
end

#load_formulas(key) ⇒ Object



76
77
78
# File 'lib/fontist/indexes/index_mixin.rb', line 76

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

#load_index_formulas(key) ⇒ Object



80
81
82
# File 'lib/fontist/indexes/index_mixin.rb', line 80

def load_index_formulas(key)
  index_formulas(key)
end

#to_file(file_path = self.class.path) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/fontist/indexes/index_mixin.rb', line 84

def to_file(file_path = self.class.path)
  # Use default path if file_path is nil
  file_path = self.class.path if file_path.nil?
  # puts "Writing index to #{file_path}"

  FileUtils.mkdir_p(File.dirname(file_path))
  File.write(file_path, to_yaml)
end