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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fontist/indexes/index_mixin.rb', line 62

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fontist/indexes/index_mixin.rb', line 79

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



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

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

  to_file

  self
end

#build_with_formulas(formulas) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/fontist/indexes/index_mixin.rb', line 52

def build_with_formulas(formulas)
  formulas.each do |formula|
    add_formula(formula)
  end

  to_file

  self
end

#index_key_for_style(_style) ⇒ Object

Raises:

  • (NotImplementedError)


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

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

#load_formulas(key) ⇒ Object



98
99
100
# File 'lib/fontist/indexes/index_mixin.rb', line 98

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

#load_index_formulas(key) ⇒ Object



102
103
104
# File 'lib/fontist/indexes/index_mixin.rb', line 102

def load_index_formulas(key)
  index_formulas(key)
end

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



106
107
108
109
110
111
112
113
# File 'lib/fontist/indexes/index_mixin.rb', line 106

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